Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,121 @@ import requests
|
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
import json
|
| 6 |
-
import tempfile
|
| 7 |
|
| 8 |
# Suno API key
|
| 9 |
SUNO_KEY = os.environ.get("SunoKey", "")
|
| 10 |
if not SUNO_KEY:
|
| 11 |
print("⚠️ SunoKey not set!")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def generate_song_from_text(lyrics_text, style, title, instrumental, model):
|
| 17 |
"""Generate a song from lyrics text"""
|
|
@@ -19,8 +125,8 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
|
|
| 19 |
yield "❌ Error: SunoKey not configured in environment variables"
|
| 20 |
return
|
| 21 |
|
| 22 |
-
if not lyrics_text.strip():
|
| 23 |
-
yield "❌ Error: Please provide lyrics"
|
| 24 |
return
|
| 25 |
|
| 26 |
if not style.strip():
|
|
@@ -32,12 +138,12 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
|
|
| 32 |
return
|
| 33 |
|
| 34 |
try:
|
| 35 |
-
#
|
| 36 |
request_data = {
|
| 37 |
"customMode": True,
|
| 38 |
"instrumental": instrumental,
|
| 39 |
"model": model,
|
| 40 |
-
"callBackUrl": "https://1hit.no/callback.php",
|
| 41 |
"style": style,
|
| 42 |
"title": title,
|
| 43 |
}
|
|
@@ -74,14 +180,14 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
|
|
| 74 |
request_data["style"] = style
|
| 75 |
request_data["title"] = title
|
| 76 |
|
| 77 |
-
yield f"
|
| 78 |
yield f"**Title:** {title}\n"
|
| 79 |
yield f"**Style:** {style}\n"
|
| 80 |
yield f"**Model:** {model}\n"
|
| 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:**
|
| 85 |
|
| 86 |
# Submit generation request
|
| 87 |
try:
|
|
@@ -97,341 +203,207 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
|
|
| 97 |
|
| 98 |
if resp.status_code != 200:
|
| 99 |
yield f"❌ Submission failed: HTTP {resp.status_code}"
|
|
|
|
| 100 |
return
|
| 101 |
|
| 102 |
data = resp.json()
|
|
|
|
|
|
|
| 103 |
if data.get("code") != 200:
|
| 104 |
yield f"❌ API error: {data.get('msg', 'Unknown')}"
|
| 105 |
return
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 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 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
# Poll for completion - with better error handling
|
| 123 |
-
max_attempts = 60 # 60 attempts * 10 seconds = 10 minutes
|
| 124 |
-
last_status = ""
|
| 125 |
-
|
| 126 |
-
for attempt in range(max_attempts):
|
| 127 |
-
time.sleep(10)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
if current_status == "COMPLETE":
|
| 149 |
-
# Try to parse the response
|
| 150 |
-
response_data = data_info.get("response", {})
|
| 151 |
-
|
| 152 |
-
# Response might be a JSON string
|
| 153 |
-
if isinstance(response_data, str):
|
| 154 |
-
try:
|
| 155 |
-
response_data = json.loads(response_data)
|
| 156 |
-
except json.JSONDecodeError:
|
| 157 |
-
# If it's not JSON, it might be a simple string
|
| 158 |
-
yield f"✅ **Generation Complete!**\n\n"
|
| 159 |
-
yield f"**Status:** {current_status}\n"
|
| 160 |
-
yield f"**Task ID:** `{task_id}`\n\n"
|
| 161 |
-
yield "**To access your song:**\n"
|
| 162 |
-
yield "1. Visit https://sunoapi.org\n"
|
| 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
|
| 170 |
-
songs = []
|
| 171 |
-
|
| 172 |
-
# Try different possible structures
|
| 173 |
-
if isinstance(response_data, dict):
|
| 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
|
| 180 |
-
|
| 181 |
-
if songs:
|
| 182 |
-
yield "🎶 **SONG GENERATION COMPLETE!** 🎶\n\n"
|
| 183 |
-
yield f"Generated {len(songs)} song(s)\n\n"
|
| 184 |
-
|
| 185 |
-
for i, song in enumerate(songs, 1):
|
| 186 |
-
if isinstance(song, dict):
|
| 187 |
-
song_title = song.get('title', f'Song {i}')
|
| 188 |
-
stream_url = song.get('streamUrl') or song.get('stream_url')
|
| 189 |
-
download_url = song.get('downloadUrl') or song.get('download_url')
|
| 190 |
-
|
| 191 |
-
yield f"## 🎵 Song {i}: {song_title}\n"
|
| 192 |
-
|
| 193 |
-
if stream_url:
|
| 194 |
-
yield f"**Stream URL:** {stream_url}\n"
|
| 195 |
-
yield f"**Listen Now:** [Click to Stream]({stream_url})\n\n"
|
| 196 |
-
|
| 197 |
-
# Audio player
|
| 198 |
-
yield f"""<audio controls style="width: 100%; margin: 10px 0; padding: 10px; background: #f0f0f0; border-radius: 5px;">
|
| 199 |
-
<source src="{stream_url}" type="audio/mpeg">
|
| 200 |
-
Your browser does not support audio playback.
|
| 201 |
-
</audio>\n\n"""
|
| 202 |
-
else:
|
| 203 |
-
yield "⏳ Stream URL not ready yet (check back in 30 seconds)\n\n"
|
| 204 |
-
|
| 205 |
-
if download_url:
|
| 206 |
-
yield f"**Download URL:** {download_url}\n"
|
| 207 |
-
yield f"**Download:** [Click to Download]({download_url})\n\n"
|
| 208 |
-
else:
|
| 209 |
-
yield "⏳ Download URL not ready yet (usually takes 2-3 minutes)\n\n"
|
| 210 |
-
|
| 211 |
-
yield "---\n\n"
|
| 212 |
-
|
| 213 |
-
yield f"⏱️ Total generation time: {(attempt + 1) * 10} seconds\n\n"
|
| 214 |
-
yield "**Important:**\n"
|
| 215 |
-
yield "- Stream links work immediately\n"
|
| 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
|
| 223 |
-
yield f"✅ **Generation Complete!**\n\n"
|
| 224 |
-
yield f"**Status:** {current_status}\n"
|
| 225 |
-
yield f"**Task ID:** `{task_id}`\n\n"
|
| 226 |
-
yield "**Response received but no song data found.**\n\n"
|
| 227 |
-
yield "**To access your song:**\n"
|
| 228 |
-
yield "1. Visit https://sunoapi.org\n"
|
| 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":
|
| 236 |
-
error_msg = data_info.get("errorMessage", "Unknown error")
|
| 237 |
-
yield f"❌ **Generation Failed**\n\n"
|
| 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"
|
| 254 |
-
yield "\nWe'll check again in 10 seconds...\n"
|
| 255 |
-
|
| 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:
|
| 264 |
-
# API returned error code
|
| 265 |
-
error_msg = check_data.get("msg", "Unknown error")
|
| 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 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
yield f"Check: {attempt + 1}/{max_attempts}\n"
|
| 283 |
-
yield f"Task ID: `{task_id}`\n"
|
| 284 |
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 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"
|
| 298 |
-
yield "3. Log in and check 'Generation History'\n"
|
| 299 |
-
yield "4. Look for this Task ID\n"
|
| 300 |
-
yield "\nSongs can sometimes take longer than expected, especially for longer tracks."
|
| 301 |
|
| 302 |
except Exception as e:
|
| 303 |
yield f"❌ **Unexpected Error:** {str(e)}"
|
| 304 |
|
| 305 |
-
def download_text_file(text):
|
| 306 |
-
"""Create downloadable text file"""
|
| 307 |
-
if not text.strip():
|
| 308 |
-
return None
|
| 309 |
-
|
| 310 |
-
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False, encoding='utf-8') as f:
|
| 311 |
-
f.write(text)
|
| 312 |
-
temp_path = f.name
|
| 313 |
-
|
| 314 |
-
return temp_path
|
| 315 |
-
|
| 316 |
-
def upload_text_file(file):
|
| 317 |
-
"""Read text from uploaded file"""
|
| 318 |
-
if file is None:
|
| 319 |
-
return ""
|
| 320 |
-
|
| 321 |
-
try:
|
| 322 |
-
with open(file.name, 'r', encoding='utf-8') as f:
|
| 323 |
-
content = f.read()
|
| 324 |
-
return content
|
| 325 |
-
except:
|
| 326 |
-
try:
|
| 327 |
-
with open(file.name, 'r', encoding='latin-1') as f:
|
| 328 |
-
content = f.read()
|
| 329 |
-
return content
|
| 330 |
-
except Exception as e:
|
| 331 |
-
return f"❌ Error reading file: {str(e)}"
|
| 332 |
-
|
| 333 |
-
def clear_all():
|
| 334 |
-
"""Clear all input fields"""
|
| 335 |
-
return "", "", "Generated Song", False, "V4_5ALL", "### Ready to generate!\n\n1. Enter lyrics\n2. Set style and title\n3. Click 'Generate Song'"
|
| 336 |
-
|
| 337 |
# Create the app
|
| 338 |
-
with gr.Blocks(
|
| 339 |
gr.Markdown("# 🎵 Suno Song Generator")
|
| 340 |
gr.Markdown("Create songs from lyrics and style using Suno AI")
|
| 341 |
|
| 342 |
-
with gr.
|
| 343 |
-
with gr.
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
lyrics_text = gr.Textbox(
|
| 349 |
label="Lyrics",
|
| 350 |
placeholder="Paste your lyrics here...\n\nExample:\n(Verse 1)\nSun is shining, sky is blue\nBirds are singing, just for you...",
|
| 351 |
-
lines=
|
| 352 |
interactive=True
|
| 353 |
)
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
file_types=[".txt"],
|
| 359 |
-
type="filepath"
|
| 360 |
-
)
|
| 361 |
-
upload_btn = gr.Button("📁 Load from File", variant="secondary")
|
| 362 |
-
|
| 363 |
-
# Song Settings
|
| 364 |
-
gr.Markdown("### Step 2: Song Settings")
|
| 365 |
-
|
| 366 |
-
with gr.Row():
|
| 367 |
style = gr.Textbox(
|
| 368 |
label="Music Style",
|
| 369 |
placeholder="Example: Pop, Rock, Jazz, Classical, Electronic, Hip Hop, Country",
|
| 370 |
value="Pop",
|
| 371 |
-
interactive=True
|
| 372 |
-
scale=2
|
| 373 |
)
|
| 374 |
-
|
| 375 |
-
with gr.Row():
|
| 376 |
title = gr.Textbox(
|
| 377 |
label="Song Title",
|
| 378 |
placeholder="My Awesome Song",
|
| 379 |
value="Generated Song",
|
| 380 |
-
interactive=True
|
| 381 |
-
scale=2
|
| 382 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
-
with gr.
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
value=
|
| 388 |
-
interactive=True
|
| 389 |
)
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
)
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
# Instructions
|
| 406 |
-
gr.Markdown("""
|
| 407 |
-
**How to use:**
|
| 408 |
-
1. Paste lyrics or upload a .txt file
|
| 409 |
-
2. Set music style (genre)
|
| 410 |
-
3. Enter song title
|
| 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
|
| 422 |
-
- 2-3 min: Download URL ready
|
| 423 |
-
- Up to 5 min for longer songs
|
| 424 |
-
""")
|
| 425 |
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
gr.Markdown("---")
|
| 437 |
gr.Markdown(
|
|
@@ -445,44 +417,40 @@ with gr.Blocks(title="Suno Song Generator", theme="soft") as app:
|
|
| 445 |
elem_id="footer"
|
| 446 |
)
|
| 447 |
|
| 448 |
-
# Event handlers
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
upload_btn.click(
|
| 452 |
-
upload_text_file,
|
| 453 |
-
inputs=file_upload,
|
| 454 |
-
outputs=lyrics_text
|
| 455 |
-
).then(
|
| 456 |
-
lambda: "📁 **Lyrics loaded from file!**\n\nReady to generate song.",
|
| 457 |
-
outputs=output
|
| 458 |
-
)
|
| 459 |
-
|
| 460 |
-
# Download lyrics
|
| 461 |
-
download_btn.click(
|
| 462 |
-
download_text_file,
|
| 463 |
-
inputs=lyrics_text,
|
| 464 |
-
outputs=file_output
|
| 465 |
-
).then(
|
| 466 |
-
lambda: "💾 **Lyrics ready for download!**\n\nCheck the download button below.",
|
| 467 |
-
outputs=output
|
| 468 |
-
)
|
| 469 |
|
| 470 |
-
# Clear all
|
| 471 |
clear_btn.click(
|
| 472 |
clear_all,
|
| 473 |
outputs=[lyrics_text, style, title, instrumental, model, output]
|
| 474 |
)
|
| 475 |
|
| 476 |
-
# Generate song
|
| 477 |
generate_btn.click(
|
| 478 |
generate_song_from_text,
|
| 479 |
inputs=[lyrics_text, style, title, instrumental, model],
|
| 480 |
outputs=output
|
| 481 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
|
|
|
|
| 483 |
if __name__ == "__main__":
|
| 484 |
print("🚀 Starting Suno Song Generator")
|
| 485 |
print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")
|
| 486 |
-
print(
|
| 487 |
-
|
| 488 |
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
import json
|
|
|
|
| 6 |
|
| 7 |
# Suno API key
|
| 8 |
SUNO_KEY = os.environ.get("SunoKey", "")
|
| 9 |
if not SUNO_KEY:
|
| 10 |
print("⚠️ SunoKey not set!")
|
| 11 |
|
| 12 |
+
def get_task_info(task_id):
|
| 13 |
+
"""Manually check any Suno task status"""
|
| 14 |
+
if not task_id:
|
| 15 |
+
return "❌ Please enter a Task ID"
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
resp = requests.get(
|
| 19 |
+
"https://api.sunoapi.org/api/v1/generate/record-info",
|
| 20 |
+
headers={"Authorization": f"Bearer {SUNO_KEY}"},
|
| 21 |
+
params={"taskId": task_id},
|
| 22 |
+
timeout=30
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
if resp.status_code != 200:
|
| 26 |
+
return f"❌ HTTP Error {resp.status_code}\n\n{resp.text}"
|
| 27 |
+
|
| 28 |
+
data = resp.json()
|
| 29 |
+
|
| 30 |
+
# Format the response for display
|
| 31 |
+
output = f"## 🔍 Task Status: `{task_id}`\n\n"
|
| 32 |
+
|
| 33 |
+
if data.get("code") == 200:
|
| 34 |
+
task_data = data.get("data", {})
|
| 35 |
+
status = task_data.get("status", "UNKNOWN")
|
| 36 |
+
|
| 37 |
+
output += f"**Status:** {status}\n"
|
| 38 |
+
output += f"**Task ID:** `{task_data.get('taskId', 'N/A')}`\n"
|
| 39 |
+
output += f"**Music ID:** `{task_data.get('musicId', 'N/A')}`\n"
|
| 40 |
+
output += f"**Created:** {task_data.get('createTime', 'N/A')}\n"
|
| 41 |
+
|
| 42 |
+
if status == "SUCCESS":
|
| 43 |
+
response_data = task_data.get("response", {})
|
| 44 |
+
|
| 45 |
+
# Try to parse response (could be string or dict)
|
| 46 |
+
if isinstance(response_data, str):
|
| 47 |
+
try:
|
| 48 |
+
response_data = json.loads(response_data)
|
| 49 |
+
except:
|
| 50 |
+
output += f"\n**Raw Response:**\n```\n{response_data}\n```\n"
|
| 51 |
+
response_data = {}
|
| 52 |
+
|
| 53 |
+
# Check for song data
|
| 54 |
+
songs = []
|
| 55 |
+
if isinstance(response_data, dict):
|
| 56 |
+
songs = response_data.get("sunoData", [])
|
| 57 |
+
if not songs:
|
| 58 |
+
songs = response_data.get("data", [])
|
| 59 |
+
elif isinstance(response_data, list):
|
| 60 |
+
songs = response_data
|
| 61 |
+
|
| 62 |
+
if songs:
|
| 63 |
+
output += f"\n## 🎵 Generated Songs ({len(songs)})\n\n"
|
| 64 |
+
|
| 65 |
+
for i, song in enumerate(songs, 1):
|
| 66 |
+
if isinstance(song, dict):
|
| 67 |
+
output += f"### Song {i}\n"
|
| 68 |
+
output += f"**Title:** {song.get('title', 'Untitled')}\n"
|
| 69 |
+
output += f"**ID:** `{song.get('id', 'N/A')}`\n"
|
| 70 |
+
|
| 71 |
+
# Audio URLs
|
| 72 |
+
audio_url = song.get('audioUrl') or song.get('audio_url')
|
| 73 |
+
stream_url = song.get('streamUrl') or song.get('stream_url')
|
| 74 |
+
download_url = song.get('downloadUrl') or song.get('download_url')
|
| 75 |
+
|
| 76 |
+
if audio_url:
|
| 77 |
+
output += f"**Audio:** [Play]({audio_url}) | [Download]({audio_url})\n"
|
| 78 |
+
elif stream_url:
|
| 79 |
+
output += f"**Stream:** [Play]({stream_url})\n"
|
| 80 |
+
|
| 81 |
+
if download_url:
|
| 82 |
+
output += f"**Download:** [MP3]({download_url})\n"
|
| 83 |
+
|
| 84 |
+
# Audio player
|
| 85 |
+
play_url = audio_url or stream_url
|
| 86 |
+
if play_url:
|
| 87 |
+
output += f"""\n<audio controls style="width: 100%; margin: 10px 0;">
|
| 88 |
+
<source src="{play_url}" type="audio/mpeg">
|
| 89 |
+
Your browser does not support audio.
|
| 90 |
+
</audio>\n"""
|
| 91 |
+
|
| 92 |
+
output += f"**Prompt:** {song.get('prompt', 'N/A')[:100]}...\n"
|
| 93 |
+
output += f"**Duration:** {song.get('duration', 'N/A')}s\n"
|
| 94 |
+
output += f"**Created:** {song.get('createTime', 'N/A')}\n\n"
|
| 95 |
+
output += "---\n\n"
|
| 96 |
+
else:
|
| 97 |
+
output += "\n**No song data found in response.**\n"
|
| 98 |
+
|
| 99 |
+
elif status == "FAILED":
|
| 100 |
+
error_msg = task_data.get("errorMessage", "Unknown error")
|
| 101 |
+
output += f"\n**Error:** {error_msg}\n"
|
| 102 |
+
|
| 103 |
+
elif status in ["PENDING", "PROCESSING", "RUNNING"]:
|
| 104 |
+
output += f"\n**Task is still processing...**\n"
|
| 105 |
+
output += f"Check again in 30 seconds.\n"
|
| 106 |
+
|
| 107 |
+
else:
|
| 108 |
+
output += f"\n**Unknown status:** {status}\n"
|
| 109 |
+
|
| 110 |
+
else:
|
| 111 |
+
output += f"**API Error:** {data.get('msg', 'Unknown')}\n"
|
| 112 |
+
|
| 113 |
+
# Show raw JSON for debugging
|
| 114 |
+
output += "\n## 📋 Raw Response\n"
|
| 115 |
+
output += f"```json\n{json.dumps(data, indent=2)}\n```"
|
| 116 |
+
|
| 117 |
+
return output
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return f"❌ Error checking task: {str(e)}"
|
| 121 |
|
| 122 |
def generate_song_from_text(lyrics_text, style, title, instrumental, model):
|
| 123 |
"""Generate a song from lyrics text"""
|
|
|
|
| 125 |
yield "❌ Error: SunoKey not configured in environment variables"
|
| 126 |
return
|
| 127 |
|
| 128 |
+
if not lyrics_text.strip() and not instrumental:
|
| 129 |
+
yield "❌ Error: Please provide lyrics or select instrumental"
|
| 130 |
return
|
| 131 |
|
| 132 |
if not style.strip():
|
|
|
|
| 138 |
return
|
| 139 |
|
| 140 |
try:
|
| 141 |
+
# Prepare request data
|
| 142 |
request_data = {
|
| 143 |
"customMode": True,
|
| 144 |
"instrumental": instrumental,
|
| 145 |
"model": model,
|
| 146 |
+
"callBackUrl": "https://1hit.no/callback.php",
|
| 147 |
"style": style,
|
| 148 |
"title": title,
|
| 149 |
}
|
|
|
|
| 180 |
request_data["style"] = style
|
| 181 |
request_data["title"] = title
|
| 182 |
|
| 183 |
+
yield f"## 🚀 Submitting Song Request\n\n"
|
| 184 |
yield f"**Title:** {title}\n"
|
| 185 |
yield f"**Style:** {style}\n"
|
| 186 |
yield f"**Model:** {model}\n"
|
| 187 |
yield f"**Instrumental:** {'Yes' if instrumental else 'No'}\n"
|
| 188 |
if not instrumental:
|
| 189 |
yield f"**Lyrics length:** {len(lyrics_text)} characters\n\n"
|
| 190 |
+
yield f"**Callback URL:** https://1hit.no/callback.php\n\n"
|
| 191 |
|
| 192 |
# Submit generation request
|
| 193 |
try:
|
|
|
|
| 203 |
|
| 204 |
if resp.status_code != 200:
|
| 205 |
yield f"❌ Submission failed: HTTP {resp.status_code}"
|
| 206 |
+
yield f"\n**Response:**\n```\n{resp.text}\n```"
|
| 207 |
return
|
| 208 |
|
| 209 |
data = resp.json()
|
| 210 |
+
print(f"Submission response: {json.dumps(data, indent=2)}")
|
| 211 |
+
|
| 212 |
if data.get("code") != 200:
|
| 213 |
yield f"❌ API error: {data.get('msg', 'Unknown')}"
|
| 214 |
return
|
| 215 |
|
| 216 |
+
# Extract task ID from response
|
| 217 |
+
task_id = None
|
| 218 |
+
if "taskId" in data:
|
| 219 |
+
task_id = data["taskId"]
|
| 220 |
+
elif "data" in data and "taskId" in data["data"]:
|
| 221 |
+
task_id = data["data"]["taskId"]
|
| 222 |
+
elif data.get("data") and "taskId" in data.get("data", {}):
|
| 223 |
+
task_id = data["data"]["taskId"]
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
if not task_id:
|
| 226 |
+
yield f"❌ Could not extract Task ID from response"
|
| 227 |
+
yield f"\n**Raw Response:**\n```json\n{json.dumps(data, indent=2)}\n```"
|
| 228 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
yield f"## ✅ Request Submitted Successfully!\n\n"
|
| 231 |
+
yield f"**🎯 Task ID:** `{task_id}`\n\n"
|
| 232 |
+
yield f"**⏳ Status:** Generation started\n"
|
| 233 |
+
yield f"**📞 Callback:** https://1hit.no/callback.php\n\n"
|
| 234 |
+
yield "**What happens now:**\n"
|
| 235 |
+
yield "1. Suno AI generates your song (1-3 minutes)\n"
|
| 236 |
+
yield "2. You'll get a callback notification\n"
|
| 237 |
+
yield "3. Use the Task ID above to check status manually\n\n"
|
| 238 |
+
yield "---\n\n"
|
| 239 |
+
yield f"## 🔍 Check Status Manually\n\n"
|
| 240 |
+
yield f"Use this Task ID: `{task_id}`\n\n"
|
| 241 |
+
yield "**To check status:**\n"
|
| 242 |
+
yield "1. Copy the Task ID above\n"
|
| 243 |
+
yield "2. Go to 'Check Any Task' tab\n"
|
| 244 |
+
yield "3. Paste and click 'Check Status'\n"
|
| 245 |
+
yield "4. Or wait for callback notification\n\n"
|
| 246 |
+
yield "**Generation time:**\n"
|
| 247 |
+
yield "- 30-60 seconds for stream URL\n"
|
| 248 |
+
yield "- 2-3 minutes for download URL\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
+
# Simple one-time check after 30 seconds
|
| 251 |
+
yield "\n**⏰ Will check once in 30 seconds...**\n"
|
| 252 |
+
time.sleep(30)
|
|
|
|
|
|
|
| 253 |
|
| 254 |
+
# Single status check
|
| 255 |
+
status_result = get_task_info(task_id)
|
| 256 |
+
yield "\n## 📊 Status Check (30s)\n\n"
|
| 257 |
+
yield status_result
|
| 258 |
+
|
| 259 |
+
except Exception as e:
|
| 260 |
+
yield f"❌ Error submitting request: {str(e)}"
|
| 261 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
except Exception as e:
|
| 264 |
yield f"❌ **Unexpected Error:** {str(e)}"
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
# Create the app
|
| 267 |
+
with gr.Blocks() as app:
|
| 268 |
gr.Markdown("# 🎵 Suno Song Generator")
|
| 269 |
gr.Markdown("Create songs from lyrics and style using Suno AI")
|
| 270 |
|
| 271 |
+
with gr.Tab("🎶 Generate Song"):
|
| 272 |
+
with gr.Row():
|
| 273 |
+
with gr.Column(scale=1):
|
| 274 |
+
# Lyrics Input
|
| 275 |
+
gr.Markdown("### Step 1: Enter Lyrics")
|
| 276 |
+
|
| 277 |
lyrics_text = gr.Textbox(
|
| 278 |
label="Lyrics",
|
| 279 |
placeholder="Paste your lyrics here...\n\nExample:\n(Verse 1)\nSun is shining, sky is blue\nBirds are singing, just for you...",
|
| 280 |
+
lines=10,
|
| 281 |
interactive=True
|
| 282 |
)
|
| 283 |
+
|
| 284 |
+
# Song Settings
|
| 285 |
+
gr.Markdown("### Step 2: Song Settings")
|
| 286 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
style = gr.Textbox(
|
| 288 |
label="Music Style",
|
| 289 |
placeholder="Example: Pop, Rock, Jazz, Classical, Electronic, Hip Hop, Country",
|
| 290 |
value="Pop",
|
| 291 |
+
interactive=True
|
|
|
|
| 292 |
)
|
| 293 |
+
|
|
|
|
| 294 |
title = gr.Textbox(
|
| 295 |
label="Song Title",
|
| 296 |
placeholder="My Awesome Song",
|
| 297 |
value="Generated Song",
|
| 298 |
+
interactive=True
|
|
|
|
| 299 |
)
|
| 300 |
+
|
| 301 |
+
with gr.Row():
|
| 302 |
+
instrumental = gr.Checkbox(
|
| 303 |
+
label="Instrumental (No Vocals)",
|
| 304 |
+
value=False,
|
| 305 |
+
interactive=True
|
| 306 |
+
)
|
| 307 |
+
model = gr.Dropdown(
|
| 308 |
+
label="Model",
|
| 309 |
+
choices=["V5", "V4_5PLUS", "V4_5ALL", "V4_5", "V4"],
|
| 310 |
+
value="V4_5ALL",
|
| 311 |
+
interactive=True
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
# Action Buttons
|
| 315 |
+
generate_btn = gr.Button("🚀 Generate Song", variant="primary")
|
| 316 |
+
clear_btn = gr.Button("🗑️ Clear All", variant="secondary")
|
| 317 |
+
|
| 318 |
+
# Instructions
|
| 319 |
+
gr.Markdown("""
|
| 320 |
+
**How to use:**
|
| 321 |
+
1. Paste lyrics (or leave empty for instrumental)
|
| 322 |
+
2. Set music style
|
| 323 |
+
3. Enter song title
|
| 324 |
+
4. Choose model
|
| 325 |
+
5. Click Generate!
|
| 326 |
+
|
| 327 |
+
**Tips:**
|
| 328 |
+
- V4_5ALL: Best overall quality
|
| 329 |
+
- V5: Latest model
|
| 330 |
+
- Instrumental: No vocals, just music
|
| 331 |
+
""")
|
| 332 |
|
| 333 |
+
with gr.Column(scale=2):
|
| 334 |
+
# Output Area
|
| 335 |
+
output = gr.Markdown(
|
| 336 |
+
value="### Ready to generate!\n\nEnter lyrics and settings, then click 'Generate Song'"
|
|
|
|
| 337 |
)
|
| 338 |
+
|
| 339 |
+
with gr.Tab("🔍 Check Any Task"):
|
| 340 |
+
with gr.Row():
|
| 341 |
+
with gr.Column(scale=1):
|
| 342 |
+
gr.Markdown("### Check Task Status")
|
| 343 |
+
gr.Markdown("Enter any Suno Task ID to check its status")
|
| 344 |
+
|
| 345 |
+
check_task_id = gr.Textbox(
|
| 346 |
+
label="Task ID",
|
| 347 |
+
placeholder="Enter Task ID from generation or separation",
|
| 348 |
+
info="From Song Generator or Vocal Separator"
|
| 349 |
)
|
| 350 |
+
|
| 351 |
+
check_btn = gr.Button("🔍 Check Status", variant="primary")
|
| 352 |
+
check_clear_btn = gr.Button("🗑️ Clear", variant="secondary")
|
| 353 |
|
| 354 |
+
with gr.Column(scale=2):
|
| 355 |
+
check_output = gr.Markdown(
|
| 356 |
+
value="### Enter a Task ID above\n\nPaste any Suno Task ID to check its current status and results."
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
with gr.Tab("📚 Instructions"):
|
| 360 |
+
gr.Markdown("""
|
| 361 |
+
## 📖 How to Use This App
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
+
### 🎶 Generate Song Tab
|
| 364 |
+
1. **Enter Lyrics** (or leave empty for instrumental)
|
| 365 |
+
2. **Set Music Style** (e.g., "Pop", "Rock", "Jazz")
|
| 366 |
+
3. **Enter Song Title**
|
| 367 |
+
4. **Choose Model** (V4_5ALL recommended)
|
| 368 |
+
5. **Click "Generate Song"**
|
| 369 |
+
|
| 370 |
+
### 🔍 Check Any Task Tab
|
| 371 |
+
1. **Paste any Suno Task ID**
|
| 372 |
+
2. **Click "Check Status"**
|
| 373 |
+
3. **View results and download links**
|
| 374 |
+
|
| 375 |
+
### ⏱️ What to Expect
|
| 376 |
+
|
| 377 |
+
**After generating:**
|
| 378 |
+
- You'll get a **Task ID** immediately
|
| 379 |
+
- Generation takes **1-3 minutes**
|
| 380 |
+
- **Callback** sent to https://1hit.no/callback.php
|
| 381 |
+
- Use Task ID to **check status manually**
|
| 382 |
+
|
| 383 |
+
**Task IDs come from:**
|
| 384 |
+
- Song Generator (this app)
|
| 385 |
+
- Vocal Separator (other app)
|
| 386 |
+
- Any Suno API request
|
| 387 |
+
|
| 388 |
+
### 🎵 Getting Your Songs
|
| 389 |
+
|
| 390 |
+
1. **Stream URL:** Ready in 30-60 seconds
|
| 391 |
+
2. **Download URL:** Ready in 2-3 minutes
|
| 392 |
+
3. **Both appear in status check**
|
| 393 |
+
4. **Audio player** included for streaming
|
| 394 |
+
|
| 395 |
+
### 🔧 Troubleshooting
|
| 396 |
+
|
| 397 |
+
**Task not found?**
|
| 398 |
+
- Wait a few minutes
|
| 399 |
+
- Check callback logs at https://1hit.no/viewer.php
|
| 400 |
+
- Ensure Task ID is correct
|
| 401 |
+
|
| 402 |
+
**No audio links?**
|
| 403 |
+
- Wait 2-3 minutes
|
| 404 |
+
- Check status again
|
| 405 |
+
- Generation may have failed
|
| 406 |
+
""")
|
| 407 |
|
| 408 |
gr.Markdown("---")
|
| 409 |
gr.Markdown(
|
|
|
|
| 417 |
elem_id="footer"
|
| 418 |
)
|
| 419 |
|
| 420 |
+
# Event handlers for Generate Song tab
|
| 421 |
+
def clear_all():
|
| 422 |
+
return "", "Pop", "Generated Song", False, "V4_5ALL", "### Ready to generate!\n\nEnter lyrics and settings, then click 'Generate Song'"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
|
|
|
|
| 424 |
clear_btn.click(
|
| 425 |
clear_all,
|
| 426 |
outputs=[lyrics_text, style, title, instrumental, model, output]
|
| 427 |
)
|
| 428 |
|
|
|
|
| 429 |
generate_btn.click(
|
| 430 |
generate_song_from_text,
|
| 431 |
inputs=[lyrics_text, style, title, instrumental, model],
|
| 432 |
outputs=output
|
| 433 |
)
|
| 434 |
+
|
| 435 |
+
# Event handlers for Check Any Task tab
|
| 436 |
+
def clear_check():
|
| 437 |
+
return "", "### Enter a Task ID above\n\nPaste any Suno Task ID to check its current status and results."
|
| 438 |
+
|
| 439 |
+
check_clear_btn.click(
|
| 440 |
+
clear_check,
|
| 441 |
+
outputs=[check_task_id, check_output]
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
check_btn.click(
|
| 445 |
+
get_task_info,
|
| 446 |
+
inputs=[check_task_id],
|
| 447 |
+
outputs=check_output
|
| 448 |
+
)
|
| 449 |
|
| 450 |
+
# Launch the app
|
| 451 |
if __name__ == "__main__":
|
| 452 |
print("🚀 Starting Suno Song Generator")
|
| 453 |
print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")
|
| 454 |
+
print("🌐 Open your browser to: http://localhost:7860")
|
| 455 |
+
|
| 456 |
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|