Phoenixak99 commited on
Commit
af04993
·
verified ·
1 Parent(s): eb0cb9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -23
app.py CHANGED
@@ -6,6 +6,9 @@ import uuid
6
  from datetime import datetime
7
  from io import BytesIO
8
  from tempfile import NamedTemporaryFile
 
 
 
9
  import requests
10
  import scipy
11
  import soundfile as sf
@@ -19,15 +22,6 @@ from wordpress_xmlrpc import Client
19
  from wordpress_xmlrpc.compat import xmlrpc_client
20
  from wordpress_xmlrpc.methods import media
21
 
22
- # Function to extract nonce from URL parameters
23
- def get_nonce_from_url():
24
- params = st.experimental_get_query_params()
25
- if 'nonce' in params:
26
- return params['nonce'][0] # Extract the nonce from the query parameters
27
- return None
28
-
29
- nonce = get_nonce_from_url()
30
-
31
  # API URLs and Tokens
32
  API_URL = os.getenv("API_URL", None)
33
  BEARER_TOKEN = os.getenv("BEARER_TOKEN", None)
@@ -40,11 +34,10 @@ if not BEARER_TOKEN:
40
  st.error("BEARER_TOKEN environment variable is not set.")
41
  st.stop()
42
 
43
- # Define the headers for requests and include nonce
44
  headers = {
45
  "Authorization": f"Bearer {BEARER_TOKEN}",
46
  "Content-Type": "application/json",
47
- "X-WP-Nonce": nonce # Include nonce in the headers
48
  }
49
 
50
  # Add background image for Streamlit app
@@ -58,28 +51,42 @@ background-size: cover;
58
  '''
59
  st.markdown(page_bg_img, unsafe_allow_html=True)
60
 
61
- # Function to check subscription status via WordPress API
 
 
 
 
 
 
 
 
 
62
  def check_subscription():
63
  if nonce:
64
- response = requests.get("https://songlabai.com/wp-json/custom/v1/subscription-status", headers=headers)
 
 
 
65
  if response.status_code == 200:
66
  return response.json()
67
  else:
 
68
  return None
69
  else:
70
- st.error("Nonce not found in the URL.")
71
  return None
72
 
 
 
 
 
73
  # Function to update generation timestamp for free users
74
  def update_generation_time():
75
- if nonce:
76
- response = requests.post("https://songlabai.com/wp-json/custom/v1/update-generation-time", headers=headers)
77
- if response.status_code == 200:
78
- st.success("Generation time updated.")
79
- else:
80
- st.error("Failed to update generation time.")
81
  else:
82
- st.error("Nonce not found in the URL.")
83
 
84
  # Display title
85
  st.title("Songlab AI")
@@ -140,7 +147,7 @@ def generate_audio(genre, energy_level, tempo, description, duration):
140
  prompt = f"Genre: {genre}, Energy Level: {energy_level}, Tempo: {tempo}, Description: {description}"
141
  payload = {"inputs": {"prompt": prompt, "duration": duration}}
142
  with st.spinner("Generating audio ..."):
143
- response = time_post_request(API_URL, headers=headers, payload=payload)
144
  if response.status_code == 200:
145
  st.success("✔️ Audio Generated, Loading...")
146
  load_and_play_generated_audio(response)
@@ -169,4 +176,4 @@ if st.button("Generate Audio"):
169
  if description:
170
  generate_audio(genre, energy_level, tempo, description, duration)
171
  else:
172
- st.info("Description field is required.")
 
6
  from datetime import datetime
7
  from io import BytesIO
8
  from tempfile import NamedTemporaryFile
9
+ from xmlrpc.client import Binary
10
+
11
+ import numpy as np
12
  import requests
13
  import scipy
14
  import soundfile as sf
 
22
  from wordpress_xmlrpc.compat import xmlrpc_client
23
  from wordpress_xmlrpc.methods import media
24
 
 
 
 
 
 
 
 
 
 
25
  # API URLs and Tokens
26
  API_URL = os.getenv("API_URL", None)
27
  BEARER_TOKEN = os.getenv("BEARER_TOKEN", None)
 
34
  st.error("BEARER_TOKEN environment variable is not set.")
35
  st.stop()
36
 
37
+ # Define the headers for requests
38
  headers = {
39
  "Authorization": f"Bearer {BEARER_TOKEN}",
40
  "Content-Type": "application/json",
 
41
  }
42
 
43
  # Add background image for Streamlit app
 
51
  '''
52
  st.markdown(page_bg_img, unsafe_allow_html=True)
53
 
54
+ # Function to extract nonce from URL parameters
55
+ def get_nonce_from_url():
56
+ params = st.experimental_get_query_params()
57
+ if 'nonce' in params:
58
+ return params['nonce'][0] # Extract the nonce from the query parameters
59
+ return None
60
+
61
+ nonce = get_nonce_from_url()
62
+
63
+ # Use the nonce in your API requests
64
  def check_subscription():
65
  if nonce:
66
+ headers = {
67
+ "X-WP-Nonce": nonce, # Pass the nonce in the request headers
68
+ }
69
+ response = requests.get("songlabai.com/wp-json/custom/v1/subscription-status", headers=headers)
70
  if response.status_code == 200:
71
  return response.json()
72
  else:
73
+ st.error("Failed to fetch subscription status.")
74
  return None
75
  else:
76
+ st.error("Nonce not found.")
77
  return None
78
 
79
+ # Call the subscription status function
80
+ subscription_status = check_subscription()
81
+ st.write("Subscription Status:", subscription_status)
82
+
83
  # Function to update generation timestamp for free users
84
  def update_generation_time():
85
+ response = requests.post("https://songlabai.com/wp-json/custom/v1/update-generation-time")
86
+ if response.status_code == 200:
87
+ st.success("Generation time updated.")
 
 
 
88
  else:
89
+ st.error("Failed to update generation time.")
90
 
91
  # Display title
92
  st.title("Songlab AI")
 
147
  prompt = f"Genre: {genre}, Energy Level: {energy_level}, Tempo: {tempo}, Description: {description}"
148
  payload = {"inputs": {"prompt": prompt, "duration": duration}}
149
  with st.spinner("Generating audio ..."):
150
+ response = time_post_request(API_URL, headers, payload)
151
  if response.status_code == 200:
152
  st.success("✔️ Audio Generated, Loading...")
153
  load_and_play_generated_audio(response)
 
176
  if description:
177
  generate_audio(genre, energy_level, tempo, description, duration)
178
  else:
179
+ st.info("Description field is required.")