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