SUNO-API / app.py
haraberget's picture
Upload app.py
1f1b856 verified
raw
history blame
6.37 kB
import gradio as gr
import requests
import os
import json
from datetime import datetime
# Load Suno API key
SUNO_KEY = os.environ.get("SunoKey", "")
if not SUNO_KEY:
print("⚠️ Warning: SunoKey environment variable not set!")
def test_all_endpoints():
"""Test all possible Suno API endpoints to find the right one"""
results = ["# 🔍 Testing All Suno API Endpoints", ""]
headers = {"Authorization": f"Bearer {SUNO_KEY}"}
endpoints = [
("GET", "https://api.sunoapi.org/api/v1/lyrics", "Base lyrics endpoint"),
("GET", "https://api.sunoapi.org/api/v1/lyrics/record-info", "Record info endpoint"),
("GET", "https://api.sunoapi.org/api/v1/lyrics/details", "Details endpoint"),
("POST", "https://api.sunoapi.org/api/v1/lyrics", "Submit lyrics"),
]
for method, url, description in endpoints:
results.append(f"\n## 🔗 {method} {url.split('/')[-1]}")
results.append(f"**Description:** {description}")
try:
if method == "GET":
response = requests.get(url, headers=headers, timeout=10)
else: # POST
# Try POST with minimal payload
post_headers = headers.copy()
post_headers["Content-Type"] = "application/json"
payload = {
"prompt": "Test endpoint",
"callBackUrl": "http://test.com/callback"
}
response = requests.post(url, json=payload, headers=post_headers, timeout=10)
results.append(f"**HTTP Status:** {response.status_code}")
if response.status_code == 200:
try:
data = response.json()
results.append(f"**Response:** ```json\n{json.dumps(data, indent=2)[:500]}...\n```")
except:
results.append(f"**Response:** {response.text[:200]}...")
else:
results.append(f"**Error:** {response.text[:200]}...")
except Exception as e:
results.append(f"**Exception:** {str(e)}")
return "\n".join(results)
def test_correct_workflow():
"""Test what SHOULD be the correct workflow based on your code"""
results = ["# 🎯 Testing Correct Workflow", ""]
if not SUNO_KEY:
return "❌ No SunoKey configured"
headers = {"Authorization": f"Bearer {SUNO_KEY}"}
# Step 1: Submit lyrics (as per your original working code)
results.append("## 1️⃣ Submit Lyrics")
submit_url = "https://api.sunoapi.org/api/v1/lyrics"
submit_headers = headers.copy()
submit_headers["Content-Type"] = "application/json"
payload = {
"prompt": "Correct workflow test song",
"callBackUrl": "http://correct-test.com/callback"
}
try:
response = requests.post(submit_url, json=payload, headers=submit_headers, timeout=30)
results.append(f"**Status:** {response.status_code}")
if response.status_code == 200:
data = response.json()
results.append(f"**Code:** {data.get('code')}")
results.append(f"**Message:** {data.get('msg', 'No message')}")
if data.get("code") == 200 and "data" in data:
task_id = data["data"]["taskId"]
results.append(f"**Task ID:** `{task_id}`")
# Step 2: Check using record-info endpoint (from your example)
results.append("\n## 2️⃣ Check with /record-info")
record_url = "https://api.sunoapi.org/api/v1/lyrics/record-info"
# Try different query parameters
test_params = [
{"taskId": task_id},
{"id": task_id},
{"record_id": task_id},
{}, # No params
]
for params in test_params:
results.append(f"\n**Trying params:** {params}")
try:
record_response = requests.get(
record_url,
headers=headers,
params=params,
timeout=30
)
results.append(f"Status: {record_response.status_code}")
if record_response.status_code == 200:
record_data = record_response.json()
results.append(f"Response: ```json\n{json.dumps(record_data, indent=2)[:300]}...\n```")
else:
results.append(f"Error: {record_response.text[:100]}")
except Exception as e:
results.append(f"Exception: {str(e)}")
# Step 3: Also try details endpoint for comparison
results.append("\n## 3️⃣ Check with /details (for comparison)")
details_url = f"https://api.sunoapi.org/api/v1/lyrics/details?taskId={task_id}"
try:
details_response = requests.get(details_url, headers=headers, timeout=30)
results.append(f"Status: {details_response.status_code}")
if details_response.status_code == 200:
details_data = details_response.json()
results.append(f"Response: ```json\n{json.dumps(details_data, indent=2)[:300]}...\n```")
except Exception as e:
results.append(f"Exception: {str(e)}")
else:
results.append("❌ No task ID in response")
else:
results.append(f"❌ Submission failed: {response.text[:200]}")
except Exception as e:
results.append(f"❌ Exception: {str(e)}")
return "\n".join(results)
def check_api_documentation():
"""Show what we know about Suno API"""
return """# 📚 Suno API Documentation
## 🔍 What We Know:
### **Endpoints Found:**
1. `POST /api/v1/lyrics` - Submit lyrics generation
```json
{
"prompt": "song about...",
"callBackUrl": "https://your-webhook.com/callback"
}