Spaces:
Running
Running
Phoenixak99
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -329,100 +329,99 @@ def time_post_request(api_url, headers=None, payload=None):
|
|
329 |
|
330 |
return response
|
331 |
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
else:
|
380 |
-
# Update user's generation count
|
381 |
-
update_generation_url = "https://songlabai.com/wp-json/custom-api/update-generation-count"
|
382 |
-
update_response = requests.post(update_generation_url, headers=auth_headers)
|
383 |
-
if update_response.status_code != 200:
|
384 |
-
st.error("Failed to update your generation count.")
|
385 |
-
return
|
386 |
-
# Proceed to generate audio
|
387 |
-
elif subscription_plan_id in ["304", "305", "306"]:
|
388 |
-
if has_exceeded_generation_limit:
|
389 |
-
st.error("You have reached your daily generation limit.")
|
390 |
-
return
|
391 |
-
else:
|
392 |
-
# Update user's generation count
|
393 |
-
update_generation_url = "https://songlabai.com/wp-json/custom-api/update-generation-count"
|
394 |
-
update_response = requests.post(update_generation_url, headers=auth_headers)
|
395 |
-
if update_response.status_code != 200:
|
396 |
-
st.error("Failed to update your generation count.")
|
397 |
-
return
|
398 |
-
# Proceed to generate audio
|
399 |
else:
|
400 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
401 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
else:
|
403 |
-
st.error("
|
404 |
return
|
405 |
else:
|
406 |
-
st.error("Failed to retrieve your
|
407 |
return
|
|
|
|
|
|
|
408 |
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
# Use the original headers for the API request
|
414 |
-
with st.spinner("Generating audio ..."):
|
415 |
-
response = time_post_request(API_URL, headers, payload)
|
416 |
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
return
|
421 |
-
else:
|
422 |
-
placeholder1.success(f"✔️ Audio Generated, Loading...")
|
423 |
-
load_and_play_generated_audio(response)
|
424 |
-
placeholder1.empty()
|
425 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
426 |
|
427 |
|
428 |
|
@@ -467,11 +466,11 @@ def load_and_play_generated_audio(response):
|
|
467 |
st.write(f"To download use the following product code: {product_code}")
|
468 |
|
469 |
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
|
476 |
|
477 |
# Post-processing options
|
|
|
329 |
|
330 |
return response
|
331 |
|
332 |
+
def generate_audio(genre, energy_level, tempo, description, duration):
|
333 |
+
# Start the countdown
|
334 |
+
count_down = 10
|
335 |
+
count_down_placeholder = st.empty()
|
336 |
+
for seconds in range(count_down):
|
337 |
+
count_down_placeholder.info(
|
338 |
+
f"Preparing the server... Please wait ⏳ {count_down - seconds} seconds."
|
339 |
+
)
|
340 |
+
time.sleep(1)
|
341 |
+
count_down_placeholder.empty()
|
342 |
|
343 |
+
# Check if the user is logged in
|
344 |
+
if st.session_state.get("jwt_token") is None:
|
345 |
+
st.error("You must be logged in to generate audio.")
|
346 |
+
return
|
347 |
|
348 |
+
jwt_token = st.session_state["jwt_token"]
|
349 |
+
|
350 |
+
# Headers for authenticated requests
|
351 |
+
auth_headers = {
|
352 |
+
"Authorization": f"Bearer {jwt_token}"
|
353 |
+
}
|
354 |
+
|
355 |
+
# Get user ID
|
356 |
+
user_id_url = "https://songlabai.com/wp-json/custom-api/v1/current-user-id"
|
357 |
+
user_response = requests.get(user_id_url, headers=auth_headers)
|
358 |
+
if user_response.status_code == 200:
|
359 |
+
user_data = user_response.json()
|
360 |
+
user_id = user_data["user_id"]
|
361 |
+
st.write("User Data:", user_data)
|
362 |
+
st.write("user_id:", user_id)
|
363 |
+
|
364 |
+
# Get subscription status
|
365 |
+
subscription_url = "https://songlabai.com/wp-json/custom-api/subscription"
|
366 |
+
subscription_response = requests.get(subscription_url, headers=auth_headers)
|
367 |
+
if subscription_response.status_code == 200:
|
368 |
+
subscription_data = subscription_response.json()
|
369 |
+
subscription_plan_id = subscription_data["subscription_plan_id"]
|
370 |
+
st.write("subscription_plan_id:", subscription_plan_id)
|
371 |
+
has_exceeded_generation_limit = subscription_data["has_exceeded_generation_limit"]
|
372 |
+
st.write("has_exceeded_generation_limit:", has_exceeded_generation_limit)
|
373 |
+
|
374 |
+
# Check subscription plan and limits
|
375 |
+
if subscription_plan_id in [None, ""]: # Free user
|
376 |
+
if has_exceeded_generation_limit:
|
377 |
+
st.error("You have reached your generation limit.")
|
378 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
else:
|
380 |
+
# Update user's generation count
|
381 |
+
update_generation_url = "https://songlabai.com/wp-json/custom-api/update-generation-count"
|
382 |
+
update_response = requests.post(update_generation_url, headers=auth_headers)
|
383 |
+
if update_response.status_code != 200:
|
384 |
+
st.error("Failed to update your generation count.")
|
385 |
+
return
|
386 |
+
# Proceed to generate audio
|
387 |
+
elif subscription_plan_id in ["304", "305", "306"]:
|
388 |
+
if has_exceeded_generation_limit:
|
389 |
+
st.error("You have reached your daily generation limit.")
|
390 |
return
|
391 |
+
else:
|
392 |
+
# Update user's generation count
|
393 |
+
update_generation_url = "https://songlabai.com/wp-json/custom-api/update-generation-count"
|
394 |
+
update_response = requests.post(update_generation_url, headers=auth_headers)
|
395 |
+
if update_response.status_code != 200:
|
396 |
+
st.error("Failed to update your generation count.")
|
397 |
+
return
|
398 |
+
# Proceed to generate audio
|
399 |
else:
|
400 |
+
st.error("Your subscription does not allow you to generate music.")
|
401 |
return
|
402 |
else:
|
403 |
+
st.error("Failed to retrieve your subscription status.")
|
404 |
return
|
405 |
+
else:
|
406 |
+
st.error("Failed to retrieve your user information.")
|
407 |
+
return
|
408 |
|
409 |
+
# Proceed to generate audio after checks
|
410 |
+
prompt = f"Genre: {genre}, Energy Level: {energy_level}, Tempo: {tempo}, Description: {description},"
|
411 |
+
payload = {"inputs": {"prompt": prompt, "duration": duration}}
|
|
|
|
|
|
|
|
|
412 |
|
413 |
+
# Use the original headers for the API request
|
414 |
+
with st.spinner("Generating audio ..."):
|
415 |
+
response = time_post_request(API_URL, headers, payload)
|
|
|
|
|
|
|
|
|
|
|
416 |
|
417 |
+
placeholder1 = st.empty()
|
418 |
+
if response.status_code != 200:
|
419 |
+
st.error("Failed to generate audio.")
|
420 |
+
return
|
421 |
+
else:
|
422 |
+
placeholder1.success(f"✔️ Audio Generated, Loading...")
|
423 |
+
load_and_play_generated_audio(response)
|
424 |
+
placeholder1.empty()
|
425 |
|
426 |
|
427 |
|
|
|
466 |
st.write(f"To download use the following product code: {product_code}")
|
467 |
|
468 |
|
469 |
+
if st.button("Generate Audio", key="generate_audio_button"):
|
470 |
+
if genre and energy_level and description and tempo:
|
471 |
+
generate_audio(genre, energy_level, tempo, description, duration)
|
472 |
+
else:
|
473 |
+
st.info("Description field is required.")
|
474 |
|
475 |
|
476 |
# Post-processing options
|