Phoenixak99 commited on
Commit
01d0dbc
Β·
verified Β·
1 Parent(s): 7e28919

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -75
app.py CHANGED
@@ -8,6 +8,7 @@ from io import BytesIO
8
  from tempfile import NamedTemporaryFile
9
  from xmlrpc.client import Binary
10
  import jwt
 
11
 
12
  import numpy as np
13
  import requests
@@ -322,10 +323,11 @@ def time_post_request(api_url, headers=None, payload=None, timeout=None):
322
  return response
323
 
324
  def generate_audio(genre, energy_level, tempo, description, duration):
325
- # Start the countdown with subscription info
326
- count_down = 30 # 300-second countdown for api initialize
327
- count_down_placeholder = st.empty()
328
  subscription_info_placeholder = st.empty()
 
329
 
330
  # Define the subscription tiers information
331
  subscription_info = """
@@ -346,20 +348,20 @@ def generate_audio(genre, energy_level, tempo, description, duration):
346
  **Tier 4 - $499/month**
347
  - 🎡 **Generations:** Up to 20 music generations per day.
348
  - πŸ“₯ **Downloads:** 20 downloads per month.
 
 
 
 
349
  """
350
 
351
- # Display the countdown and subscription info during the countdown period
352
- for seconds in range(count_down):
353
- remaining_seconds = count_down - seconds
354
- count_down_placeholder.info(
355
- f"⏳ **Preparing the server... Please wait:** {remaining_seconds} seconds remaining."
356
- )
357
  subscription_info_placeholder.markdown(subscription_info)
358
- time.sleep(1) # Wait for 1 second
359
-
360
- # Clear both placeholders after the countdown
361
- count_down_placeholder.empty()
362
- subscription_info_placeholder.empty()
363
 
364
  # Check if the user is logged in
365
  if st.session_state.get("jwt_token") is None:
@@ -393,12 +395,18 @@ def generate_audio(genre, energy_level, tempo, description, duration):
393
  # Check subscription plan and limits
394
  if has_exceeded_generation_limit:
395
  st.error(
396
- "You have reached your generation limit. "
397
- "Please upgrade your subscription for additional access."
398
- " [Upgrade Now](https://songlabai.com/subscribe/)"
399
- )
400
  return
401
 
 
 
 
 
 
 
402
  # Prepare the prompt and payload for the music generation API
403
  prompt = f"Genre: {genre}, Energy Level: {energy_level}, Tempo: {tempo}, Description: {description}"
404
  payload = {"inputs": {"prompt": prompt, "duration": duration}}
@@ -406,67 +414,71 @@ def generate_audio(genre, energy_level, tempo, description, duration):
406
  # Headers for the music generation API
407
  api_headers = get_api_headers()
408
 
409
- # Retry logic
410
  max_retries = 3
411
- retry_delay = 10 # seconds
412
  for attempt in range(max_retries):
 
 
413
  with st.spinner(f"Generating audio... Attempt {attempt+1}/{max_retries}"):
414
- response = time_post_request(API_URL, headers=api_headers, payload=payload, timeout=600)
415
-
416
- if response is None:
417
- st.warning(f"Attempt {attempt+1} failed due to network error. Retrying...")
418
- time.sleep(retry_delay)
419
- continue
420
-
421
- if response.status_code == 200:
422
- placeholder1 = st.empty()
423
- placeholder1.success(f"βœ”οΈ Audio Generated, Loading...")
424
- load_and_play_generated_audio(response)
425
- placeholder1.empty()
426
-
427
- # Now, update generation count
428
- update_generation_url = "https://songlabai.com/wp-json/custom-api/v1/update-generation-count"
429
- update_response = requests.post(update_generation_url, headers=auth_headers)
430
- if update_response.status_code != 200:
431
- st.error("Failed to update your generation count.")
432
-
433
- # Check if the user has now exceeded the limit
434
- subscription_status_url = "https://songlabai.com/wp-json/custom-api/v1/subscription"
435
- subscription_response = requests.get(subscription_status_url, headers=auth_headers)
436
- if subscription_response.status_code == 200:
437
- subscription_data = subscription_response.json()
438
- if subscription_data["has_exceeded_generation_limit"]:
439
- st.error(
440
- "You have reached your generation limit. "
441
- "Please upgrade your subscription for additional access."
442
- " [Upgrade Now](https://songlabai.com/subcribe/)"
443
- )
444
- return
445
- else:
446
- st.warning(f"Attempt {attempt+1} failed with status code {response.status_code}. Retrying...")
447
- time.sleep(retry_delay)
448
-
449
- st.error("Failed to generate audio after multiple attempts.")
450
-
451
-
452
-
453
-
454
- # Proceed to generate audio after checks
455
- prompt = f"Genre: {genre}, Energy Level: {energy_level}, Tempo: {tempo}, Description: {description},"
456
- payload = {"inputs": {"prompt": prompt, "duration": duration}}
457
-
458
- # Use the original headers for the API request
459
- with st.spinner("Generating audio ..."):
460
- response = time_post_request(API_URL, headers=auth_headers, payload=payload)
461
 
462
- placeholder1 = st.empty()
463
- if response.status_code != 200:
464
- st.error("Failed to generate audio.")
465
- return
466
- else:
467
- placeholder1.success(f"βœ”οΈ Audio Generated, Loading...")
468
- load_and_play_generated_audio(response)
469
- placeholder1.empty()
 
 
 
 
 
 
470
 
471
  def download_audio():
472
  if st_state.audio_pydub is None:
 
8
  from tempfile import NamedTemporaryFile
9
  from xmlrpc.client import Binary
10
  import jwt
11
+ import threading
12
 
13
  import numpy as np
14
  import requests
 
323
  return response
324
 
325
  def generate_audio(genre, energy_level, tempo, description, duration):
326
+ # Create placeholders for dynamic updates
327
+ status_placeholder = st.empty()
328
+ progress_placeholder = st.empty()
329
  subscription_info_placeholder = st.empty()
330
+ server_message_placeholder = st.empty()
331
 
332
  # Define the subscription tiers information
333
  subscription_info = """
 
348
  **Tier 4 - $499/month**
349
  - 🎡 **Generations:** Up to 20 music generations per day.
350
  - πŸ“₯ **Downloads:** 20 downloads per month.
351
+
352
+ ### πŸš€ **Why Subscribe?**
353
+ Premium subscribers enjoy priority access to dedicated servers,
354
+ significantly reducing wait times and server initialization delays.
355
  """
356
 
357
+ # Start the server initialization phase
358
+ for seconds in range(30): # 30-second initial countdown
359
+ remaining_seconds = 30 - seconds
360
+ status_placeholder.markdown("### πŸ”„ Initializing AI Music Server...")
361
+ progress_placeholder.progress(seconds / 30)
362
+ server_message_placeholder.info(f"⏳ Server warmup: {remaining_seconds}s remaining. Premium users get priority access!")
363
  subscription_info_placeholder.markdown(subscription_info)
364
+ time.sleep(1)
 
 
 
 
365
 
366
  # Check if the user is logged in
367
  if st.session_state.get("jwt_token") is None:
 
395
  # Check subscription plan and limits
396
  if has_exceeded_generation_limit:
397
  st.error(
398
+ "You have reached your generation limit. "
399
+ "Please upgrade your subscription for additional access."
400
+ " [Upgrade Now](https://songlabai.com/subscribe/)"
401
+ )
402
  return
403
 
404
+ # Clear initial countdown placeholders
405
+ status_placeholder.empty()
406
+ progress_placeholder.empty()
407
+ subscription_info_placeholder.empty()
408
+ server_message_placeholder.empty()
409
+
410
  # Prepare the prompt and payload for the music generation API
411
  prompt = f"Genre: {genre}, Energy Level: {energy_level}, Tempo: {tempo}, Description: {description}"
412
  payload = {"inputs": {"prompt": prompt, "duration": duration}}
 
414
  # Headers for the music generation API
415
  api_headers = get_api_headers()
416
 
417
+ # Enhanced retry logic with better feedback
418
  max_retries = 3
419
+ retry_delay = 30 # increased initial delay
420
  for attempt in range(max_retries):
421
+ current_delay = retry_delay * (attempt + 1) # Progressive delay
422
+
423
  with st.spinner(f"Generating audio... Attempt {attempt+1}/{max_retries}"):
424
+ try:
425
+ response = time_post_request(API_URL, headers=api_headers, payload=payload, timeout=600)
426
+
427
+ if response is None:
428
+ server_message_placeholder.warning(
429
+ "πŸ”„ Server is warming up. This may take a few minutes on first request. "
430
+ f"Retrying in {current_delay} seconds... \n\n"
431
+ "πŸ’‘ Premium subscribers enjoy faster access with dedicated servers!"
432
+ )
433
+ time.sleep(current_delay)
434
+ continue
435
+
436
+ if response.status_code == 200:
437
+ placeholder1 = st.empty()
438
+ placeholder1.success("βœ”οΈ Audio Generated, Loading...")
439
+ load_and_play_generated_audio(response)
440
+ placeholder1.empty()
441
+
442
+ # Update generation count
443
+ update_generation_url = "https://songlabai.com/wp-json/custom-api/v1/update-generation-count"
444
+ update_response = requests.post(update_generation_url, headers=auth_headers)
445
+ if update_response.status_code != 200:
446
+ st.error("Failed to update your generation count.")
447
+
448
+ # Check if the user has now exceeded the limit
449
+ subscription_status_url = "https://songlabai.com/wp-json/custom-api/v1/subscription"
450
+ subscription_response = requests.get(subscription_status_url, headers=auth_headers)
451
+ if subscription_response.status_code == 200:
452
+ subscription_data = subscription_response.json()
453
+ if subscription_data["has_exceeded_generation_limit"]:
454
+ st.error(
455
+ "You have reached your generation limit. "
456
+ "Please upgrade your subscription for additional access."
457
+ " [Upgrade Now](https://songlabai.com/subscribe/)"
458
+ )
459
+ return
460
+ else:
461
+ server_message_placeholder.warning(
462
+ f"⚠️ Attempt {attempt+1} failed with status code {response.status_code}. "
463
+ f"Retrying in {current_delay} seconds...\n\n"
464
+ "πŸ’‘ Premium users experience fewer delays and faster generation times!"
465
+ )
466
+ time.sleep(current_delay)
 
 
 
 
467
 
468
+ except requests.exceptions.Timeout:
469
+ server_message_placeholder.warning(
470
+ "⏱️ Request timed out while server is initializing.\n\n"
471
+ "πŸ’‘ Premium subscribers get priority access to ready servers!"
472
+ )
473
+ time.sleep(current_delay)
474
+ continue
475
+
476
+ # If all retries failed
477
+ st.error(
478
+ "❌ Failed to generate audio after multiple attempts.\n\n"
479
+ "Consider upgrading to premium for better server availability and faster generation times.\n\n"
480
+ "[Upgrade Now](https://songlabai.com/subscribe/)"
481
+ )
482
 
483
  def download_audio():
484
  if st_state.audio_pydub is None: