MySafeCode commited on
Commit
7a6d6f3
·
verified ·
1 Parent(s): 1bf9e40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -10,6 +10,9 @@ SUNO_KEY = os.environ.get("SunoKey", "")
10
  if not SUNO_KEY:
11
  print("⚠️ SunoKey not set!")
12
 
 
 
 
13
  def generate_song_from_text(lyrics_text, style, title, instrumental, model):
14
  """Generate a song from lyrics text"""
15
  if not SUNO_KEY:
@@ -34,7 +37,7 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
34
  "customMode": True,
35
  "instrumental": instrumental,
36
  "model": model,
37
- "callBackUrl": "https://mysafecode-suno-api-v5.hf.space", # Empty callback URL - we'll poll instead
38
  "style": style,
39
  "title": title,
40
  }
@@ -78,6 +81,7 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
78
  yield f"**Instrumental:** {'Yes' if instrumental else 'No'}\n"
79
  if not instrumental:
80
  yield f"**Lyrics length:** {len(lyrics_text)} characters\n\n"
 
81
 
82
  # Submit generation request
83
  try:
@@ -104,11 +108,12 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
104
  yield f"✅ **Request submitted successfully!**\n"
105
  yield f"**Task ID:** `{task_id}`\n\n"
106
  yield f"⏳ Song generation started...\n\n"
107
- yield "**What to do next:**\n"
108
- yield "1. Keep this window open while the song generates\n"
109
- yield "2. We'll automatically check the status every 10 seconds\n"
110
- yield "3. Generation typically takes 1-3 minutes\n"
111
- yield "4. When complete, you'll get streaming and download links\n\n"
 
112
 
113
  except Exception as e:
114
  yield f"❌ Error submitting request: {str(e)}"
@@ -158,6 +163,7 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
158
  yield "2. Log in to your account\n"
159
  yield "3. Go to 'Generation History'\n"
160
  yield "4. Find your song by Task ID\n"
 
161
  return
162
 
163
  # Look for songs in different possible locations
@@ -168,12 +174,6 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
168
  songs = response_data.get("data", [])
169
  if not songs:
170
  songs = response_data.get("songs", [])
171
- if not songs:
172
- # Check if response_data itself is a song list
173
- if isinstance(response_data.get("0"), dict):
174
- songs = [response_data.get("0")]
175
- if isinstance(response_data.get("1"), dict):
176
- songs.append(response_data.get("1"))
177
 
178
  elif isinstance(response_data, list):
179
  songs = response_data
@@ -216,6 +216,7 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
216
  yield "- Download links may take 2-3 minutes\n"
217
  yield "- Files are kept for 15 days\n"
218
  yield f"- Task ID: `{task_id}` (save this for reference)\n"
 
219
  return
220
  else:
221
  # No songs found in response
@@ -228,6 +229,7 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
228
  yield "2. Log in to your account\n"
229
  yield "3. Check 'Generation History'\n"
230
  yield "4. Look for this Task ID\n"
 
231
  return
232
 
233
  elif current_status == "FAILED":
@@ -236,14 +238,16 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
236
  yield f"**Status:** {current_status}\n"
237
  yield f"**Error:** {error_msg}\n"
238
  yield f"**Task ID:** `{task_id}`\n\n"
239
- yield "Please try again with different parameters."
 
240
  return
241
 
242
  elif current_status in ["PENDING", "PROCESSING"]:
243
  yield f"⏳ **Status Update**\n\n"
244
  yield f"**Current Status:** {current_status}\n"
245
  yield f"**Task ID:** `{task_id}`\n"
246
- yield f"**Check:** {attempt + 1}/{max_attempts}\n\n"
 
247
  yield "**Estimated time remaining:**\n"
248
  yield "- Stream URL: 30-60 seconds\n"
249
  yield "- Download URL: 2-3 minutes\n"
@@ -252,7 +256,8 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
252
  else:
253
  yield f"📊 **Status:** {current_status}\n"
254
  yield f"**Task ID:** `{task_id}`\n"
255
- yield f"**Check:** {attempt + 1}/{max_attempts}\n\n"
 
256
  yield "Still processing...\n"
257
 
258
  else:
@@ -261,27 +266,32 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
261
  yield f"⚠️ **API Error**\n\n"
262
  yield f"**Code:** {check_data.get('code')}\n"
263
  yield f"**Message:** {error_msg}\n"
264
- yield f"**Task ID:** `{task_id}`\n\n"
 
265
  yield "Will continue checking...\n"
266
 
267
  else:
268
  yield f"⚠️ **HTTP Error {check_resp.status_code}**\n\n"
269
  yield f"Failed to check status. Will retry in 10 seconds...\n"
270
  yield f"Check: {attempt + 1}/{max_attempts}\n"
 
271
 
272
  except requests.exceptions.Timeout:
273
  yield f"⏱️ **Timeout checking status**\n\n"
274
  yield "The status check timed out. Will try again in 10 seconds...\n"
275
  yield f"Check: {attempt + 1}/{max_attempts}\n"
 
276
 
277
  except Exception as e:
278
  yield f"⚠️ **Error checking status:** {str(e)}\n\n"
279
  yield "Will retry in 10 seconds...\n"
280
  yield f"Check: {attempt + 1}/{max_attempts}\n"
 
281
 
282
  # If we get here, we timed out
283
  yield "⏰ **Timeout after 10 minutes**\n\n"
284
- yield f"**Task ID:** `{task_id}`\n\n"
 
285
  yield "**What to do:**\n"
286
  yield "1. The song may still be processing\n"
287
  yield "2. Visit https://sunoapi.org\n"
@@ -401,12 +411,11 @@ with gr.Blocks(title="Suno Song Generator", theme="soft") as app:
401
  4. Choose model (V4_5ALL recommended)
402
  5. Click Generate Song!
403
 
404
- **Tips:**
405
- - Use structured lyrics with verses/chorus for best results
406
- - Style examples: "Pop", "Rock guitar", "Jazz piano", "Electronic"
407
- - V5: Latest model, best quality
408
- - V4_5ALL: Good balance, up to 8 minutes
409
- - Instrumental: Check for music only (no vocals)
410
 
411
  **Generation time:**
412
  - 30-60s: Stream URL ready
@@ -474,4 +483,6 @@ with gr.Blocks(title="Suno Song Generator", theme="soft") as app:
474
  if __name__ == "__main__":
475
  print("🚀 Starting Suno Song Generator")
476
  print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")
 
 
477
  app.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
10
  if not SUNO_KEY:
11
  print("⚠️ SunoKey not set!")
12
 
13
+ # Get the current Space URL for callbacks
14
+ SPACE_URL = os.environ.get("SPACE_URL", "https://mysafecode-suno-api-v5.hf.space")
15
+
16
  def generate_song_from_text(lyrics_text, style, title, instrumental, model):
17
  """Generate a song from lyrics text"""
18
  if not SUNO_KEY:
 
37
  "customMode": True,
38
  "instrumental": instrumental,
39
  "model": model,
40
+ "callBackUrl": f"{SPACE_URL}/callback", # Use the Space URL for callbacks
41
  "style": style,
42
  "title": title,
43
  }
 
81
  yield f"**Instrumental:** {'Yes' if instrumental else 'No'}\n"
82
  if not instrumental:
83
  yield f"**Lyrics length:** {len(lyrics_text)} characters\n\n"
84
+ yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"
85
 
86
  # Submit generation request
87
  try:
 
108
  yield f"✅ **Request submitted successfully!**\n"
109
  yield f"**Task ID:** `{task_id}`\n\n"
110
  yield f"⏳ Song generation started...\n\n"
111
+ yield "**What's happening now:**\n"
112
+ yield "1. Suno AI is generating your song (1-3 minutes)\n"
113
+ yield "2. We'll poll the API every 10 seconds for updates\n"
114
+ yield "3. When complete, you'll get direct links to your song\n"
115
+ yield "4. Suno will also send a callback to our server\n\n"
116
+ yield "**Keep this window open while we monitor progress...**\n"
117
 
118
  except Exception as e:
119
  yield f"❌ Error submitting request: {str(e)}"
 
163
  yield "2. Log in to your account\n"
164
  yield "3. Go to 'Generation History'\n"
165
  yield "4. Find your song by Task ID\n"
166
+ yield f"\n**Callback URL used:** {request_data['callBackUrl']}\n"
167
  return
168
 
169
  # Look for songs in different possible locations
 
174
  songs = response_data.get("data", [])
175
  if not songs:
176
  songs = response_data.get("songs", [])
 
 
 
 
 
 
177
 
178
  elif isinstance(response_data, list):
179
  songs = response_data
 
216
  yield "- Download links may take 2-3 minutes\n"
217
  yield "- Files are kept for 15 days\n"
218
  yield f"- Task ID: `{task_id}` (save this for reference)\n"
219
+ yield f"- Callback URL: {request_data['callBackUrl']}\n"
220
  return
221
  else:
222
  # No songs found in response
 
229
  yield "2. Log in to your account\n"
230
  yield "3. Check 'Generation History'\n"
231
  yield "4. Look for this Task ID\n"
232
+ yield f"\n**Callback URL used:** {request_data['callBackUrl']}\n"
233
  return
234
 
235
  elif current_status == "FAILED":
 
238
  yield f"**Status:** {current_status}\n"
239
  yield f"**Error:** {error_msg}\n"
240
  yield f"**Task ID:** `{task_id}`\n\n"
241
+ yield f"**Callback URL:** {request_data['callBackUrl']}\n"
242
+ yield "\nPlease try again with different parameters."
243
  return
244
 
245
  elif current_status in ["PENDING", "PROCESSING"]:
246
  yield f"⏳ **Status Update**\n\n"
247
  yield f"**Current Status:** {current_status}\n"
248
  yield f"**Task ID:** `{task_id}`\n"
249
+ yield f"**Check:** {attempt + 1}/{max_attempts}\n"
250
+ yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"
251
  yield "**Estimated time remaining:**\n"
252
  yield "- Stream URL: 30-60 seconds\n"
253
  yield "- Download URL: 2-3 minutes\n"
 
256
  else:
257
  yield f"📊 **Status:** {current_status}\n"
258
  yield f"**Task ID:** `{task_id}`\n"
259
+ yield f"**Check:** {attempt + 1}/{max_attempts}\n"
260
+ yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"
261
  yield "Still processing...\n"
262
 
263
  else:
 
266
  yield f"⚠️ **API Error**\n\n"
267
  yield f"**Code:** {check_data.get('code')}\n"
268
  yield f"**Message:** {error_msg}\n"
269
+ yield f"**Task ID:** `{task_id}`\n"
270
+ yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"
271
  yield "Will continue checking...\n"
272
 
273
  else:
274
  yield f"⚠️ **HTTP Error {check_resp.status_code}**\n\n"
275
  yield f"Failed to check status. Will retry in 10 seconds...\n"
276
  yield f"Check: {attempt + 1}/{max_attempts}\n"
277
+ yield f"Task ID: `{task_id}`\n"
278
 
279
  except requests.exceptions.Timeout:
280
  yield f"⏱️ **Timeout checking status**\n\n"
281
  yield "The status check timed out. Will try again in 10 seconds...\n"
282
  yield f"Check: {attempt + 1}/{max_attempts}\n"
283
+ yield f"Task ID: `{task_id}`\n"
284
 
285
  except Exception as e:
286
  yield f"⚠️ **Error checking status:** {str(e)}\n\n"
287
  yield "Will retry in 10 seconds...\n"
288
  yield f"Check: {attempt + 1}/{max_attempts}\n"
289
+ yield f"Task ID: `{task_id}`\n"
290
 
291
  # If we get here, we timed out
292
  yield "⏰ **Timeout after 10 minutes**\n\n"
293
+ yield f"**Task ID:** `{task_id}`\n"
294
+ yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"
295
  yield "**What to do:**\n"
296
  yield "1. The song may still be processing\n"
297
  yield "2. Visit https://sunoapi.org\n"
 
411
  4. Choose model (V4_5ALL recommended)
412
  5. Click Generate Song!
413
 
414
+ **Features:**
415
+ - Real callback URL for notifications
416
+ - Automatic polling for status updates
417
+ - Direct streaming and download links
418
+ - File upload/download support
 
419
 
420
  **Generation time:**
421
  - 30-60s: Stream URL ready
 
483
  if __name__ == "__main__":
484
  print("🚀 Starting Suno Song Generator")
485
  print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")
486
+ print(f"🌐 Space URL: {SPACE_URL}")
487
+ print(f"📞 Callback URL: {SPACE_URL}/callback")
488
  app.launch(server_name="0.0.0.0", server_port=7860, share=False)