Spaces:
Running
Running
Update app.py
Browse files
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 |
-
#
|
326 |
-
|
327 |
-
|
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 |
-
#
|
352 |
-
for seconds in range(
|
353 |
-
remaining_seconds =
|
354 |
-
|
355 |
-
|
356 |
-
)
|
357 |
subscription_info_placeholder.markdown(subscription_info)
|
358 |
-
time.sleep(1)
|
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 |
-
|
397 |
-
|
398 |
-
|
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 |
-
#
|
410 |
max_retries = 3
|
411 |
-
retry_delay =
|
412 |
for attempt in range(max_retries):
|
|
|
|
|
413 |
with st.spinner(f"Generating audio... Attempt {attempt+1}/{max_retries}"):
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
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 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|