SUNO-API-V5 / app.py
MySafeCode's picture
Update app.py
ee2e3a0 verified
raw
history blame
3.59 kB
import gradio as gr
import requests
import os
import json
# Suno API key
SUNO_KEY = os.environ.get("SunoKey", "")
if not SUNO_KEY:
print("⚠️ SunoKey not set!")
def submit_song_generation(lyrics_text, style, title, instrumental, model):
"""Submit song generation request and return Task ID immediately"""
if not SUNO_KEY:
return "❌ Error: SunoKey not configured in environment variables"
if not lyrics_text.strip() and not instrumental:
return "❌ Error: Please provide lyrics or select instrumental"
if not style.strip():
return "❌ Error: Please provide a music style"
if not title.strip():
return "❌ Error: Please provide a song title"
try:
# Prepare request data
request_data = {
"customMode": True,
"instrumental": instrumental,
"model": model,
"callBackUrl": "https://1hit.no/callback.php",
"style": style,
"title": title,
}
if not instrumental:
# Apply character limits
if model == "V4" and len(lyrics_text) > 3000:
lyrics_text = lyrics_text[:3000]
elif model in ["V4_5", "V4_5PLUS", "V4_5ALL", "V5"] and len(lyrics_text) > 5000:
lyrics_text = lyrics_text[:5000]
request_data["prompt"] = lyrics_text
else:
request_data["prompt"] = ""
# Submit generation request
resp = requests.post(
"https://api.sunoapi.org/api/v1/generate",
json=request_data,
headers={
"Authorization": f"Bearer {SUNO_KEY}",
"Content-Type": "application/json"
},
timeout=30
)
print(f"Submission response: {resp.status_code}")
print(f"Response: {resp.text}")
if resp.status_code != 200:
return f"❌ Submission failed: HTTP {resp.status_code}\n\n{resp.text}"
data = resp.json()
print(f"Parsed data: {data}")
# Extract task ID from response
task_id = None
if "taskId" in data:
task_id = data["taskId"]
output = f"## ✅ TASK SUBMITTED SUCCESSFULLY!\n\n"
output += f"**🎯 Your Task ID:** `{task_id}`\n\n"
elif "data" in data and "taskId" in data["data"]:
task_id = data["data"]["taskId"]
output = f"## ✅ TASK SUBMITTED SUCCESSFULLY!\n\n"
output += f"**🎯 Your Task ID:** `{task_id}`\n\n"
elif data.get("data") and "taskId" in data.get("data", {}):
task_id = data["data"]["taskId"]
output = f"## ✅ TASK SUBMITTED SUCCESSFULLY!\n\n"
output += f"**🎯 Your Task ID:** `{task_id}`\n\n"
else:
return f"❌ Could not extract Task ID from response\n\n**Raw Response:**\n```json\n{json.dumps(data, indent=2)}\n```"
output += f"**📱 What to do with this Task ID:**\n\n"
output += "1. **Copy this Task ID:** `" + task_id + "`\n"
output += "2. **Switch to the 'Check Any Task' tab**\n"
output += "3. **Paste the Task ID** in the input field\n"
output += "4. **Click 'Check Status'** to see your song results\n\n"
output += "**⏱️ What happens now:**\n"
output += "- Suno AI is generating your song (1-3 minutes)\n"
output += "- You can check status anytime with the Task ID\n"
output += "- When ready, you'll see audio URLs