Spaces:
Running
Running
Phoenixak99
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -160,11 +160,13 @@ headers = {
|
|
160 |
|
161 |
# Streamlit app title
|
162 |
st.title("Songlab AI")
|
163 |
-
cookies = cookie_manager.get_all(key='
|
164 |
|
165 |
|
166 |
# Initialize session state for JWT token
|
167 |
if "jwt_token" not in st.session_state:
|
|
|
|
|
168 |
# Check if jwt_token is stored in cookies
|
169 |
jwt_token = cookies.get('jwt_token')
|
170 |
if jwt_token:
|
@@ -172,16 +174,14 @@ if "jwt_token" not in st.session_state:
|
|
172 |
else:
|
173 |
st.session_state["jwt_token"] = None
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
if st.session_state["jwt_token"] is None:
|
178 |
st.header("Please log in to continue")
|
179 |
-
username = st.text_input("Username")
|
180 |
-
password = st.text_input("Password", type="password")
|
181 |
# Create two columns for the buttons
|
182 |
col1, col2 = st.columns([1, 1])
|
183 |
with col1:
|
184 |
-
if st.button("Log in"):
|
185 |
# Send POST request to WordPress JWT authentication endpoint
|
186 |
login_url = "https://songlabai.com/wp-json/jwt-auth/v1/token"
|
187 |
data = {
|
@@ -194,35 +194,35 @@ if st.session_state["jwt_token"] is None:
|
|
194 |
jwt_token = result["token"]
|
195 |
# Set jwt_token in session state and cookie
|
196 |
st.session_state["jwt_token"] = jwt_token
|
197 |
-
cookie_manager.set('jwt_token', jwt_token)
|
198 |
st.success("Logged in successfully!")
|
199 |
st.experimental_rerun()
|
200 |
else:
|
201 |
st.error("Invalid username or password")
|
202 |
st.stop()
|
203 |
with col2:
|
204 |
-
if st.button("Don't have an account?"):
|
205 |
js = "window.location.href = 'https://songlabai.com/register/';"
|
206 |
html = '<script>{}</script>'.format(js)
|
207 |
st.components.v1.html(html)
|
208 |
st.stop()
|
209 |
else:
|
210 |
# User is logged in, continue with the app
|
211 |
-
if st.button("Log out"):
|
212 |
-
# Clear
|
213 |
-
st.session_state
|
|
|
214 |
# Synchronize cookies
|
215 |
-
cookies = cookie_manager.get_all(key='
|
216 |
-
#
|
217 |
-
|
218 |
cookie_manager.delete('jwt_token', key='delete_jwt_token')
|
219 |
-
except KeyError:
|
220 |
-
print("Cookie 'jwt_token' does not exist.")
|
221 |
st.success("Logged out successfully!")
|
222 |
st.experimental_rerun()
|
223 |
|
224 |
|
225 |
|
|
|
226 |
# Initialize session state variables
|
227 |
if "vocal_audio" not in st_state:
|
228 |
st_state.vocal_audio = None
|
@@ -329,94 +329,100 @@ def time_post_request(api_url, headers=None, payload=None):
|
|
329 |
|
330 |
return response
|
331 |
|
332 |
-
def generate_audio(genre, energy_level, tempo, description, duration):
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
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) # Add this line to display the user data
|
362 |
-
st.write("user_id:", user_id) # Add this line to display the user data
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
# Get subscription status
|
367 |
-
subscription_url = "https://songlabai.com/wp-json/custom-api/subscription"
|
368 |
-
subscription_response = requests.get(subscription_url, headers=auth_headers)
|
369 |
-
if subscription_response.status_code == 200:
|
370 |
-
subscription_data = subscription_response.json()
|
371 |
-
subscription_plan_id = subscription_data["subscription_plan_id"]
|
372 |
-
st.write("subscription_plan_id:", subscription_plan_id) # Add this line to display the user data
|
373 |
-
|
374 |
-
has_exceeded_daily_limit = subscription_data["has_exceeded_daily_limit"]
|
375 |
-
st.write("has_exceeded_daily_limit:", has_exceeded_daily_limit) # Add this line to display the user data
|
376 |
|
|
|
|
|
|
|
|
|
377 |
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
return
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
394 |
else:
|
395 |
-
st.error("
|
396 |
return
|
397 |
else:
|
398 |
-
st.error("Failed to retrieve your
|
399 |
return
|
400 |
-
else:
|
401 |
-
st.error("Failed to retrieve your user information.")
|
402 |
-
return
|
403 |
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
|
408 |
-
|
409 |
-
|
410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
411 |
|
412 |
-
placeholder1 = st.empty()
|
413 |
-
if response.status_code != 200:
|
414 |
-
st.error("Failed to generate audio.")
|
415 |
-
return
|
416 |
-
else:
|
417 |
-
placeholder1.success(f"✔️ Audio Generated, Loading...")
|
418 |
-
load_and_play_generated_audio(response)
|
419 |
-
placeholder1.empty()
|
420 |
|
421 |
|
422 |
|
@@ -461,12 +467,12 @@ def load_and_play_generated_audio(response):
|
|
461 |
st.write(f"To download use the following product code: {product_code}")
|
462 |
|
463 |
|
464 |
-
if st.button("Generate Audio"):
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
|
471 |
# Post-processing options
|
472 |
st.header("Post-processing Options")
|
|
|
160 |
|
161 |
# Streamlit app title
|
162 |
st.title("Songlab AI")
|
163 |
+
cookies = cookie_manager.get_all(key='init_cookies')
|
164 |
|
165 |
|
166 |
# Initialize session state for JWT token
|
167 |
if "jwt_token" not in st.session_state:
|
168 |
+
# Synchronize cookies
|
169 |
+
cookies = cookie_manager.get_all(key='get_cookies')
|
170 |
# Check if jwt_token is stored in cookies
|
171 |
jwt_token = cookies.get('jwt_token')
|
172 |
if jwt_token:
|
|
|
174 |
else:
|
175 |
st.session_state["jwt_token"] = None
|
176 |
|
177 |
+
if st.session_state.get("jwt_token") is None:
|
|
|
|
|
178 |
st.header("Please log in to continue")
|
179 |
+
username = st.text_input("Username", key="username_input")
|
180 |
+
password = st.text_input("Password", type="password", key="password_input")
|
181 |
# Create two columns for the buttons
|
182 |
col1, col2 = st.columns([1, 1])
|
183 |
with col1:
|
184 |
+
if st.button("Log in", key="login_button"):
|
185 |
# Send POST request to WordPress JWT authentication endpoint
|
186 |
login_url = "https://songlabai.com/wp-json/jwt-auth/v1/token"
|
187 |
data = {
|
|
|
194 |
jwt_token = result["token"]
|
195 |
# Set jwt_token in session state and cookie
|
196 |
st.session_state["jwt_token"] = jwt_token
|
197 |
+
cookie_manager.set('jwt_token', jwt_token, key='set_jwt_token')
|
198 |
st.success("Logged in successfully!")
|
199 |
st.experimental_rerun()
|
200 |
else:
|
201 |
st.error("Invalid username or password")
|
202 |
st.stop()
|
203 |
with col2:
|
204 |
+
if st.button("Don't have an account?", key="register_button"):
|
205 |
js = "window.location.href = 'https://songlabai.com/register/';"
|
206 |
html = '<script>{}</script>'.format(js)
|
207 |
st.components.v1.html(html)
|
208 |
st.stop()
|
209 |
else:
|
210 |
# User is logged in, continue with the app
|
211 |
+
if st.button("Log out", key="logout_button"):
|
212 |
+
# Clear all session state variables
|
213 |
+
for key in list(st.session_state.keys()):
|
214 |
+
del st.session_state[key]
|
215 |
# Synchronize cookies
|
216 |
+
cookies = cookie_manager.get_all(key='get_cookies_logout')
|
217 |
+
# Delete the cookie if it exists
|
218 |
+
if 'jwt_token' in cookies:
|
219 |
cookie_manager.delete('jwt_token', key='delete_jwt_token')
|
|
|
|
|
220 |
st.success("Logged out successfully!")
|
221 |
st.experimental_rerun()
|
222 |
|
223 |
|
224 |
|
225 |
+
|
226 |
# Initialize session state variables
|
227 |
if "vocal_audio" not in st_state:
|
228 |
st_state.vocal_audio = None
|
|
|
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 |
|
428 |
|
|
|
467 |
st.write(f"To download use the following product code: {product_code}")
|
468 |
|
469 |
|
470 |
+
if st.button("Generate Audio", key="generate_audio_button"):
|
471 |
+
if genre and energy_level and description and tempo:
|
472 |
+
generate_audio(genre, energy_level, tempo, description, duration)
|
473 |
+
else:
|
474 |
+
st.info("Description field is required.")
|
475 |
+
|
476 |
|
477 |
# Post-processing options
|
478 |
st.header("Post-processing Options")
|