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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import time
5
+ import json
6
+ from urllib.parse import urlparse, parse_qs
7
+
8
+ # Suno API key
9
+ SUNO_KEY = os.environ.get("SunoKey", "")
10
+ if not SUNO_KEY:
11
+ print("⚠️ SunoKey not set!")
12
+
13
+ def get_task_info(task_id):
14
+ """Manually check any Suno task status"""
15
+ if not task_id:
16
+ return "❌ Please enter a Task ID"
17
+
18
+ try:
19
+ resp = requests.get(
20
+ "https://api.sunoapi.org/api/v1/generate/record-info",
21
+ headers={"Authorization": f"Bearer {SUNO_KEY}"},
22
+ params={"taskId": task_id},
23
+ timeout=30
24
+ )
25
+
26
+ if resp.status_code != 200:
27
+ return f"❌ HTTP Error {resp.status_code}\n\n{resp.text}"
28
+
29
+ data = resp.json()
30
+
31
+ # Format the response for display
32
+ output = f"## 🔍 Task Status: `{task_id}`\n\n"
33
+
34
+ if data.get("code") == 200:
35
+ task_data = data.get("data", {})
36
+ status = task_data.get("status", "UNKNOWN")
37
+
38
+ output += f"**Status:** {status}\n"
39
+ output += f"**Task ID:** `{task_data.get('taskId', 'N/A')}`\n"
40
+ output += f"**Music ID:** `{task_data.get('musicId', 'N/A')}`\n"
41
+ output += f"**Created:** {task_data.get('createTime', 'N/A')}\n"
42
+
43
+ if status == "SUCCESS":
44
+ response_data = task_data.get("response", {})
45
+
46
+ # Try to parse response (could be string or dict)
47
+ if isinstance(response_data, str):
48
+ try:
49
+ response_data = json.loads(response_data)
50
+ except:
51
+ output += f"\n**Raw Response:**\n```\n{response_data}\n```\n"
52
+ response_data = {}
53
+
54
+ # Check for song data
55
+ songs = []
56
+ if isinstance(response_data, dict):
57
+ songs = response_data.get("sunoData", [])
58
+ if not songs:
59
+ songs = response_data.get("data", [])
60
+ elif isinstance(response_data, list):
61
+ songs = response_data
62
+
63
+ if songs:
64
+ output += f"\n## 🎵 Generated Songs ({len(songs)})\n\n"
65
+
66
+ for i, song in enumerate(songs, 1):
67
+ if isinstance(song, dict):
68
+ output += f"### Song {i}\n"
69
+ output += f"**Title:** {song.get('title', 'Untitled')}\n"
70
+ output += f"**ID:** `{song.get('id', 'N/A')}`\n"
71
+
72
+ # Audio URLs
73
+ audio_url = song.get('audioUrl') or song.get('audio_url')
74
+ stream_url = song.get('streamUrl') or song.get('stream_url')
75
+ download_url = song.get('downloadUrl') or song.get('download_url')
76
+
77
+ if audio_url:
78
+ output += f"**Audio:** [Play]({audio_url}) | [Download]({audio_url})\n"
79
+ elif stream_url:
80
+ output += f"**Stream:** [Play]({stream_url})\n"
81
+
82
+ if download_url:
83
+ output += f"**Download:** [MP3]({download_url})\n"
84
+
85
+ # Audio player
86
+ play_url = audio_url or stream_url
87
+ if play_url:
88
+ output += f"""\n<audio controls style="width: 100%; margin: 10px 0;">
89
+ <source src="{play_url}" type="audio/mpeg">
90
+ Your browser does not support audio.
91
+ </audio>\n"""
92
+
93
+ output += f"**Prompt:** {song.get('prompt', 'N/A')[:100]}...\n"
94
+ output += f"**Duration:** {song.get('duration', 'N/A')}s\n"
95
+ output += f"**Created:** {song.get('createTime', 'N/A')}\n\n"
96
+ output += "---\n\n"
97
+ else:
98
+ output += "\n**No song data found in response.**\n"
99
+
100
+ elif status == "FAILED":
101
+ error_msg = task_data.get("errorMessage", "Unknown error")
102
+ output += f"\n**Error:** {error_msg}\n"
103
+
104
+ elif status in ["PENDING", "PROCESSING", "RUNNING"]:
105
+ output += f"\n**Task is still processing...**\n"
106
+ output += f"Check again in 30 seconds.\n"
107
+
108
+ else:
109
+ output += f"\n**Unknown status:** {status}\n"
110
+
111
+ else:
112
+ output += f"**API Error:** {data.get('msg', 'Unknown')}\n"
113
+
114
+ # Show raw JSON for debugging
115
+ output += "\n## 📋 Raw Response\n"
116
+ output += f"```json\n{json.dumps(data, indent=2)}\n```"
117
+
118
+ return output
119
+
120
+ except Exception as e:
121
+ return f"❌ Error checking task: {str(e)}"
122
+
123
+ def generate_song_from_text(lyrics_text, style, title, instrumental, model):
124
+ """Generate a song from lyrics text"""
125
+ if not SUNO_KEY:
126
+ yield "❌ Error: SunoKey not configured in environment variables"
127
+ return
128
+
129
+ if not lyrics_text.strip() and not instrumental:
130
+ yield "❌ Error: Please provide lyrics or select instrumental"
131
+ return
132
+
133
+ if not style.strip():
134
+ yield "❌ Error: Please provide a music style"
135
+ return
136
+
137
+ if not title.strip():
138
+ yield "❌ Error: Please provide a song title"
139
+ return
140
+
141
+ try:
142
+ # Prepare request data
143
+ request_data = {
144
+ "customMode": True,
145
+ "instrumental": instrumental,
146
+ "model": model,
147
+ "callBackUrl": "https://1hit.no/gen/cb.php",
148
+ "style": style,
149
+ "title": title,
150
+ }
151
+
152
+ if not instrumental:
153
+ # Apply character limits
154
+ if model == "V4" and len(lyrics_text) > 3000:
155
+ lyrics_text = lyrics_text[:3000]
156
+ yield f"⚠️ Lyrics truncated to 3000 characters for V4 model\n\n"
157
+ elif model in ["V4_5", "V4_5PLUS", "V4_5ALL", "V5"] and len(lyrics_text) > 5000:
158
+ lyrics_text = lyrics_text[:5000]
159
+ yield f"⚠️ Lyrics truncated to 5000 characters for {model} model\n\n"
160
+
161
+ request_data["prompt"] = lyrics_text
162
+ else:
163
+ request_data["prompt"] = ""
164
+
165
+ # Apply style length limits
166
+ if model == "V4" and len(style) > 200:
167
+ style = style[:200]
168
+ yield f"⚠️ Style truncated to 200 characters for V4 model\n\n"
169
+ elif model in ["V4_5", "V4_5PLUS", "V4_5ALL", "V5"] and len(style) > 1000:
170
+ style = style[:1000]
171
+ yield f"⚠️ Style truncated to 1000 characters for {model} model\n\n"
172
+
173
+ # Apply title length limits
174
+ if model in ["V4", "V4_5ALL"] and len(title) > 80:
175
+ title = title[:80]
176
+ yield f"⚠️ Title truncated to 80 characters for {model} model\n\n"
177
+ elif model in ["V4_5", "V4_5PLUS", "V5"] and len(title) > 100:
178
+ title = title[:100]
179
+ yield f"⚠️ Title truncated to 100 characters for {model} model\n\n"
180
+
181
+ request_data["style"] = style
182
+ request_data["title"] = title
183
+
184
+ yield f"## 🚀 Submitting Song Request\n\n"
185
+ yield f"**Title:** {title}\n"
186
+ yield f"**Style:** {style}\n"
187
+ yield f"**Model:** {model}\n"
188
+ yield f"**Instrumental:** {'Yes' if instrumental else 'No'}\n"
189
+ if not instrumental:
190
+ yield f"**Lyrics length:** {len(lyrics_text)} characters\n\n"
191
+ yield f"**Callback URL:** https://1hit.no/callback.php\n\n"
192
+
193
+ # Submit generation request
194
+ try:
195
+ resp = requests.post(
196
+ "https://api.sunoapi.org/api/v1/generate",
197
+ json=request_data,
198
+ headers={
199
+ "Authorization": f"Bearer {SUNO_KEY}",
200
+ "Content-Type": "application/json"
201
+ },
202
+ timeout=30
203
+ )
204
+
205
+ if resp.status_code != 200:
206
+ yield f"❌ Submission failed: HTTP {resp.status_code}"
207
+ yield f"\n**Response:**\n```\n{resp.text}\n```"
208
+ return
209
+
210
+ data = resp.json()
211
+ print(f"Submission response: {json.dumps(data, indent=2)}")
212
+
213
+ if data.get("code") != 200:
214
+ yield f"❌ API error: {data.get('msg', 'Unknown')}"
215
+ return
216
+
217
+ # Extract task ID from response
218
+ task_id = None
219
+ if "taskId" in data:
220
+ task_id = data["taskId"]
221
+ elif "data" in data and "taskId" in data["data"]:
222
+ task_id = data["data"]["taskId"]
223
+ elif data.get("data") and "taskId" in data.get("data", {}):
224
+ task_id = data["data"]["taskId"]
225
+
226
+ if not task_id:
227
+ yield f"❌ Could not extract Task ID from response"
228
+ yield f"\n**Raw Response:**\n```json\n{json.dumps(data, indent=2)}\n```"
229
+ return
230
+
231
+ yield f"## ✅ Request Submitted Successfully!\n\n"
232
+ yield f"**🎯 Task ID:** `{task_id}`\n\n"
233
+ yield f"**⏳ Status:** Generation started\n"
234
+ yield f"**📞 Callback:** https://1hit.no/callback.php\n\n"
235
+ yield "**What happens now:**\n"
236
+ yield "1. Suno AI generates your song (1-3 minutes)\n"
237
+ yield "2. You'll get a callback notification\n"
238
+ yield "3. Use the Task ID above to check status manually\n\n"
239
+ yield "---\n\n"
240
+ yield f"## 🔍 Check Status Manually\n\n"
241
+ yield f"Use this Task ID: `{task_id}`\n\n"
242
+ yield "**To check status:**\n"
243
+ yield "1. Copy the Task ID above\n"
244
+ yield "2. Go to 'Check Any Task' tab\n"
245
+ yield "3. Paste and click 'Check Status'\n"
246
+ yield "4. Or wait for callback notification\n\n"
247
+ yield "**Generation time:**\n"
248
+ yield "- 30-60 seconds for stream URL\n"
249
+ yield "- 2-3 minutes for download URL\n"
250
+
251
+ # Simple one-time check after 30 seconds
252
+ yield "\n**⏰ Will check once in 30 seconds...**\n"
253
+ time.sleep(30)
254
+
255
+ # Single status check
256
+ status_result = get_task_info(task_id)
257
+ yield "\n## 📊 Status Check (30s)\n\n"
258
+ yield status_result
259
+
260
+ except Exception as e:
261
+ yield f"❌ Error submitting request: {str(e)}"
262
+ return
263
+
264
+ except Exception as e:
265
+ yield f"❌ **Unexpected Error:** {str(e)}"
266
+
267
+ # Function to handle URL parameters
268
+ def parse_url_params(request: gr.Request):
269
+ """Parse taskid from URL parameters"""
270
+ task_id = None
271
+ if request:
272
+ query_params = parse_qs(urlparse(request.request.url).query)
273
+ if 'taskid' in query_params:
274
+ task_id = query_params['taskid'][0]
275
+
276
+ return task_id
277
+
278
+ # Create the app
279
+ with gr.Blocks() as app:
280
+ gr.Markdown("# 🎵 Suno Song Generator")
281
+ gr.Markdown("Create songs from lyrics and style using Suno AI")
282
+
283
+ # Store for URL parameters
284
+ url_task_id = gr.State(value="")
285
+
286
+ with gr.Tab("🎶 Generate Song", id=0) as tab_generate:
287
+ with gr.Row():
288
+ with gr.Column(scale=1):
289
+ # Lyrics Input
290
+ gr.Markdown("### Step 1: Enter Lyrics")
291
+
292
+ lyrics_text = gr.Textbox(
293
+ label="Lyrics",
294
+ placeholder="Paste your lyrics here...\n\nExample:\n(Verse 1)\nSun is shining, sky is blue\nBirds are singing, just for you...",
295
+ lines=10,
296
+ interactive=True
297
+ )
298
+
299
+ # Song Settings
300
+ gr.Markdown("### Step 2: Song Settings")
301
+
302
+ style = gr.Textbox(
303
+ label="Music Style",
304
+ placeholder="Example: Pop, Rock, Jazz, Classical, Electronic, Hip Hop, Country",
305
+ value="Folk soul flamenco glam rock goa trance fusion",
306
+ interactive=True
307
+ )
308
+
309
+ title = gr.Textbox(
310
+ label="Song Title",
311
+ placeholder="My Awesome Song",
312
+ value="Generated Song",
313
+ interactive=True
314
+ )
315
+
316
+ with gr.Row():
317
+ instrumental = gr.Checkbox(
318
+ label="Instrumental (No Vocals)",
319
+ value=False,
320
+ interactive=True
321
+ )
322
+ model = gr.Dropdown(
323
+ label="Model",
324
+ choices=["V5", "V4_5PLUS", "V4_5ALL", "V4_5", "V4"],
325
+ value="V4_5ALL",
326
+ interactive=True
327
+ )
328
+
329
+ # Action Buttons
330
+ generate_btn = gr.Button("🚀 Generate Song", variant="primary")
331
+ clear_btn = gr.Button("🗑️ Clear All", variant="secondary")
332
+
333
+ # Instructions
334
+ gr.Markdown("""
335
+ **How to use:**
336
+ 1. Paste lyrics (or leave empty for instrumental)
337
+ 2. Set music style
338
+ 3. Enter song title
339
+ 4. Choose model
340
+ 5. Click Generate!
341
+
342
+ **Tips:**
343
+ - V4_5ALL: Best overall quality
344
+ - V5: Latest model
345
+ - Instrumental: No vocals, just music
346
+ """)
347
+
348
+ with gr.Column(scale=2):
349
+ # Output Area
350
+ output = gr.Markdown(
351
+ value="### Ready to generate!\n\nEnter lyrics and settings, then click 'Generate Song'"
352
+ )
353
+
354
+ with gr.Tab("🔍 Check Any Task", id=1) as tab_check:
355
+ with gr.Row():
356
+ with gr.Column(scale=1):
357
+ gr.Markdown("### Check Task Status")
358
+ gr.Markdown("Enter any Suno Task ID to check its status")
359
+
360
+ check_task_id = gr.Textbox(
361
+ label="Task ID",
362
+ placeholder="Enter Task ID from generation or separation",
363
+ info="From Song Generator or Vocal Separator"
364
+ )
365
+
366
+ check_btn = gr.Button("🔍 Check Status", variant="primary")
367
+ check_clear_btn = gr.Button("🗑️ Clear", variant="secondary")
368
+
369
+ # URL parameter info
370
+ gr.Markdown("""
371
+ **Quick access via URL:**
372
+ Add `?taskid=YOUR_TASK_ID` to the URL
373
+
374
+ Example:
375
+ `https://huggingface.co/spaces/MySafeCode/SUNO-API-V5?taskid=5af381d69f5022ae60e28ec71eb6a997`
376
+ """)
377
+
378
+ with gr.Column(scale=2):
379
+ check_output = gr.Markdown(
380
+ value="### Enter a Task ID above\n\nPaste any Suno Task ID to check its current status and results."
381
+ )
382
+
383
+ with gr.Tab("📚 Instructions", id=2):
384
+ gr.Markdown("""
385
+ ## 📖 How to Use This App
386
+
387
+ ### 🎶 Generate Song Tab
388
+ 1. **Enter Lyrics** (or leave empty for instrumental)
389
+ 2. **Set Music Style** (e.g., "Pop", "Rock", "Jazz")
390
+ 3. **Enter Song Title**
391
+ 4. **Choose Model** (V4_5ALL recommended)
392
+ 5. **Click "Generate Song"**
393
+
394
+ ### 🔍 Check Any Task Tab
395
+ 1. **Paste any Suno Task ID**
396
+ 2. **Click "Check Status"**
397
+ 3. **View results and download links**
398
+
399
+ **Quick URL Access:**
400
+ - Visit with `?taskid=YOUR_TASK_ID` in the URL
401
+ - Automatically switches to Check tab
402
+ - Shows task status immediately
403
+
404
+ Example:
405
+ ```
406
+ https://huggingface.co/spaces/MySafeCode/SUNO-API-V5?taskid=5af381d69f5022ae60e28ec71eb6a997
407
+ ```
408
+
409
+ ### ⏱️ What to Expect
410
+
411
+ **After generating:**
412
+ - You'll get a **Task ID** immediately
413
+ - Generation takes **1-3 minutes**
414
+ - **Callback** sent to https://1hit.no/gen/cb.php
415
+ - Use Task ID to **check status manually**
416
+
417
+ **Task IDs come from:**
418
+ - Song Generator (this app)
419
+ - Vocal Separator (other app)
420
+ - Any Suno API request
421
+
422
+ ### 🎵 Getting Your Songs
423
+
424
+ 1. **Stream URL:** Ready in 30-60 seconds
425
+ 2. **Download URL:** Ready in 2-3 minutes
426
+ 3. **Both appear in status check**
427
+ 4. **Audio player** included for streaming
428
+
429
+ ### 🔧 Troubleshooting
430
+
431
+ **Task not found?**
432
+ - Wait a few minutes
433
+ - Check callback logs at https://1hit.no/gen/view.php
434
+ - Ensure Task ID is correct
435
+
436
+ **No audio links?**
437
+ - Wait 2-3 minutes
438
+ - Check status again
439
+ - Generation may have failed
440
+ """)
441
+
442
+ with gr.Tab("📚 Less Instructions", id=3):
443
+ gr.Markdown("""
444
+ ## 📖 How to Use This App
445
+
446
+ ### 🎶 Generate Song Tab
447
+ 1. **Enter Lyrics** (or leave empty for instrumental)
448
+ 2. **Set Music Style** (e.g., "Pop", "Rock", "Jazz")
449
+ 3. **Enter Song Title**
450
+ 4. **Choose Model** (V4_5ALL recommended)
451
+ 5. **Click "Generate Song"**
452
+
453
+ ### 🔍 Check Any Task via URL
454
+ Add `?taskid=YOUR_TASK_ID` to the URL
455
+
456
+ Example:
457
+ ```
458
+ https://huggingface.co/spaces/MySafeCode/SUNO-API-V5?taskid=5af381d69f5022ae60e28ec71eb6a997
459
+ ```
460
+
461
+ ### 📞 Callback Status
462
+ https://1hit.no/gen/view.php
463
+
464
+ **No audio links?**
465
+ - Wait 2-3 minutes
466
+ - Check status again
467
+ - Generation may have failed
468
+ """)
469
+
470
+ gr.Markdown("---")
471
+ gr.Markdown(
472
+ """
473
+ <div style="text-align: center; padding: 20px;">
474
+ <p>Powered by <a href="https://suno.ai" target="_blank">Suno AI</a> •
475
+ <a href="https://sunoapi.org" target="_blank">Suno API Docs</a></p>
476
+ <p><small>Create custom songs by providing lyrics and music style</small></p>
477
+ </div>
478
+ """,
479
+ elem_id="footer"
480
+ )
481
+
482
+ # Event handlers for Generate Song tab
483
+ def clear_all():
484
+ return "", "Pop", "Generated Song", False, "V4_5ALL", "### Ready to generate!\n\nEnter lyrics and settings, then click 'Generate Song'"
485
+
486
+ clear_btn.click(
487
+ clear_all,
488
+ outputs=[lyrics_text, style, title, instrumental, model, output]
489
+ )
490
+
491
+ generate_btn.click(
492
+ generate_song_from_text,
493
+ inputs=[lyrics_text, style, title, instrumental, model],
494
+ outputs=output
495
+ )
496
+
497
+ # Event handlers for Check Any Task tab
498
+ def clear_check():
499
+ return "", "### Enter a Task ID above\n\nPaste any Suno Task ID to check its current status and results."
500
+
501
+ check_clear_btn.click(
502
+ clear_check,
503
+ outputs=[check_task_id, check_output]
504
+ )
505
+
506
+ check_btn.click(
507
+ get_task_info,
508
+ inputs=[check_task_id],
509
+ outputs=check_output
510
+ )
511
+
512
+ # Function to handle URL parameter on load
513
+ def on_page_load(request: gr.Request):
514
+ """Handle URL parameters when page loads"""
515
+ task_id = parse_url_params(request)
516
+ if task_id:
517
+ # Set the task ID in the input field
518
+ return task_id, get_task_info(task_id), gr.update(selected=1)
519
+ return "", "### Enter a Task ID above\n\nPaste any Suno Task ID to check its current status and results.", gr.update(selected=0)
520
+
521
+ # Load URL parameters when the app starts
522
+ app.load(
523
+ fn=on_page_load,
524
+ inputs=[],
525
+ outputs=[check_task_id, check_output, gr.Tabs(selected=tab_check)],
526
+ queue=False
527
+ )
528
+
529
+ # Launch the app
530
+ if __name__ == "__main__":
531
+ print("🚀 Starting Suno Song Generator")
532
+ print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")
533
+ print("🌐 Open your browser to: http://localhost:7860")
534
+ print("🔗 Use URL parameter: http://localhost:7860?taskid=YOUR_TASK_ID")
535
+
536
+ app.launch(server_name="0.0.0.0", server_port=7860, share=False)