Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import json | |
| import requests | |
| import time | |
| from datetime import datetime | |
| # Polling function | |
| def poll_status(task_id, single_poll=False): | |
| """Poll Suno API for task status including task ID""" | |
| if not task_id: | |
| st.error("❌ Task ID cannot be empty") | |
| return False | |
| headers = { | |
| "Authorization": f"Bearer {st.session_state.suno_api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| try: | |
| with st.spinner(f"Checking status for task: {task_id}..."): | |
| # Two possible endpoints - let's try both formats | |
| endpoints =[ | |
| f"https://api.sunoapi.org/api/v1/wav/record-info/{task_id}", | |
| f"https://api.sunoapi.org/api/v1/wav/record-info?taskId={task_id}" | |
| ] | |
| response = None | |
| for endpoint in endpoints: | |
| try: | |
| response = requests.get( | |
| endpoint, | |
| headers=headers, | |
| timeout=30 | |
| ) | |
| if response.status_code == 200: | |
| break | |
| except: | |
| continue | |
| if not response: | |
| st.error("❌ Failed to connect to polling endpoint") | |
| return False | |
| if response.status_code == 200: | |
| result = response.json() | |
| # Display result | |
| if result.get("code") == 200: | |
| data = result.get("data", {}) | |
| if data.get("successFlag") == "SUCCESS": | |
| # Audio is ready! | |
| audio_url = data.get("response", {}).get("audioWavUrl") | |
| if audio_url: | |
| st.success("✅ Audio ready!") | |
| st.balloons() | |
| # Display audio info | |
| st.markdown("### 🎵 Audio Information") | |
| cols = st.columns(2) | |
| with cols[0]: | |
| st.metric("Task ID", data.get("taskId", "N/A")[:12] + "...") | |
| st.metric("Music ID", data.get("musicId", "N/A")[:12] + "...") | |
| with cols[1]: | |
| st.metric("Status", data.get("successFlag", "N/A")) | |
| st.metric("Created", data.get("createTime", "N/A")) | |
| # Audio player | |
| st.markdown("### 🔊 Listen") | |
| st.audio(audio_url, format="audio/wav") | |
| # Download section | |
| st.markdown("### 📥 Download") | |
| st.code(audio_url) | |
| col_dl1, col_dl2 = st.columns(2) | |
| with col_dl1: | |
| st.download_button( | |
| label="⬇ Download WAV", | |
| data=requests.get(audio_url).content if audio_url.startswith("http") else b"", | |
| file_name=f"audio_{data.get('taskId', 'unknown')[:8]}.wav", | |
| mime="audio/wav", | |
| key=f"download_{task_id}" | |
| ) | |
| with col_dl2: | |
| if st.button("📋 Copy URL", key=f"copy_{task_id}"): | |
| st.code(audio_url) | |
| st.success("URL copied to clipboard!") | |
| # Stop auto-polling if active | |
| if st.session_state.get('auto_poll'): | |
| st.session_state.auto_poll = False | |
| st.success("✅ Auto-polling stopped (audio ready)") | |
| else: | |
| st.info("⏳ Audio still processing...") | |
| st.json(data) | |
| else: | |
| # Still processing or error | |
| status = data.get("successFlag", "UNKNOWN") | |
| if status == "PROCESSING": | |
| st.info(f"⏳ Processing... ({status})") | |
| elif status == "FAILED": | |
| st.error(f"❌ Processing failed: {data.get('errorMessage', 'Unknown error')}") | |
| else: | |
| st.info(f"ℹ️ Status: {status}") | |
| # Show progress info | |
| st.json(data) | |
| else: | |
| st.error(f"❌ API Error: {result.get('msg', 'Unknown error')}") | |
| st.json(result) | |
| else: | |
| st.error(f"❌ Status check failed: {response.status_code}") | |
| try: | |
| error_data = response.json() | |
| st.json(error_data) | |
| except: | |
| st.text(f"Response: {response.text}") | |
| except Exception as e: | |
| st.error(f"⚠️ Polling error: {str(e)}") | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="Suno API Generator", | |
| page_icon="🎵", | |
| layout="wide" | |
| ) | |
| # Load YOUR secret names | |
| SUNO_API_KEY = st.secrets.get("SunoKey", "") | |
| CALLBACK_URL = "https://1hit.no/wav/cb.php" | |
| # Initialize session state | |
| if 'task_id' not in st.session_state: | |
| st.session_state.task_id = None | |
| if 'polling' not in st.session_state: | |
| st.session_state.polling = False | |
| if 'poll_results' not in st.session_state: | |
| st.session_state.poll_results = [] | |
| if 'auto_poll' not in st.session_state: | |
| st.session_state.auto_poll = False | |
| if 'suno_api_key' not in st.session_state: | |
| st.session_state.suno_api_key = SUNO_API_KEY | |
| # Title and info | |
| st.title("🎵 Suno API Audio Generator") | |
| with st.expander("📋 How it works", expanded=True): | |
| st.markdown(""" | |
| ### Workflow: | |
| 1. **Step 1**: Submit task to Suno API → Get `taskId` from response | |
| 2. **Step 2**: Suno processes audio (async) | |
| 3. **Step 3**: Suno sends callback to: `https://1hit.no/wav/cb.php` | |
| 4. **Step 4**: You can view callbacks at: `https://1hit.no/wav/view.php` | |
| 5. **Step 5**: OR use Poll button to check status via `/api/v1/record-info` endpoint | |
| ### Polling Endpoint: | |
| ```javascript | |
| fetch('https://api.sunoapi.org/api/v1/wav/record-info', { | |
| method: 'GET', | |
| headers: { | |
| 'Authorization': 'Bearer YOUR_TOKEN' | |
| } | |
| }) | |
| ``` | |
| ### Error Handling: | |
| - **400**: Task ID cannot be empty | |
| - Make sure to include task ID in polling request | |
| """) | |
| # Main layout | |
| col1, col2 = st.columns([2, 1]) | |
| with col1: | |
| st.header("🚀 Step 1: Submit to Suno API") | |
| # Form inputs | |
| task_id = st.text_input("Task ID", value="5c79****be8e", help="Enter your Suno task ID", key="input_task_id") | |
| audio_id = st.text_input("Audio ID", value="e231****-****-****-****-****8cadc7dc", help="Enter your Suno audio ID", key="input_audio_id") | |
| if st.button("🎵 Generate Audio", type="primary", use_container_width=True, key="generate_btn"): | |
| if not SUNO_API_KEY: | |
| st.error("❌ SunoKey not configured in Hugging Face secrets") | |
| st.info("Add `SunoKey` secret in Hugging Face Space settings") | |
| st.stop() | |
| if not task_id or not audio_id: | |
| st.error("❌ Please enter both Task ID and Audio ID") | |
| st.stop() | |
| with st.spinner("Sending request to Suno API..."): | |
| headers = { | |
| "Authorization": f"Bearer {SUNO_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| # Test sending to correct endpoint | |
| endpoint_url = "https://api.sunoapi.org/api/v1/wav/generate" | |
| st.info(f"Making request to: {endpoint_url}") | |
| payload = { | |
| "taskId": task_id, | |
| "audioId": audio_id, | |
| "callBackUrl": CALLBACK_URL | |
| } | |
| st.json(payload) | |
| try: | |
| response = requests.post( | |
| endpoint_url, | |
| headers=headers, | |
| json=payload, | |
| timeout=30 | |
| ) | |
| if response.status_code == 200: | |
| result = response.json() | |
| # Display response | |
| with st.expander("📋 API Response", expanded=True): | |
| st.json(result) | |
| if result.get("code") == 200 and "data" in result and "taskId" in result["data"]: | |
| st.session_state.task_id = result["data"]["taskId"] | |
| st.success(f"✅ Task submitted successfully!") | |
| st.info(f"**Task ID:** `{st.session_state.task_id}`") | |
| # Auto-fill poll input | |
| st.session_state.poll_task_input = st.session_state.task_id | |
| st.markdown("---") | |
| st.markdown("### 🔄 Next Steps:") | |
| st.markdown(""" | |
| 1. **Automatic Callback**: Suno will send result to your callback URL | |
| 2. **Manual Poll**: Use the Poll button to check status | |
| 3. **View Logs**: Check [Callback Viewer](https://1hit.no/wav/view.php) | |
| """) | |
| else: | |
| st.error("❌ Unexpected response format") | |
| # Debug info | |
| st.markdown("**Debug Response:**") | |
| st.code(str(result)) | |
| else: | |
| st.error(f"❌ API Error: {response.status_code}") | |
| try: | |
| error_data = response.json() | |
| st.json(error_data) | |
| except: | |
| st.text(f"Response: {response.text}") | |
| except requests.exceptions.Timeout: | |
| st.error("⏰ Request timed out after 30 seconds") | |
| except requests.exceptions.ConnectionError: | |
| st.error("🔌 Connection error - check your internet connection") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"⚠️ Request failed: {str(e)}") | |
| with col2: | |
| st.header("🔍 Step 2: Poll Status") | |
| # Manual task ID input for polling | |
| poll_task_id = st.text_input( | |
| "Task ID to Poll", | |
| value=st.session_state.get('poll_task_input', st.session_state.task_id or ""), | |
| help="Enter task ID to check status", | |
| key="poll_task_input_field" | |
| ) | |
| col2a, col2b = st.columns(2) | |
| with col2a: | |
| poll_once_btn = st.button("🔄 Poll Once", type="secondary", use_container_width=True, key="poll_once_btn") | |
| with col2b: | |
| auto_poll_btn = st.button("🔁 Auto Poll (10s)", type="secondary", use_container_width=True, key="auto_poll_btn") | |
| # Handle poll button clicks | |
| if poll_once_btn: | |
| if not poll_task_id: | |
| st.error("❌ Please enter a Task ID") | |
| elif not SUNO_API_KEY: | |
| st.error("❌ SunoKey not configured") | |
| else: | |
| poll_status(poll_task_id, single_poll=True) | |
| if auto_poll_btn: | |
| if not poll_task_id: | |
| st.error("❌ Please enter a Task ID") | |
| elif not SUNO_API_KEY: | |
| st.error("❌ SunoKey not configured") | |
| else: | |
| st.session_state.auto_poll = True | |
| st.session_state.auto_poll_task_id = poll_task_id | |
| st.rerun() | |
| # Display poll results if any | |
| if st.session_state.poll_results: | |
| st.markdown("---") | |
| st.header("📊 Polling History") | |
| # Show last 5 results | |
| recent_results = list(reversed(st.session_state.poll_results[-5:])) | |
| for i, result in enumerate(recent_results): | |
| with st.expander(f"Poll {i+1} - {result['timestamp']} - Task: {result['task_id'][:12]}...", expanded=(i == len(recent_results)-1)): | |
| response_data = result["response"] | |
| if response_data.get("code") == 200: | |
| data = response_data.get("data", {}) | |
| status = data.get("successFlag", "UNKNOWN") | |
| # Status badge | |
| if status == "SUCCESS": | |
| st.success(f"✅ {status}") | |
| elif status == "PROCESSING": | |
| st.info(f"⏳ {status}") | |
| elif status == "FAILED": | |
| st.error(f"❌ {status}") | |
| else: | |
| st.warning(f"⚠️ {status}") | |
| # Display key info | |
| cols = st.columns(3) | |
| with cols[0]: | |
| st.metric("Task ID", data.get("taskId", "N/A")[:12] + "...") | |
| with cols[1]: | |
| st.metric("Music ID", data.get("musicId", "N/A")[:12] + "...") | |
| with cols[2]: | |
| st.metric("Status", status) | |
| # Show audio if available | |
| audio_url = data.get("response", {}).get("audioWavUrl") | |
| if audio_url: | |
| st.audio(audio_url, format="audio/wav") | |
| st.markdown(f"**Audio URL:** `{audio_url}`") | |
| # Show full response | |
| with st.expander("📋 Full Response JSON"): | |
| st.json(response_data) | |
| else: | |
| st.error(f"❌ Error: {response_data.get('msg', 'Unknown error')}") | |
| st.json(response_data) | |
| # Quick links and info | |
| st.markdown("---") | |
| st.markdown("### 📝 Quick Links") | |
| st.markdown(""" | |
| - 🔗 [View Suno Callbacks](https://1hit.no/wav/view.php) - See all callback logs | |
| - 📚 [Suno API Documentation](https://docs.sunoapi.org/) - Official API docs | |
| - 🔄 **Poll Endpoint**: `GET https://api.sunoapi.org/api/v1/wav/record-info` | |
| """) | |
| # Secret status | |
| with st.expander("🔐 Secret Status", expanded=False): | |
| if SUNO_API_KEY: | |
| masked_key = f"{SUNO_API_KEY[:8]}...{SUNO_API_KEY[-8:]}" if len(SUNO_API_KEY) > 16 else "••••••••" | |
| st.success(f"✅ SunoKey loaded ({len(SUNO_API_KEY)} chars)") | |
| st.code(f"SunoKey: {masked_key}") | |
| else: | |
| st.error("❌ SunoKey not found") | |
| st.info(f"**Callback URL:** `{CALLBACK_URL}`") |