MySafeCode commited on
Commit
c053fbf
·
verified ·
1 Parent(s): ee7ac4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -38
app.py CHANGED
@@ -4,9 +4,13 @@ import requests
4
  import time
5
  from datetime import datetime
6
 
7
- # Polling function - defined FIRST
8
  def poll_status(task_id, single_poll=False):
9
- """Poll Suno API for task status using /api/v1/wav/record-info"""
 
 
 
 
10
  headers = {
11
  "Authorization": f"Bearer {st.session_state.suno_api_key}",
12
  "Content-Type": "application/json"
@@ -14,24 +18,32 @@ def poll_status(task_id, single_poll=False):
14
 
15
  try:
16
  with st.spinner(f"Checking status for task: {task_id}..."):
17
- # Poll using the correct endpoint
18
- response = requests.get(
19
- f"https://api.sunoapi.org/api/v1/wav/record-info",
20
- headers=headers,
21
- timeout=30
22
- )
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  if response.status_code == 200:
25
  result = response.json()
26
 
27
- # Add to poll results
28
- poll_entry = {
29
- "timestamp": datetime.now().strftime("%H:%M:%S"),
30
- "task_id": task_id,
31
- "response": result
32
- }
33
- st.session_state.poll_results.append(poll_entry)
34
-
35
  # Display result
36
  if result.get("code") == 200:
37
  data = result.get("data", {})
@@ -131,18 +143,18 @@ if 'auto_poll' not in st.session_state:
131
  if 'suno_api_key' not in st.session_state:
132
  st.session_state.suno_api_key = SUNO_API_KEY
133
 
 
134
  st.title("🎵 Suno API Audio Generator")
135
 
136
- # Info section
137
  with st.expander("📋 How it works", expanded=True):
138
  st.markdown("""
139
  ### Workflow:
140
- 1. **Step 1**: Submit task to Suno API → Get `taskId`
141
  2. **Step 2**: Suno processes audio (async)
142
  3. **Step 3**: Suno sends callback to: `https://1hit.no/wav/cb.php`
143
  4. **Step 4**: You can view callbacks at: `https://1hit.no/wav/view.php`
144
- 5. **Step 5**: OR use Poll button to check status via `/api/v1/wav/record-info`
145
-
146
  ### Polling Endpoint:
147
  ```javascript
148
  fetch('https://api.sunoapi.org/api/v1/wav/record-info', {
@@ -152,8 +164,13 @@ with st.expander("📋 How it works", expanded=True):
152
  }
153
  })
154
  ```
 
 
 
 
155
  """)
156
 
 
157
  col1, col2 = st.columns([2, 1])
158
 
159
  with col1:
@@ -179,15 +196,21 @@ with col1:
179
  "Content-Type": "application/json"
180
  }
181
 
 
 
 
 
182
  payload = {
183
  "taskId": task_id,
184
  "audioId": audio_id,
185
  "callBackUrl": CALLBACK_URL
186
  }
187
 
 
 
188
  try:
189
  response = requests.post(
190
- "https://api.sunoapi.org/api/v1/wav/generate",
191
  headers=headers,
192
  json=payload,
193
  timeout=30
@@ -217,6 +240,10 @@ with col1:
217
  """)
218
  else:
219
  st.error("❌ Unexpected response format")
 
 
 
 
220
  else:
221
  st.error(f"❌ API Error: {response.status_code}")
222
  try:
@@ -270,23 +297,6 @@ if auto_poll_btn:
270
  st.session_state.auto_poll_task_id = poll_task_id
271
  st.rerun()
272
 
273
- # Auto-polling logic
274
- if st.session_state.get('auto_poll') and st.session_state.get('auto_poll_task_id'):
275
- task_id = st.session_state.auto_poll_task_id
276
-
277
- # Create a placeholder for auto-poll status
278
- poll_placeholder = st.empty()
279
-
280
- with poll_placeholder.container():
281
- st.info(f"🔁 Auto-polling task: `{task_id[:12]}...` (every 10 seconds)")
282
-
283
- # Poll once
284
- poll_status(task_id, single_poll=True)
285
-
286
- # Schedule next poll
287
- time.sleep(10)
288
- st.rerun()
289
-
290
  # Display poll results if any
291
  if st.session_state.poll_results:
292
  st.markdown("---")
 
4
  import time
5
  from datetime import datetime
6
 
7
+ # Polling function
8
  def poll_status(task_id, single_poll=False):
9
+ """Poll Suno API for task status including task ID"""
10
+ if not task_id:
11
+ st.error("❌ Task ID cannot be empty")
12
+ return False
13
+
14
  headers = {
15
  "Authorization": f"Bearer {st.session_state.suno_api_key}",
16
  "Content-Type": "application/json"
 
18
 
19
  try:
20
  with st.spinner(f"Checking status for task: {task_id}..."):
21
+ # Two possible endpoints - let's try both formats
22
+ endpoints =[
23
+ f"https://api.sunoapi.org/api/v1/wav/record-info/{task_id}",
24
+ f"https://api.sunoapi.org/api/v1/wav/record-info?taskId={task_id}"
25
+ ]
 
26
 
27
+ response = None
28
+ for endpoint in endpoints:
29
+ try:
30
+ response = requests.get(
31
+ endpoint,
32
+ headers=headers,
33
+ timeout=30
34
+ )
35
+ if response.status_code == 200:
36
+ break
37
+ except:
38
+ continue
39
+
40
+ if not response:
41
+ st.error("❌ Failed to connect to polling endpoint")
42
+ return False
43
+
44
  if response.status_code == 200:
45
  result = response.json()
46
 
 
 
 
 
 
 
 
 
47
  # Display result
48
  if result.get("code") == 200:
49
  data = result.get("data", {})
 
143
  if 'suno_api_key' not in st.session_state:
144
  st.session_state.suno_api_key = SUNO_API_KEY
145
 
146
+ # Title and info
147
  st.title("🎵 Suno API Audio Generator")
148
 
 
149
  with st.expander("📋 How it works", expanded=True):
150
  st.markdown("""
151
  ### Workflow:
152
+ 1. **Step 1**: Submit task to Suno API → Get `taskId` from response
153
  2. **Step 2**: Suno processes audio (async)
154
  3. **Step 3**: Suno sends callback to: `https://1hit.no/wav/cb.php`
155
  4. **Step 4**: You can view callbacks at: `https://1hit.no/wav/view.php`
156
+ 5. **Step 5**: OR use Poll button to check status via `/api/v1/record-info` endpoint
157
+
158
  ### Polling Endpoint:
159
  ```javascript
160
  fetch('https://api.sunoapi.org/api/v1/wav/record-info', {
 
164
  }
165
  })
166
  ```
167
+
168
+ ### Error Handling:
169
+ - **400**: Task ID cannot be empty
170
+ - Make sure to include task ID in polling request
171
  """)
172
 
173
+ # Main layout
174
  col1, col2 = st.columns([2, 1])
175
 
176
  with col1:
 
196
  "Content-Type": "application/json"
197
  }
198
 
199
+ # Test sending to correct endpoint
200
+ endpoint_url = "https://api.sunoapi.org/api/v1/wav/generate"
201
+ st.info(f"Making request to: {endpoint_url}")
202
+
203
  payload = {
204
  "taskId": task_id,
205
  "audioId": audio_id,
206
  "callBackUrl": CALLBACK_URL
207
  }
208
 
209
+ st.json(payload)
210
+
211
  try:
212
  response = requests.post(
213
+ endpoint_url,
214
  headers=headers,
215
  json=payload,
216
  timeout=30
 
240
  """)
241
  else:
242
  st.error("❌ Unexpected response format")
243
+
244
+ # Debug info
245
+ st.markdown("**Debug Response:**")
246
+ st.code(str(result))
247
  else:
248
  st.error(f"❌ API Error: {response.status_code}")
249
  try:
 
297
  st.session_state.auto_poll_task_id = poll_task_id
298
  st.rerun()
299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  # Display poll results if any
301
  if st.session_state.poll_results:
302
  st.markdown("---")