haraberget commited on
Commit
d15c3c8
·
verified ·
1 Parent(s): 70969e2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # -----------------------------
5
+ # CONFIG
6
+ # -----------------------------
7
+ SUNO_API_BASE = "https://api.sunoapi.org"
8
+ API_KEY = "YOUR_API_KEY_HERE" # set via env in real use
9
+
10
+ HEADERS = {
11
+ "Authorization": f"Bearer {API_KEY}",
12
+ "Content-Type": "application/json"
13
+ }
14
+
15
+ # -----------------------------
16
+ # API HELPERS (minimal, clear)
17
+ # -----------------------------
18
+
19
+ def generate_lyrics(theme, style):
20
+ payload = {
21
+ "prompt": f"Theme: {theme}. Style: {style}."
22
+ }
23
+ r = requests.post(f"{SUNO_API_BASE}/lyrics/generate", json=payload, headers=HEADERS)
24
+ r.raise_for_status()
25
+ return r.json().get("lyrics", "")
26
+
27
+
28
+ def generate_song(prompt, lyrics, model="v5"):
29
+ payload = {
30
+ "model": model,
31
+ "prompt": prompt,
32
+ "lyrics": lyrics
33
+ }
34
+ r = requests.post(f"{SUNO_API_BASE}/music/generate", json=payload, headers=HEADERS)
35
+ r.raise_for_status()
36
+ return r.json()
37
+
38
+
39
+ def separate_vocals(song_id):
40
+ payload = {"song_id": song_id}
41
+ r = requests.post(f"{SUNO_API_BASE}/audio/separate", json=payload, headers=HEADERS)
42
+ r.raise_for_status()
43
+ return r.json()
44
+
45
+ # -----------------------------
46
+ # GRADIO LOGIC
47
+ # -----------------------------
48
+
49
+ def run_pipeline(style, theme, custom_lyrics):
50
+ # Step 1: lyrics
51
+ if not custom_lyrics.strip():
52
+ lyrics = generate_lyrics(theme, style)
53
+ else:
54
+ lyrics = custom_lyrics
55
+
56
+ # Step 2: song
57
+ song = generate_song(style, lyrics)
58
+ song_id = song.get("id")
59
+ audio_url = song.get("audio_url")
60
+
61
+ return lyrics, audio_url, song_id
62
+
63
+
64
+ def run_separation(song_id):
65
+ result = separate_vocals(song_id)
66
+ return result.get("vocals_url"), result.get("instrumental_url")
67
+
68
+ # -----------------------------
69
+ # UI
70
+ # -----------------------------
71
+
72
+ with gr.Blocks(title="AI Music Instrument – Suno") as app:
73
+ gr.Markdown("# 🎵 AI Music Instrument (Suno API)\nTreat AI like a studio instrument, not a platform.")
74
+
75
+ with gr.Row():
76
+ style = gr.Textbox(label="Style / Genre", placeholder="old ballad, lute, choir, dark medieval")
77
+ theme = gr.Textbox(label="Theme", placeholder="fallen king, fate, prophecy")
78
+
79
+ lyrics_input = gr.Textbox(label="Custom Lyrics (optional)", lines=6)
80
+
81
+ run_btn = gr.Button("Generate Song")
82
+
83
+ lyrics_out = gr.Textbox(label="Lyrics Used", lines=8)
84
+ audio_out = gr.Audio(label="Generated Song", type="filepath")
85
+ song_id_out = gr.Textbox(label="Song ID")
86
+
87
+ gr.Markdown("## 🎚 Stem Separation")
88
+ sep_btn = gr.Button("Separate Vocals / Instrumental")
89
+ vocals_out = gr.Audio(label="Vocals Stem", type="filepath")
90
+ inst_out = gr.Audio(label="Instrumental Stem", type="filepath")
91
+
92
+ run_btn.click(run_pipeline, [style, theme, lyrics_input], [lyrics_out, audio_out, song_id_out])
93
+ sep_btn.click(run_separation, song_id_out, [vocals_out, inst_out])
94
+
95
+ app.launch()