Spaces:
Paused
Paused
Update app.py
Browse files9 unmodified lines10111213141518 unmodified lines3435363738394037 unmodified lines78798081828320 unmodified lines10410510610710810911011111211311443 unmodified lines1581591601611621634 unmodified lines16816917017117217317417517617717817936 unmodified lines2162172182192202216 unmodified lines2282292302312322332 unmodified lines2362372382392402412422432442452462472482492 unmodified lines2522532542552562572582 unmodified lines261262263264265266267268269270271272273274275276277278279280281282283284285286287113 unmodified lines40140240340440540640740840941041141261 unmodified lines4744754764779 unmodified linesif not SUNO_KEY:print("⚠️ SunoKey not set!")def generate_song_from_text(lyrics_text, style, title, instrumental, model):"""Generate a song from lyrics text"""if not SUNO_KEY:18 unmodified lines"customMode": True,"instrumental": instrumental,"model": model,"callBackUrl": "https://mysafecode-suno-api-v5.hf.space", # Empty callback URL - we'll poll instead"style": style,"title": title,}37 unmodified linesyield f"**Instrumental:** {'Yes' if instrumental else 'No'}\n"if not instrumental:yield f"**Lyrics length:** {len(lyrics_text)} characters\n\n"# Submit generation requesttry:20 unmodified linesyield f"✅ **Request submitted successfully!**\n"yield f"**Task ID:** `{task_id}`\n\n"yield f"⏳ Song generation started...\n\n"yield "**What to do next:**\n"yield "1. Keep this window open while the song generates\n"yield "2. We'll automatically check the status every 10 seconds\n"yield "3. Generation typically takes 1-3 minutes\n"yield "4. When complete, you'll get streaming and download links\n\n"except Exception as e:yield f"❌ Error submitting request: {str(e)}"43 unmodified linesyield "2. Log in to your account\n"yield "3. Go to 'Generation History'\n"yield "4. Find your song by Task ID\n"return# Look for songs in different possible locations4 unmodified linessongs = response_data.get("data", [])if not songs:songs = response_data.get("songs", [])if not songs:# Check if response_data itself is a song listif isinstance(response_data.get("0"), dict):songs = [response_data.get("0")]if isinstance(response_data.get("1"), dict):songs.append(response_data.get("1"))elif isinstance(response_data, list):songs = response_data36 unmodified linesyield "- Download links may take 2-3 minutes\n"yield "- Files are kept for 15 days\n"yield f"- Task ID: `{task_id}` (save this for reference)\n"returnelse:# No songs found in response6 unmodified linesyield "2. Log in to your account\n"yield "3. Check 'Generation History'\n"yield "4. Look for this Task ID\n"returnelif current_status == "FAILED":2 unmodified linesyield f"**Status:** {current_status}\n"yield f"**Error:** {error_msg}\n"yield f"**Task ID:** `{task_id}`\n\n"yield "Please try again with different parameters."returnelif current_status in ["PENDING", "PROCESSING"]:yield f"⏳ **Status Update**\n\n"yield f"**Current Status:** {current_status}\n"yield f"**Task ID:** `{task_id}`\n"yield f"**Check:** {attempt + 1}/{max_attempts}\n\n"yield "**Estimated time remaining:**\n"yield "- Stream URL: 30-60 seconds\n"yield "- Download URL: 2-3 minutes\n"2 unmodified lineselse:yield f"📊 **Status:** {current_status}\n"yield f"**Task ID:** `{task_id}`\n"yield f"**Check:** {attempt + 1}/{max_attempts}\n\n"yield "Still processing...\n"else:2 unmodified linesyield f"⚠️ **API Error**\n\n"yield f"**Code:** {check_data.get('code')}\n"yield f"**Message:** {error_msg}\n"yield f"**Task ID:** `{task_id}`\n\n"yield "Will continue checking...\n"else:yield f"⚠️ **HTTP Error {check_resp.status_code}**\n\n"yield f"Failed to check status. Will retry in 10 seconds...\n"yield f"Check: {attempt + 1}/{max_attempts}\n"except requests.exceptions.Timeout:yield f"⏱️ **Timeout checking status**\n\n"yield "The status check timed out. Will try again in 10 seconds...\n"yield f"Check: {attempt + 1}/{max_attempts}\n"except Exception as e:yield f"⚠️ **Error checking status:** {str(e)}\n\n"yield "Will retry in 10 seconds...\n"yield f"Check: {attempt + 1}/{max_attempts}\n"# If we get here, we timed outyield "⏰ **Timeout after 10 minutes**\n\n"yield f"**Task ID:** `{task_id}`\n\n"yield "**What to do:**\n"yield "1. The song may still be processing\n"yield "2. Visit https://sunoapi.org\n"113 unmodified lines4. Choose model (V4_5ALL recommended)5. Click Generate Song!**Tips:**- Use structured lyrics with verses/chorus for best results- Style examples: "Pop", "Rock guitar", "Jazz piano", "Electronic"- V5: Latest model, best quality- V4_5ALL: Good balance, up to 8 minutes- Instrumental: Check for music only (no vocals)**Generation time:**- 30-60s: Stream URL ready61 unmodified linesif __name__ == "__main__":print("🚀 Starting Suno Song Generator")print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")app.launch(server_name="0.0.0.0", server_port=7860, share=False)9 unmodified lines10111213141516171818 unmodified lines3738394041424337 unmodified lines8182838485868720 unmodified lines10810911011111211311411511611711811943 unmodified lines1631641651661671681694 unmodified lines17417517617717817936 unmodified lines2162172182192202212226 unmodified lines2292302312322332342352 unmodified lines2382392402412422432442452462472482492502512522532 unmodified lines2562572582592602612622632 unmodified lines266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297113 unmodified lines41141241341441541641741841942042161 unmodified lines4834844854864874889 unmodified linesif not SUNO_KEY:print("⚠️ SunoKey not set!")# Get the current Space URL for callbacksSPACE_URL = os.environ.get("SPACE_URL", "https://mysafecode-suno-api-v5.hf.space")def generate_song_from_text(lyrics_text, style, title, instrumental, model):"""Generate a song from lyrics text"""if not SUNO_KEY:18 unmodified lines"customMode": True,"instrumental": instrumental,"model": model,"callBackUrl": f"{SPACE_URL}/callback", # Use the Space URL for callbacks"style": style,"title": title,}37 unmodified linesyield f"**Instrumental:** {'Yes' if instrumental else 'No'}\n"if not instrumental:yield f"**Lyrics length:** {len(lyrics_text)} characters\n\n"yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"# Submit generation requesttry:20 unmodified linesyield f"✅ **Request submitted successfully!**\n"yield f"**Task ID:** `{task_id}`\n\n"yield f"⏳ Song generation started...\n\n"yield "**What's happening now:**\n"yield "1. Suno AI is generating your song (1-3 minutes)\n"yield "2. We'll poll the API every 10 seconds for updates\n"yield "3. When complete, you'll get direct links to your song\n"yield "4. Suno will also send a callback to our server\n\n"yield "**Keep this window open while we monitor progress...**\n"except Exception as e:yield f"❌ Error submitting request: {str(e)}"43 unmodified linesyield "2. Log in to your account\n"yield "3. Go to 'Generation History'\n"yield "4. Find your song by Task ID\n"yield f"\n**Callback URL used:** {request_data['callBackUrl']}\n"return# Look for songs in different possible locations4 unmodified linessongs = response_data.get("data", [])if not songs:songs = response_data.get("songs", [])elif isinstance(response_data, list):songs = response_data36 unmodified linesyield "- Download links may take 2-3 minutes\n"yield "- Files are kept for 15 days\n"yield f"- Task ID: `{task_id}` (save this for reference)\n"yield f"- Callback URL: {request_data['callBackUrl']}\n"returnelse:# No songs found in response6 unmodified linesyield "2. Log in to your account\n"yield "3. Check 'Generation History'\n"yield "4. Look for this Task ID\n"yield f"\n**Callback URL used:** {request_data['callBackUrl']}\n"returnelif current_status == "FAILED":2 unmodified linesyield f"**Status:** {current_status}\n"yield f"**Error:** {error_msg}\n"yield f"**Task ID:** `{task_id}`\n\n"yield f"**Callback URL:** {request_data['callBackUrl']}\n"yield "\nPlease try again with different parameters."returnelif current_status in ["PENDING", "PROCESSING"]:yield f"⏳ **Status Update**\n\n"yield f"**Current Status:** {current_status}\n"yield f"**Task ID:** `{task_id}`\n"yield f"**Check:** {attempt + 1}/{max_attempts}\n"yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"yield "**Estimated time remaining:**\n"yield "- Stream URL: 30-60 seconds\n"yield "- Download URL: 2-3 minutes\n"2 unmodified lineselse:yield f"📊 **Status:** {current_status}\n"yield f"**Task ID:** `{task_id}`\n"yield f"**Check:** {attempt + 1}/{max_attempts}\n"yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"yield "Still processing...\n"else:2 unmodified linesyield f"⚠️ **API Error**\n\n"yield f"**Code:** {check_data.get('code')}\n"yield f"**Message:** {error_msg}\n"yield f"**Task ID:** `{task_id}`\n"yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"yield "Will continue checking...\n"else:yield f"⚠️ **HTTP Error {check_resp.status_code}**\n\n"yield f"Failed to check status. Will retry in 10 seconds...\n"yield f"Check: {attempt + 1}/{max_attempts}\n"yield f"Task ID: `{task_id}`\n"except requests.exceptions.Timeout:yield f"⏱️ **Timeout checking status**\n\n"yield "The status check timed out. Will try again in 10 seconds...\n"yield f"Check: {attempt + 1}/{max_attempts}\n"yield f"Task ID: `{task_id}`\n"except Exception as e:yield f"⚠️ **Error checking status:** {str(e)}\n\n"yield "Will retry in 10 seconds...\n"yield f"Check: {attempt + 1}/{max_attempts}\n"yield f"Task ID: `{task_id}`\n"# If we get here, we timed outyield "⏰ **Timeout after 10 minutes**\n\n"yield f"**Task ID:** `{task_id}`\n"yield f"**Callback URL:** {request_data['callBackUrl']}\n\n"yield "**What to do:**\n"yield "1. The song may still be processing\n"yield "2. Visit https://sunoapi.org\n"113 unmodified lines4. Choose model (V4_5ALL recommended)5. Click Generate Song!**Features:**- Real callback URL for notifications- Automatic polling for status updates- Direct streaming and download links- File upload/download support**Generation time:**- 30-60s: Stream URL ready61 unmodified linesif __name__ == "__main__":print("🚀 Starting Suno Song Generator")print(f"🔑 SunoKey: {'✅ Set' if SUNO_KEY else '❌ Not set'}")print(f"🌐 Space URL: {SPACE_URL}")print(f"📞 Callback URL: {SPACE_URL}/callback")app.launch(server_name="0.0.0.0", server_port=7860, share=False)
app.py+34-23