Spaces:
Paused
Paused
Update app.py
Browse files9 unmodified lines10111213141516171818 unmodified lines373839374041424337 unmodified lines8182838485868720 unmodified lines10810911010710810911011111111211311411511611711811943 unmodified lines1631641651661671681694 unmodified lines17417517617117217317417517617717817936 unmodified lines2162172182192202212226 unmodified lines2292302312322332342352 unmodified lines2382392402412392422432442452462472482462492502512522532 unmodified lines2562572582552592602612622632 unmodified lines266267268264269270271272273274275276277278279280281282283284285286287288289290291292284293294295296297113 unmodified lines41141241340440540640740840941441541641741841942042161 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": "https://mysafecode-suno-api-v5.hf.space", # Empty callback URL - we'll poll instead "callBackUrl": f"{SPACE_URL}/callback", # Use the Space URL for callbacks "style": style, "title": title, }37 unmodified lines yield 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 request try:20 unmodified lines yield 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" 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 lines yield "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 lines songs = response_data.get("data", []) if not songs: songs = response_data.get("songs", []) if not songs: # Check if response_data itself is a song list if 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 lines yield "- 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" return else: # No songs found in response6 unmodified lines yield "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" return elif current_status == "FAILED":2 unmodified lines yield 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 "Please try again with different parameters." yield "\nPlease try again with different parameters." return elif 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 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 lines else: yield f"📊 **Status:** {current_status}\n" yield f"**Task ID:** `{task_id}`\n" yield f"**Check:** {attempt + 1}/{max_attempts}\n\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 lines yield 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 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 out yield "⏰ **Timeout after 10 minutes**\n\n" yield f"**Task ID:** `{task_id}`\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 lines 4. 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) **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