Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,59 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
|
| 4 |
-
# -----------------------------
|
| 5 |
-
# CONFIG
|
| 6 |
-
# -----------------------------
|
| 7 |
-
SUNO_API_BASE = "https://api.sunoapi.org"
|
| 8 |
import os
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
-
# Load
|
| 14 |
load_dotenv()
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
suno_api_key = os.getenv('SUNO_API_KEY')
|
| 18 |
-
|
| 19 |
-
if not API_KEY:
|
| 20 |
-
raise RuntimeError("Missing SunoKey environment variable")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# API HELPERS (minimal, clear)
|
| 29 |
-
# -----------------------------
|
| 30 |
-
|
| 31 |
-
def generate_lyrics(theme, style):
|
| 32 |
-
payload = {
|
| 33 |
-
"prompt": f"Theme: {theme}. Style: {style}."
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return r.json().get("lyrics", "")
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def generate_song(prompt, lyrics, model="v5"):
|
| 41 |
-
payload = {
|
| 42 |
-
"model": model,
|
| 43 |
-
"prompt": prompt,
|
| 44 |
-
"lyrics": lyrics
|
| 45 |
}
|
| 46 |
-
r = requests.post(f"{SUNO_API_BASE}/music/generate", json=payload, headers=HEADERS)
|
| 47 |
-
r.raise_for_status()
|
| 48 |
-
return r.json()
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def separate_vocals(song_id):
|
| 52 |
-
payload = {"song_id": song_id}
|
| 53 |
-
r = requests.post(f"{SUNO_API_BASE}/audio/separate", json=payload, headers=HEADERS)
|
| 54 |
-
r.raise_for_status()
|
| 55 |
-
return r.json()
|
| 56 |
-
|
| 57 |
-
# -----------------------------
|
| 58 |
-
# GRADIO LOGIC
|
| 59 |
-
# -----------------------------
|
| 60 |
-
|
| 61 |
-
def run_pipeline(style, theme, custom_lyrics):
|
| 62 |
-
# Step 1: lyrics
|
| 63 |
-
if not custom_lyrics.strip():
|
| 64 |
-
lyrics = generate_lyrics(theme, style)
|
| 65 |
-
else:
|
| 66 |
-
lyrics = custom_lyrics
|
| 67 |
-
|
| 68 |
-
# Step 2: song
|
| 69 |
-
song = generate_song(style, lyrics)
|
| 70 |
-
song_id = song.get("id")
|
| 71 |
-
audio_url = song.get("audio_url")
|
| 72 |
-
|
| 73 |
-
return lyrics, audio_url, song_id
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def run_separation(song_id):
|
| 77 |
-
result = separate_vocals(song_id)
|
| 78 |
-
return result.get("vocals_url"), result.get("instrumental_url")
|
| 79 |
-
|
| 80 |
-
# -----------------------------
|
| 81 |
-
# UI
|
| 82 |
-
# -----------------------------
|
| 83 |
-
|
| 84 |
-
with gr.Blocks(title="AI Music Instrument – Suno") as app:
|
| 85 |
-
gr.Markdown("# 🎵 AI Music Instrument (Suno API)\nTreat AI like a studio instrument, not a platform.")
|
| 86 |
-
|
| 87 |
-
with gr.Row():
|
| 88 |
-
style = gr.Textbox(label="Style / Genre", placeholder="old ballad, lute, choir, dark medieval")
|
| 89 |
-
theme = gr.Textbox(label="Theme", placeholder="fallen king, fate, prophecy")
|
| 90 |
-
|
| 91 |
-
lyrics_input = gr.Textbox(label="Custom Lyrics (optional)", lines=6)
|
| 92 |
-
|
| 93 |
-
run_btn = gr.Button("Generate Song")
|
| 94 |
-
|
| 95 |
-
lyrics_out = gr.Textbox(label="Lyrics Used", lines=8)
|
| 96 |
-
audio_out = gr.Audio(label="Generated Song", type="filepath")
|
| 97 |
-
song_id_out = gr.Textbox(label="Song ID")
|
| 98 |
-
|
| 99 |
-
gr.Markdown("## 🎚 Stem Separation")
|
| 100 |
-
sep_btn = gr.Button("Separate Vocals / Instrumental")
|
| 101 |
-
vocals_out = gr.Audio(label="Vocals Stem", type="filepath")
|
| 102 |
-
inst_out = gr.Audio(label="Instrumental Stem", type="filepath")
|
| 103 |
-
|
| 104 |
-
run_btn.click(run_pipeline, [style, theme, lyrics_input], [lyrics_out, audio_out, song_id_out])
|
| 105 |
-
sep_btn.click(run_separation, song_id_out, [vocals_out, inst_out])
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Load .env
|
| 8 |
load_dotenv()
|
| 9 |
+
SUNO_KEY = os.getenv("SunoKey")
|
| 10 |
|
| 11 |
+
API_BASE = "https://api.sunoapi.org/api/v1"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def generate_lyrics(prompt):
|
| 14 |
+
# Step 1: Submit lyrics generation task
|
| 15 |
+
submit_url = f"{API_BASE}/lyrics"
|
| 16 |
+
headers = {
|
| 17 |
+
"Authorization": f"Bearer {SUNO_KEY}",
|
| 18 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
+
data = {
|
| 21 |
+
"prompt": prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
submit_resp = requests.post(submit_url, json=data, headers=headers)
|
| 25 |
+
if submit_resp.status_code != 200:
|
| 26 |
+
return f"Submit failed: {submit_resp.status_code} {submit_resp.text}"
|
| 27 |
+
|
| 28 |
+
submit_json = submit_resp.json()
|
| 29 |
+
task_id = submit_json.get("data", {}).get("taskId")
|
| 30 |
+
if not task_id:
|
| 31 |
+
return f"Failed to get taskId: {submit_json}"
|
| 32 |
+
|
| 33 |
+
# Step 2: Poll for lyrics
|
| 34 |
+
details_url = f"{API_BASE}/lyrics/details?taskId={task_id}"
|
| 35 |
+
for _ in range(30): # poll up to ~30 times (~30*2 sec = 1 min)
|
| 36 |
+
time.sleep(2)
|
| 37 |
+
details_resp = requests.get(details_url, headers=headers)
|
| 38 |
+
if details_resp.status_code != 200:
|
| 39 |
+
continue
|
| 40 |
+
details_json = details_resp.json()
|
| 41 |
+
if details_json.get("data", {}).get("data"):
|
| 42 |
+
lyrics_variants = details_json["data"]["data"]
|
| 43 |
+
results = []
|
| 44 |
+
for i, variant in enumerate(lyrics_variants):
|
| 45 |
+
results.append(f"Variant {i+1}:\n{variant.get('text', '')}")
|
| 46 |
+
return "\n\n".join(results)
|
| 47 |
+
|
| 48 |
+
return "Lyrics generation timed out. Try again."
|
| 49 |
+
|
| 50 |
+
# Gradio interface
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("## Suno Lyrics Generator")
|
| 53 |
+
prompt_input = gr.Textbox(label="Enter your song prompt")
|
| 54 |
+
output_box = gr.Textbox(label="Generated Lyrics", lines=15)
|
| 55 |
+
generate_btn = gr.Button("Generate Lyrics")
|
| 56 |
+
|
| 57 |
+
generate_btn.click(generate_lyrics, inputs=prompt_input, outputs=output_box)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|