Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -97,7 +97,7 @@ def submit_song_generation(lyrics_text, style, title, instrumental, model):
|
|
| 97 |
except Exception as e:
|
| 98 |
return f"❌ Error: {str(e)}", ""
|
| 99 |
|
| 100 |
-
def check_song_status(task_id
|
| 101 |
"""Check song generation status - RETURNS DOWNLOAD LINKS"""
|
| 102 |
if not task_id:
|
| 103 |
return "❌ Please enter a Task ID", ""
|
|
@@ -115,7 +115,7 @@ def check_song_status(task_id, show_raw=False):
|
|
| 115 |
|
| 116 |
data = resp.json()
|
| 117 |
|
| 118 |
-
# RAW JSON for debugging
|
| 119 |
raw_json = f"```json\n{json.dumps(data, indent=2)}\n```"
|
| 120 |
|
| 121 |
if data.get("code") != 200:
|
|
@@ -129,7 +129,6 @@ def check_song_status(task_id, show_raw=False):
|
|
| 129 |
output += f"**Status:** {status}\n"
|
| 130 |
|
| 131 |
if status == "TEXT_SUCCESS":
|
| 132 |
-
# This is the key status! Songs are ready!
|
| 133 |
response_data = task_data.get("response", {})
|
| 134 |
songs = response_data.get("sunoData", [])
|
| 135 |
|
|
@@ -146,19 +145,14 @@ def check_song_status(task_id, show_raw=False):
|
|
| 146 |
|
| 147 |
# Get all possible URLs
|
| 148 |
audio_url = song.get('audioUrl')
|
| 149 |
-
stream_url = song.get('streamAudioUrl') or song.get('
|
| 150 |
-
source_stream = song.get('sourceStreamAudioUrl')
|
| 151 |
-
source_audio = song.get('sourceAudioUrl')
|
| 152 |
-
|
| 153 |
-
# Image URL
|
| 154 |
-
image_url = song.get('imageUrl') or song.get('sourceImageUrl')
|
| 155 |
|
| 156 |
# Best URL for listening
|
| 157 |
-
best_url = audio_url or stream_url
|
| 158 |
|
| 159 |
if best_url:
|
| 160 |
output += f"**🎧 Listen Now:** [Click to Play]({best_url})\n"
|
| 161 |
-
# Audio player
|
| 162 |
output += f"""<audio controls style="width: 100%; margin: 10px 0;">
|
| 163 |
<source src="{best_url}" type="audio/mpeg">
|
| 164 |
Your browser does not support audio.
|
|
@@ -170,33 +164,17 @@ def check_song_status(task_id, show_raw=False):
|
|
| 170 |
elif stream_url:
|
| 171 |
output += f"**💾 Download:** [Stream MP3]({stream_url})\n"
|
| 172 |
|
| 173 |
-
if image_url:
|
| 174 |
-
output += f"**🖼️ Cover Art:** [View Image]({image_url})\n"
|
| 175 |
-
|
| 176 |
output += f"**ID:** `{song.get('id', 'N/A')}`\n"
|
| 177 |
output += f"**Model:** {song.get('modelName', 'N/A')}\n"
|
| 178 |
output += f"**Tags:** {song.get('tags', 'N/A')}\n"
|
| 179 |
-
if song.get('duration'):
|
| 180 |
-
output += f"**Duration:** {song.get('duration')}s\n"
|
| 181 |
-
|
| 182 |
-
# Show first 200 chars of prompt
|
| 183 |
-
prompt = song.get('prompt', '')
|
| 184 |
-
if prompt:
|
| 185 |
-
output += f"**Preview:** {prompt[:200]}...\n"
|
| 186 |
|
| 187 |
output += "\n---\n\n"
|
| 188 |
|
| 189 |
-
# Summary
|
| 190 |
output += f"**✅ Generation complete!**\n\n"
|
| 191 |
-
output += "**To save your songs:**\n"
|
| 192 |
-
output += "1. Right-click 'Listen Now' links → 'Save link as...'\n"
|
| 193 |
-
output += "2. Or use the download links above\n"
|
| 194 |
-
output += "3. Note the Track IDs for reference\n"
|
| 195 |
|
| 196 |
elif status in ["PENDING", "PROCESSING", "RUNNING"]:
|
| 197 |
output += f"\n**⏳ Song is still being generated...**\n"
|
| 198 |
output += "Check again in 30-60 seconds\n"
|
| 199 |
-
output += f"This usually takes 1-3 minutes total\n"
|
| 200 |
|
| 201 |
elif status == "SUCCESS":
|
| 202 |
output += f"\n**✅ Generation succeeded!**\n"
|
|
@@ -210,10 +188,7 @@ def check_song_status(task_id, show_raw=False):
|
|
| 210 |
output += f"\n**⚠️ Unknown status:** {status}\n"
|
| 211 |
output += "Check raw data below\n"
|
| 212 |
|
| 213 |
-
|
| 214 |
-
output += f"\n**💡 Tip:** Songs may take 2-3 minutes to fully process\n"
|
| 215 |
-
output += f"**📞 Callback sent to:** https://1hit.no/callback.php\n"
|
| 216 |
-
output += f"**🔗 Viewer:** [https://1hit.no/viewer.php?task_id={task_id}](https://1hit.no/viewer.php?task_id={task_id})\n"
|
| 217 |
|
| 218 |
return output, raw_json
|
| 219 |
|
|
@@ -327,46 +302,21 @@ with gr.Blocks() as app:
|
|
| 327 |
- Default: "Gnomes remember" track
|
| 328 |
- Volume control
|
| 329 |
- Play/pause/seek
|
| 330 |
-
- Works with your generated songs!
|
| 331 |
""")
|
| 332 |
|
| 333 |
with gr.Column(scale=2):
|
| 334 |
-
# Audio player
|
| 335 |
-
gr.
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
| 343 |
</div>"""
|
| 344 |
)
|
| 345 |
-
|
| 346 |
-
# Quick player controls
|
| 347 |
-
with gr.Row():
|
| 348 |
-
volume_slider = gr.Slider(
|
| 349 |
-
minimum=0,
|
| 350 |
-
maximum=100,
|
| 351 |
-
value=80,
|
| 352 |
-
label="Volume",
|
| 353 |
-
interactive=True
|
| 354 |
-
)
|
| 355 |
-
|
| 356 |
-
# Quick links to generated songs
|
| 357 |
-
gr.Markdown("### 🎵 Quick Play")
|
| 358 |
-
gr.Markdown("Paste URLs from generated songs:")
|
| 359 |
-
|
| 360 |
-
quick_url_1 = gr.Textbox(
|
| 361 |
-
label="Song URL 1",
|
| 362 |
-
placeholder="Paste audio URL here",
|
| 363 |
-
lines=1
|
| 364 |
-
)
|
| 365 |
-
quick_url_2 = gr.Textbox(
|
| 366 |
-
label="Song URL 2",
|
| 367 |
-
placeholder="Paste another URL here",
|
| 368 |
-
lines=1
|
| 369 |
-
)
|
| 370 |
|
| 371 |
with gr.Tab("📋 Instructions"):
|
| 372 |
gr.Markdown("""
|
|
@@ -384,81 +334,19 @@ with gr.Blocks() as app:
|
|
| 384 |
- Click **Check Status Now**
|
| 385 |
- Wait for **TEXT_SUCCESS** status
|
| 386 |
|
| 387 |
-
### 3. Play
|
| 388 |
-
-
|
| 389 |
-
-
|
| 390 |
-
-
|
| 391 |
-
-
|
| 392 |
-
- Quick URLs
|
| 393 |
|
| 394 |
### 4. Player Features
|
| 395 |
- **Default track**: Gnomes remember.mp3
|
| 396 |
- **Any MP3 URL**: Paste and play
|
| 397 |
- **From Suno**: Use streamAudioUrl
|
| 398 |
-
- **Volume control**:
|
| 399 |
-
|
| 400 |
-
### 5. Status Meanings
|
| 401 |
-
- **TEXT_SUCCESS**: ✅ Songs ready!
|
| 402 |
-
- **PENDING/PROCESSING**: ⏳ Still working
|
| 403 |
-
- **SUCCESS**: Check raw data
|
| 404 |
-
- **FAILED**: Try again
|
| 405 |
""")
|
| 406 |
|
| 407 |
-
# JavaScript for music player
|
| 408 |
-
app.load(
|
| 409 |
-
None,
|
| 410 |
-
None,
|
| 411 |
-
None,
|
| 412 |
-
js="""
|
| 413 |
-
function updateAudioPlayer(url, title) {
|
| 414 |
-
const player = document.getElementById('musicPlayer');
|
| 415 |
-
if (player) {
|
| 416 |
-
// Update audio source
|
| 417 |
-
const source = player.querySelector('source');
|
| 418 |
-
if (source) {
|
| 419 |
-
source.src = url;
|
| 420 |
-
}
|
| 421 |
-
|
| 422 |
-
// Update player src
|
| 423 |
-
player.src = url;
|
| 424 |
-
|
| 425 |
-
// Update label
|
| 426 |
-
const label = document.querySelector('#musicPlayer + div');
|
| 427 |
-
if (label) {
|
| 428 |
-
label.innerHTML = '<strong>Now Playing:</strong> ' + (title || url);
|
| 429 |
-
}
|
| 430 |
-
|
| 431 |
-
// Load new audio
|
| 432 |
-
player.load();
|
| 433 |
-
|
| 434 |
-
// Try to play
|
| 435 |
-
player.play().catch(e => console.log("Auto-play prevented:", e));
|
| 436 |
-
}
|
| 437 |
-
}
|
| 438 |
-
|
| 439 |
-
// Volume control
|
| 440 |
-
function updateVolume(volume) {
|
| 441 |
-
const player = document.getElementById('musicPlayer');
|
| 442 |
-
if (player) {
|
| 443 |
-
player.volume = volume / 100;
|
| 444 |
-
}
|
| 445 |
-
}
|
| 446 |
-
|
| 447 |
-
// Quick play functions
|
| 448 |
-
function playQuickUrl1(url) {
|
| 449 |
-
if (url && url.includes('://')) {
|
| 450 |
-
updateAudioPlayer(url, 'Quick Song 1');
|
| 451 |
-
}
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
-
function playQuickUrl2(url) {
|
| 455 |
-
if (url && url.includes('://')) {
|
| 456 |
-
updateAudioPlayer(url, 'Quick Song 2');
|
| 457 |
-
}
|
| 458 |
-
}
|
| 459 |
-
"""
|
| 460 |
-
)
|
| 461 |
-
|
| 462 |
# Auto-fill status tab with task ID
|
| 463 |
def update_status_field(task_id):
|
| 464 |
return task_id
|
|
@@ -473,9 +361,70 @@ with gr.Blocks() as app:
|
|
| 473 |
def on_generate(lyrics, style, title, instrumental, model):
|
| 474 |
output, task_id = submit_song_generation(lyrics, style, title, instrumental, model)
|
| 475 |
if task_id:
|
| 476 |
-
return output, task_id, task_id
|
| 477 |
-
return output, "", ""
|
| 478 |
|
| 479 |
submit_btn.click(
|
| 480 |
fn=on_generate,
|
| 481 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
return f"❌ Error: {str(e)}", ""
|
| 99 |
|
| 100 |
+
def check_song_status(task_id):
|
| 101 |
"""Check song generation status - RETURNS DOWNLOAD LINKS"""
|
| 102 |
if not task_id:
|
| 103 |
return "❌ Please enter a Task ID", ""
|
|
|
|
| 115 |
|
| 116 |
data = resp.json()
|
| 117 |
|
| 118 |
+
# RAW JSON for debugging
|
| 119 |
raw_json = f"```json\n{json.dumps(data, indent=2)}\n```"
|
| 120 |
|
| 121 |
if data.get("code") != 200:
|
|
|
|
| 129 |
output += f"**Status:** {status}\n"
|
| 130 |
|
| 131 |
if status == "TEXT_SUCCESS":
|
|
|
|
| 132 |
response_data = task_data.get("response", {})
|
| 133 |
songs = response_data.get("sunoData", [])
|
| 134 |
|
|
|
|
| 145 |
|
| 146 |
# Get all possible URLs
|
| 147 |
audio_url = song.get('audioUrl')
|
| 148 |
+
stream_url = song.get('streamAudioUrl') or song.get('stream_url')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# Best URL for listening
|
| 151 |
+
best_url = audio_url or stream_url
|
| 152 |
|
| 153 |
if best_url:
|
| 154 |
output += f"**🎧 Listen Now:** [Click to Play]({best_url})\n"
|
| 155 |
+
# Audio player for HTML
|
| 156 |
output += f"""<audio controls style="width: 100%; margin: 10px 0;">
|
| 157 |
<source src="{best_url}" type="audio/mpeg">
|
| 158 |
Your browser does not support audio.
|
|
|
|
| 164 |
elif stream_url:
|
| 165 |
output += f"**💾 Download:** [Stream MP3]({stream_url})\n"
|
| 166 |
|
|
|
|
|
|
|
|
|
|
| 167 |
output += f"**ID:** `{song.get('id', 'N/A')}`\n"
|
| 168 |
output += f"**Model:** {song.get('modelName', 'N/A')}\n"
|
| 169 |
output += f"**Tags:** {song.get('tags', 'N/A')}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
output += "\n---\n\n"
|
| 172 |
|
|
|
|
| 173 |
output += f"**✅ Generation complete!**\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
elif status in ["PENDING", "PROCESSING", "RUNNING"]:
|
| 176 |
output += f"\n**⏳ Song is still being generated...**\n"
|
| 177 |
output += "Check again in 30-60 seconds\n"
|
|
|
|
| 178 |
|
| 179 |
elif status == "SUCCESS":
|
| 180 |
output += f"\n**✅ Generation succeeded!**\n"
|
|
|
|
| 188 |
output += f"\n**⚠️ Unknown status:** {status}\n"
|
| 189 |
output += "Check raw data below\n"
|
| 190 |
|
| 191 |
+
output += f"\n**🔗 Viewer:** [https://1hit.no/viewer.php?task_id={task_id}](https://1hit.no/viewer.php?task_id={task_id})\n"
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
return output, raw_json
|
| 194 |
|
|
|
|
| 302 |
- Default: "Gnomes remember" track
|
| 303 |
- Volume control
|
| 304 |
- Play/pause/seek
|
|
|
|
| 305 |
""")
|
| 306 |
|
| 307 |
with gr.Column(scale=2):
|
| 308 |
+
# Audio player HTML
|
| 309 |
+
audio_display = gr.HTML(
|
| 310 |
+
value="""<div>
|
| 311 |
+
<audio id="mainPlayer" controls style="width: 100%; height: 60px; margin: 20px 0;">
|
| 312 |
+
<source src="https://huggingface.co/spaces/MySafeCode/SUNO-API-V5/resolve/main/Gnomes%20remember.mp3" type="audio/mpeg">
|
| 313 |
+
Your browser does not support audio.
|
| 314 |
+
</audio>
|
| 315 |
+
<div style="margin: 10px 0; padding: 10px; background: #f5f5f5; border-radius: 5px;">
|
| 316 |
+
<strong>Now Playing:</strong> Gnomes remember.mp3
|
| 317 |
+
</div>
|
| 318 |
</div>"""
|
| 319 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
with gr.Tab("📋 Instructions"):
|
| 322 |
gr.Markdown("""
|
|
|
|
| 334 |
- Click **Check Status Now**
|
| 335 |
- Wait for **TEXT_SUCCESS** status
|
| 336 |
|
| 337 |
+
### 3. Play Music
|
| 338 |
+
- Use **Music Player** tab:
|
| 339 |
+
- Default track plays automatically
|
| 340 |
+
- Paste any MP3 URL to play
|
| 341 |
+
- Works with your generated songs!
|
|
|
|
| 342 |
|
| 343 |
### 4. Player Features
|
| 344 |
- **Default track**: Gnomes remember.mp3
|
| 345 |
- **Any MP3 URL**: Paste and play
|
| 346 |
- **From Suno**: Use streamAudioUrl
|
| 347 |
+
- **Volume control**: Use player controls
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
""")
|
| 349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
# Auto-fill status tab with task ID
|
| 351 |
def update_status_field(task_id):
|
| 352 |
return task_id
|
|
|
|
| 361 |
def on_generate(lyrics, style, title, instrumental, model):
|
| 362 |
output, task_id = submit_song_generation(lyrics, style, title, instrumental, model)
|
| 363 |
if task_id:
|
| 364 |
+
return output, task_id, task_id
|
| 365 |
+
return output, "", ""
|
| 366 |
|
| 367 |
submit_btn.click(
|
| 368 |
fn=on_generate,
|
| 369 |
+
inputs=[lyrics_text, style, title, instrumental, model],
|
| 370 |
+
outputs=[submission_output, current_task_id, status_task_id]
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
# Check status
|
| 374 |
+
def on_check(task_id):
|
| 375 |
+
output, raw = check_song_status(task_id)
|
| 376 |
+
return output, raw
|
| 377 |
+
|
| 378 |
+
check_btn.click(
|
| 379 |
+
fn=on_check,
|
| 380 |
+
inputs=[status_task_id],
|
| 381 |
+
outputs=[status_output, raw_output]
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
# Music player controls
|
| 385 |
+
def play_music(url):
|
| 386 |
+
if not url.strip():
|
| 387 |
+
url = "https://huggingface.co/spaces/MySafeCode/SUNO-API-V5/resolve/main/Gnomes%20remember.mp3"
|
| 388 |
+
|
| 389 |
+
# Create HTML with the new URL
|
| 390 |
+
html = f"""<div>
|
| 391 |
+
<audio id="mainPlayer" controls style="width: 100%; height: 60px; margin: 20px 0;">
|
| 392 |
+
<source src="{url}" type="audio/mpeg">
|
| 393 |
+
Your browser does not support audio.
|
| 394 |
+
</audio>
|
| 395 |
+
<div style="margin: 10px 0; padding: 10px; background: #f5f5f5; border-radius: 5px;">
|
| 396 |
+
<strong>Now Playing:</strong> {url.split('/')[-1].replace('%20', ' ')}
|
| 397 |
+
</div>
|
| 398 |
+
</div>"""
|
| 399 |
+
return html
|
| 400 |
+
|
| 401 |
+
play_btn.click(
|
| 402 |
+
fn=play_music,
|
| 403 |
+
inputs=[music_url],
|
| 404 |
+
outputs=[audio_display]
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
+
def reset_music():
|
| 408 |
+
url = "https://huggingface.co/spaces/MySafeCode/SUNO-API-V5/resolve/main/Gnomes%20remember.mp3"
|
| 409 |
+
html = f"""<div>
|
| 410 |
+
<audio id="mainPlayer" controls style="width: 100%; height: 60px; margin: 20px 0;">
|
| 411 |
+
<source src="{url}" type="audio/mpeg">
|
| 412 |
+
Your browser does not support audio.
|
| 413 |
+
</audio>
|
| 414 |
+
<div style="margin: 10px 0; padding: 10px; background: #f5f5f5; border-radius: 5px;">
|
| 415 |
+
<strong>Now Playing:</strong> Gnomes remember.mp3
|
| 416 |
+
</div>
|
| 417 |
+
</div>"""
|
| 418 |
+
return html, url
|
| 419 |
+
|
| 420 |
+
reset_btn.click(
|
| 421 |
+
fn=reset_music,
|
| 422 |
+
outputs=[audio_display, music_url]
|
| 423 |
+
)
|
| 424 |
+
|
| 425 |
+
# Launch the app
|
| 426 |
+
if __name__ == "__main__":
|
| 427 |
+
print("🚀 Starting Suno Song Generator")
|
| 428 |
+
print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")
|
| 429 |
+
print("🌐 Open your browser to: http://localhost:7860")
|
| 430 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|