Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import json
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
# Page configuration
|
|
@@ -12,26 +13,37 @@ st.set_page_config(
|
|
| 12 |
# Title
|
| 13 |
st.title("🎵 Suno API Audio Generator")
|
| 14 |
|
| 15 |
-
# Load secrets from Hugging Face Space
|
| 16 |
-
SUNO_API_KEY = st.secrets.get("
|
| 17 |
-
CALLBACK_URL = st.secrets.get("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Show secret status
|
| 20 |
if SUNO_API_KEY and CALLBACK_URL:
|
| 21 |
-
st.success("✅ Secrets loaded from Hugging Face!")
|
|
|
|
| 22 |
else:
|
| 23 |
-
st.error("❌ Secrets not
|
| 24 |
st.info("""
|
| 25 |
-
|
| 26 |
1. Go to **Settings** → **Repository secrets**
|
| 27 |
2. Add:
|
| 28 |
-
- `
|
| 29 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
""")
|
| 31 |
|
| 32 |
# API Form
|
| 33 |
st.markdown("---")
|
| 34 |
-
st.markdown("### API Parameters")
|
| 35 |
|
| 36 |
task_id = st.text_input("Task ID", value="5c79****be8e")
|
| 37 |
audio_id = st.text_input("Audio ID", value="e231****-****-****-****-****8cadc7dc")
|
|
@@ -39,71 +51,127 @@ audio_id = st.text_input("Audio ID", value="e231****-****-****-****-****8cadc7dc
|
|
| 39 |
# Make API call button
|
| 40 |
if st.button("🎵 Generate Audio", type="primary", use_container_width=True):
|
| 41 |
if not SUNO_API_KEY or not CALLBACK_URL:
|
| 42 |
-
st.error("Please configure
|
| 43 |
elif not task_id or not audio_id:
|
| 44 |
-
st.error("Please enter Task ID and Audio ID")
|
| 45 |
else:
|
| 46 |
-
with st.spinner("Making API call..."):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
st.
|
|
|
|
| 62 |
POST https://api.sunoapi.org/api/v1/wav/generate
|
| 63 |
|
| 64 |
Headers:
|
| 65 |
-
{
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
Body:
|
| 68 |
{json.dumps(data, indent=2)}
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
# Secret info
|
| 99 |
-
|
|
|
|
| 100 |
if SUNO_API_KEY:
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
else:
|
| 104 |
-
st.write("**
|
| 105 |
|
| 106 |
if CALLBACK_URL:
|
| 107 |
-
st.write(f"**
|
| 108 |
else:
|
| 109 |
-
st.write("**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import json
|
| 3 |
+
import requests
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
# Page configuration
|
|
|
|
| 13 |
# Title
|
| 14 |
st.title("🎵 Suno API Audio Generator")
|
| 15 |
|
| 16 |
+
# Load secrets from Hugging Face Space - using YOUR exact secret names
|
| 17 |
+
SUNO_API_KEY = st.secrets.get("SunoKey", "")
|
| 18 |
+
CALLBACK_URL = st.secrets.get("CallbackURL", "")
|
| 19 |
+
|
| 20 |
+
# Debug: Show what secrets were found
|
| 21 |
+
st.write("### 🔍 Debug Info")
|
| 22 |
+
st.write(f"Secrets found: {list(st.secrets.keys())}")
|
| 23 |
+
st.write(f"SunoKey loaded: {'✅ Yes' if SUNO_API_KEY else '❌ No'}")
|
| 24 |
+
st.write(f"CallbackURL loaded: {'✅ Yes' if CALLBACK_URL else '❌ No'}")
|
| 25 |
|
| 26 |
# Show secret status
|
| 27 |
if SUNO_API_KEY and CALLBACK_URL:
|
| 28 |
+
st.success("✅ Secrets loaded successfully from Hugging Face!")
|
| 29 |
+
st.info(f"Loaded **SunoKey** and **CallbackURL** secrets")
|
| 30 |
else:
|
| 31 |
+
st.error("❌ Secrets not loaded correctly")
|
| 32 |
st.info("""
|
| 33 |
+
**Make sure you've added these secrets in Hugging Face Space:**
|
| 34 |
1. Go to **Settings** → **Repository secrets**
|
| 35 |
2. Add:
|
| 36 |
+
- **Key:** `SunoKey` (exact name)
|
| 37 |
+
- **Value:** Your Suno API Bearer token
|
| 38 |
+
|
| 39 |
+
3. Add:
|
| 40 |
+
- **Key:** `CallbackURL` (exact name)
|
| 41 |
+
- **Value:** Your callback URL
|
| 42 |
""")
|
| 43 |
|
| 44 |
# API Form
|
| 45 |
st.markdown("---")
|
| 46 |
+
st.markdown("### 🎛️ API Parameters")
|
| 47 |
|
| 48 |
task_id = st.text_input("Task ID", value="5c79****be8e")
|
| 49 |
audio_id = st.text_input("Audio ID", value="e231****-****-****-****-****8cadc7dc")
|
|
|
|
| 51 |
# Make API call button
|
| 52 |
if st.button("🎵 Generate Audio", type="primary", use_container_width=True):
|
| 53 |
if not SUNO_API_KEY or not CALLBACK_URL:
|
| 54 |
+
st.error("❌ Please configure SunoKey and CallbackURL in Hugging Face Space settings")
|
| 55 |
elif not task_id or not audio_id:
|
| 56 |
+
st.error("❌ Please enter both Task ID and Audio ID")
|
| 57 |
else:
|
| 58 |
+
with st.spinner("Making API call to Suno..."):
|
| 59 |
+
try:
|
| 60 |
+
# Prepare the API call using YOUR secrets
|
| 61 |
+
headers = {
|
| 62 |
+
"Authorization": f"Bearer {SUNO_API_KEY}",
|
| 63 |
+
"Content-Type": "application/json"
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
data = {
|
| 67 |
+
"taskId": task_id,
|
| 68 |
+
"audioId": audio_id,
|
| 69 |
+
"callBackUrl": CALLBACK_URL
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
# Show what we're sending
|
| 73 |
+
with st.expander("📡 Request Details", expanded=True):
|
| 74 |
+
st.code(f"""
|
| 75 |
POST https://api.sunoapi.org/api/v1/wav/generate
|
| 76 |
|
| 77 |
Headers:
|
| 78 |
+
{{
|
| 79 |
+
"Authorization": "Bearer {SUNO_API_KEY[:10]}...{SUNO_API_KEY[-10:] if len(SUNO_API_KEY) > 20 else '***'}",
|
| 80 |
+
"Content-Type": "application/json"
|
| 81 |
+
}}
|
| 82 |
|
| 83 |
Body:
|
| 84 |
{json.dumps(data, indent=2)}
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
# Make the actual API call
|
| 88 |
+
response = requests.post(
|
| 89 |
+
"https://api.sunoapi.org/api/v1/wav/generate",
|
| 90 |
+
headers=headers,
|
| 91 |
+
json=data,
|
| 92 |
+
timeout=30
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Check response
|
| 96 |
+
if response.status_code == 200:
|
| 97 |
+
result = response.json()
|
| 98 |
+
st.success(f"✅ Success! Status: {response.status_code}")
|
| 99 |
+
|
| 100 |
+
# Display response
|
| 101 |
+
st.markdown("### 📋 API Response")
|
| 102 |
+
st.json(result)
|
| 103 |
+
|
| 104 |
+
# Download button
|
| 105 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 106 |
+
st.download_button(
|
| 107 |
+
label="📥 Download JSON Response",
|
| 108 |
+
data=json.dumps(result, indent=2),
|
| 109 |
+
file_name=f"suno_response_{timestamp}.json",
|
| 110 |
+
mime="application/json"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Show useful info
|
| 114 |
+
if "jobId" in result:
|
| 115 |
+
st.info(f"**Job ID:** `{result['jobId']}`")
|
| 116 |
+
if "message" in result:
|
| 117 |
+
st.info(f"**Message:** {result['message']}")
|
| 118 |
+
|
| 119 |
+
else:
|
| 120 |
+
st.error(f"❌ API Error: {response.status_code}")
|
| 121 |
+
try:
|
| 122 |
+
error_details = response.json()
|
| 123 |
+
st.json(error_details)
|
| 124 |
+
except:
|
| 125 |
+
st.text(f"Response: {response.text}")
|
| 126 |
+
|
| 127 |
+
except requests.exceptions.Timeout:
|
| 128 |
+
st.error("⏰ Request timed out after 30 seconds")
|
| 129 |
+
except requests.exceptions.ConnectionError:
|
| 130 |
+
st.error("🔌 Connection error - check your internet connection")
|
| 131 |
+
except requests.exceptions.RequestException as e:
|
| 132 |
+
st.error(f"⚠️ Request failed: {str(e)}")
|
| 133 |
+
except Exception as e:
|
| 134 |
+
st.error(f"❌ Unexpected error: {str(e)}")
|
| 135 |
|
| 136 |
+
# Secret info section
|
| 137 |
+
st.markdown("---")
|
| 138 |
+
with st.expander("🔐 Secret Information"):
|
| 139 |
if SUNO_API_KEY:
|
| 140 |
+
# Show masked key
|
| 141 |
+
key_length = len(SUNO_API_KEY)
|
| 142 |
+
if key_length > 20:
|
| 143 |
+
masked = f"{SUNO_API_KEY[:10]}...{SUNO_API_KEY[-10:]}"
|
| 144 |
+
elif key_length > 8:
|
| 145 |
+
masked = f"{SUNO_API_KEY[:4]}...{SUNO_API_KEY[-4:]}"
|
| 146 |
+
else:
|
| 147 |
+
masked = "••••••••"
|
| 148 |
+
|
| 149 |
+
st.write(f"**SunoKey (API Key):** `{masked}`")
|
| 150 |
+
st.write(f"**Key Length:** {key_length} characters")
|
| 151 |
else:
|
| 152 |
+
st.write("**SunoKey:** Not loaded")
|
| 153 |
|
| 154 |
if CALLBACK_URL:
|
| 155 |
+
st.write(f"**CallbackURL:** `{CALLBACK_URL}`")
|
| 156 |
else:
|
| 157 |
+
st.write("**CallbackURL:** Not loaded")
|
| 158 |
+
|
| 159 |
+
st.write("---")
|
| 160 |
+
st.write("**Note:** Secrets are securely loaded from Hugging Face Space settings.")
|
| 161 |
+
|
| 162 |
+
# Instructions
|
| 163 |
+
st.markdown("---")
|
| 164 |
+
st.markdown("### 📝 How to Use")
|
| 165 |
+
st.markdown("""
|
| 166 |
+
1. **Configure Secrets** in Hugging Face Space settings:
|
| 167 |
+
- Add `SunoKey` with your API Bearer token
|
| 168 |
+
- Add `CallbackURL` with your callback endpoint
|
| 169 |
+
|
| 170 |
+
2. **Enter API Parameters**:
|
| 171 |
+
- Task ID from your Suno account
|
| 172 |
+
- Audio ID from your Suno account
|
| 173 |
+
|
| 174 |
+
3. **Click "Generate Audio"** to make the API call
|
| 175 |
+
|
| 176 |
+
4. **View Response** and download the JSON result
|
| 177 |
+
""")
|