Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,51 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
import
|
13 |
-
import
|
14 |
from pydub import AudioSegment
|
15 |
from torch.cuda.amp import autocast
|
16 |
from audiocraft.models import MusicGen
|
17 |
from huggingface_hub import login
|
18 |
|
19 |
-
#
|
20 |
-
#
|
21 |
-
#
|
22 |
if not hasattr(torch, "get_default_device"):
|
23 |
torch.get_default_device = lambda: torch.device(
|
24 |
"cuda" if torch.cuda.is_available() else "cpu"
|
25 |
)
|
26 |
|
|
|
|
|
|
|
27 |
warnings.filterwarnings("ignore")
|
28 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
29 |
|
30 |
-
#
|
31 |
-
#
|
32 |
-
#
|
33 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
34 |
if not HF_TOKEN:
|
35 |
-
print("ERROR:
|
|
|
|
|
|
|
|
|
|
|
36 |
sys.exit(1)
|
37 |
-
login(HF_TOKEN)
|
38 |
|
39 |
-
#
|
40 |
-
#
|
41 |
-
#
|
42 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
43 |
print(f"Running on {device.upper()}")
|
44 |
if device == "cuda":
|
@@ -53,262 +60,311 @@ def gpu_clean():
|
|
53 |
|
54 |
gpu_clean()
|
55 |
|
56 |
-
#
|
57 |
-
#
|
58 |
-
#
|
59 |
-
print("Loading
|
60 |
-
musicgen = MusicGen.get_pretrained("
|
61 |
musicgen.set_generation_params(duration=10, two_step_cfg=False)
|
62 |
|
63 |
-
#
|
64 |
-
#
|
65 |
-
#
|
66 |
-
def log_resources(
|
67 |
-
if
|
68 |
-
print(f"
|
69 |
if device == "cuda":
|
70 |
-
alloc = torch.cuda.memory_allocated() / 1024
|
71 |
-
|
72 |
-
print(f"GPU
|
73 |
-
print(f"CPU
|
74 |
|
75 |
-
def vram_ok(
|
76 |
if device != "cuda":
|
77 |
return True
|
78 |
-
total = torch.cuda.get_device_properties(0).total_memory / 1024
|
79 |
-
free = total - torch.cuda.memory_allocated() / 1024
|
80 |
-
if free <
|
81 |
-
print(f"Only {free:.2f} GB VRAM free (<{
|
82 |
-
return free >=
|
83 |
-
|
84 |
-
#
|
85 |
-
#
|
86 |
-
#
|
87 |
-
def
|
88 |
-
step_txt
|
89 |
-
drum_txt
|
90 |
-
|
91 |
-
bass_txt
|
92 |
-
gtr_txt
|
93 |
-
return f"{base}{bass_txt}{gtr_txt}{drum_txt}{
|
94 |
-
|
95 |
-
def set_red_hot_chili_peppers_prompt(bpm,
|
96 |
-
return
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
def
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
def
|
164 |
-
return
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
def apply_eq(seg: AudioSegment):
|
172 |
return seg.low_pass_filter(8000).high_pass_filter(80)
|
173 |
|
174 |
def apply_fade(seg: AudioSegment, fin=1000, fout=1000):
|
175 |
return seg.fade_in(fin).fade_out(fout)
|
176 |
|
177 |
-
#
|
178 |
-
#
|
179 |
-
#
|
180 |
-
def generate_music(
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
184 |
if not prompt.strip():
|
185 |
-
return None,"⚠️ Prompt cannot be empty."
|
186 |
if not vram_ok():
|
187 |
-
return None,"⚠️
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
overlap = min(1.0,
|
194 |
-
render_len =
|
195 |
sr = musicgen.sample_rate
|
196 |
-
|
197 |
|
198 |
torch.manual_seed(42)
|
199 |
np.random.seed(42)
|
200 |
|
201 |
-
t0=time.time()
|
202 |
-
for i in range(
|
203 |
-
log_resources(f"
|
204 |
musicgen.set_generation_params(
|
205 |
-
duration=render_len,
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
221 |
os.unlink(tmp.name)
|
222 |
-
|
223 |
-
gpu_clean()
|
224 |
-
log_resources(f"after chunk {i+1}")
|
225 |
-
|
226 |
-
track=parts[0]
|
227 |
-
for seg in parts[1:]:
|
228 |
-
track=track.append(seg+1,crossfade=xfade)
|
229 |
-
track=track[:total_dur*1000]
|
230 |
-
track=apply_fade(apply_eq(track).normalize(headroom=-9.0))
|
231 |
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
|
239 |
def clear_inputs():
|
240 |
-
return (
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
"""
|
249 |
|
250 |
with gr.Blocks(css=css) as demo:
|
251 |
-
gr.HTML('<div class="header"><
|
252 |
-
|
253 |
-
prompt_box=gr.Textbox(label="Prompt",lines=4)
|
254 |
-
cfg_scale =gr.Slider(1,10,3,label="CFG")
|
255 |
-
top_k =gr.Slider(10,500,250,step=10,label="Top-K")
|
256 |
-
top_p =gr.Slider(0,1,0.9,step=0.05,label="Top-P")
|
257 |
-
temp =gr.Slider(0.1,2,1,step=0.1,label="Temperature")
|
258 |
-
total_dur =gr.Radio([30,60,90,120],value=30,label="Length (s)")
|
259 |
-
chunk_dur =gr.Slider(5,15,10,step=1,label="Chunk Length (s)")
|
260 |
-
xfade =gr.Slider(100,2000,1000,step=100,label="Cross-fade (ms)")
|
261 |
-
|
262 |
-
bpm =gr.Slider(60,180,120,label="BPM")
|
263 |
-
drum =gr.Dropdown(["none","standard rock","funk groove","techno kick","jazz swing"],value="none",label="Drum")
|
264 |
-
synth =gr.Dropdown(["none","analog synth","digital pad","arpeggiated synth"],value="none",label="Synth")
|
265 |
-
step =gr.Dropdown(["none","syncopated steps","steady steps","complex steps"],value="none",label="Steps")
|
266 |
-
bass =gr.Dropdown(["none","slap bass","deep bass","melodic bass"],value="none",label="Bass")
|
267 |
-
gtr =gr.Dropdown(["none","distorted","clean","jangle"],value="none",label="Guitar")
|
268 |
-
|
269 |
-
# Genre buttons
|
270 |
-
btns={
|
271 |
-
"RHCP 🌶️":set_red_hot_chili_peppers_prompt,
|
272 |
-
"Nirvana 🎸":set_nirvana_grunge_prompt,
|
273 |
-
"Pearl Jam 🦪":set_pearl_jam_grunge_prompt,
|
274 |
-
"Soundgarden 🌑":set_soundgarden_grunge_prompt,
|
275 |
-
"Foo Fighters 🤘":set_foo_fighters_prompt,
|
276 |
-
"Smashing Pumpkins 🎃":set_smashing_pumpkins_prompt,
|
277 |
-
"Radiohead 🧠":set_radiohead_prompt,
|
278 |
-
"Classic Rock 🎸":set_classic_rock_prompt,
|
279 |
-
"Alt Rock 🎵":set_alternative_rock_prompt,
|
280 |
-
"Post-Punk 🖤":set_post_punk_prompt,
|
281 |
-
"Indie Rock 🎤":set_indie_rock_prompt,
|
282 |
-
"Funk Rock 🕺":set_funk_rock_prompt,
|
283 |
-
"Detroit Techno 🎛️":set_detroit_techno_prompt,
|
284 |
-
"Deep House 🏠":set_deep_house_prompt
|
285 |
-
}
|
286 |
with gr.Row():
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
try:
|
312 |
-
demo._server.app.docs_url=demo._server.app.redoc_url=demo._server.app.openapi_url=None
|
313 |
except Exception:
|
314 |
pass
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import gc
|
4 |
+
import time
|
5 |
+
import random
|
6 |
+
import warnings
|
7 |
+
import tempfile
|
8 |
+
|
9 |
+
import psutil
|
10 |
+
import numpy as np
|
11 |
+
import torch
|
12 |
+
import torchaudio
|
13 |
+
import gradio as gr
|
14 |
from pydub import AudioSegment
|
15 |
from torch.cuda.amp import autocast
|
16 |
from audiocraft.models import MusicGen
|
17 |
from huggingface_hub import login
|
18 |
|
19 |
+
# ----------------------------------------------------------------------
|
20 |
+
# Compatibility shim (torch < 2.3)
|
21 |
+
# ----------------------------------------------------------------------
|
22 |
if not hasattr(torch, "get_default_device"):
|
23 |
torch.get_default_device = lambda: torch.device(
|
24 |
"cuda" if torch.cuda.is_available() else "cpu"
|
25 |
)
|
26 |
|
27 |
+
# ----------------------------------------------------------------------
|
28 |
+
# Warnings & CUDA fragmentation tuning
|
29 |
+
# ----------------------------------------------------------------------
|
30 |
warnings.filterwarnings("ignore")
|
31 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
32 |
|
33 |
+
# ----------------------------------------------------------------------
|
34 |
+
# Hugging Face authentication
|
35 |
+
# ----------------------------------------------------------------------
|
36 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
37 |
if not HF_TOKEN:
|
38 |
+
print("ERROR: environment variable HF_TOKEN not set.")
|
39 |
+
sys.exit(1)
|
40 |
+
try:
|
41 |
+
login(HF_TOKEN)
|
42 |
+
except Exception as e:
|
43 |
+
print(f"ERROR: Hugging Face login failed: {e}")
|
44 |
sys.exit(1)
|
|
|
45 |
|
46 |
+
# ----------------------------------------------------------------------
|
47 |
+
# Device setup & cleanup
|
48 |
+
# ----------------------------------------------------------------------
|
49 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
50 |
print(f"Running on {device.upper()}")
|
51 |
if device == "cuda":
|
|
|
60 |
|
61 |
gpu_clean()
|
62 |
|
63 |
+
# ----------------------------------------------------------------------
|
64 |
+
# Load MusicGen model (fixed checkpoint name)
|
65 |
+
# ----------------------------------------------------------------------
|
66 |
+
print("Loading MusicGen ‘medium’ checkpoint …")
|
67 |
+
musicgen = MusicGen.get_pretrained("medium", device=device)
|
68 |
musicgen.set_generation_params(duration=10, two_step_cfg=False)
|
69 |
|
70 |
+
# ----------------------------------------------------------------------
|
71 |
+
# Resource monitoring
|
72 |
+
# ----------------------------------------------------------------------
|
73 |
+
def log_resources(stage=""):
|
74 |
+
if stage:
|
75 |
+
print(f"--- {stage} ---")
|
76 |
if device == "cuda":
|
77 |
+
alloc = torch.cuda.memory_allocated() / 1024**3
|
78 |
+
resv = torch.cuda.memory_reserved() / 1024**3
|
79 |
+
print(f"GPU Mem | Alloc {alloc:.2f} GB Reserved {resv:.2f} GB")
|
80 |
+
print(f"CPU Mem | {psutil.virtual_memory().percent}% used")
|
81 |
|
82 |
+
def vram_ok(threshold=3.5):
|
83 |
if device != "cuda":
|
84 |
return True
|
85 |
+
total = torch.cuda.get_device_properties(0).total_memory / 1024**3
|
86 |
+
free = total - torch.cuda.memory_allocated() / 1024**3
|
87 |
+
if free < threshold:
|
88 |
+
print(f"WARNING: Only {free:.2f} GB VRAM free (<{threshold} GB).")
|
89 |
+
return free >= threshold
|
90 |
+
|
91 |
+
# ----------------------------------------------------------------------
|
92 |
+
# Prompt builders
|
93 |
+
# ----------------------------------------------------------------------
|
94 |
+
def _make_prompt(base, bpm, drum, synth, steps, bass, gtr, def_bass, def_gtr, flow):
|
95 |
+
step_txt = f" with {steps}" if steps != "none" else flow.format(bpm=bpm)
|
96 |
+
drum_txt = f", {drum} drums" if drum != "none" else ""
|
97 |
+
synth_txt = f", {synth} accents" if synth != "none" else ""
|
98 |
+
bass_txt = f", {bass}" if bass != "none" else def_bass
|
99 |
+
gtr_txt = f", {gtr} guitar riffs" if gtr != "none" else def_gtr
|
100 |
+
return f"{base}{bass_txt}{gtr_txt}{drum_txt}{synth_txt}{step_txt} at {bpm} BPM."
|
101 |
+
|
102 |
+
def set_red_hot_chili_peppers_prompt(bpm, drum, synth, steps, bass, gtr):
|
103 |
+
return _make_prompt(
|
104 |
+
"Instrumental funk rock", bpm, drum, synth, steps, bass, gtr,
|
105 |
+
", groovy basslines", ", syncopated guitar riffs",
|
106 |
+
"{bpm} BPM funky flow" if bpm > 120 else "groovy rhythmic flow"
|
107 |
+
)
|
108 |
+
|
109 |
+
def set_nirvana_grunge_prompt(bpm, drum, synth, steps, bass, gtr):
|
110 |
+
return _make_prompt(
|
111 |
+
"Instrumental grunge", bpm, drum, synth, steps, bass, gtr,
|
112 |
+
", melodic basslines", ", raw distorted guitar riffs",
|
113 |
+
"{bpm} BPM grungy pulse" if bpm > 120 else "grungy rhythmic pulse"
|
114 |
+
)
|
115 |
+
|
116 |
+
def set_pearl_jam_grunge_prompt(bpm, drum, synth, steps, bass, gtr):
|
117 |
+
return _make_prompt(
|
118 |
+
"Instrumental grunge", bpm, drum, synth, steps, bass, gtr,
|
119 |
+
", deep bass", ", soulful guitar leads",
|
120 |
+
"{bpm} BPM driving flow" if bpm > 120 else "driving rhythmic flow"
|
121 |
+
)
|
122 |
+
|
123 |
+
def set_soundgarden_grunge_prompt(bpm, drum, synth, steps, bass, gtr):
|
124 |
+
return _make_prompt(
|
125 |
+
"Instrumental grunge", bpm, drum, synth, steps, bass, gtr,
|
126 |
+
"", ", heavy sludgy guitar riffs",
|
127 |
+
"{bpm} BPM heavy groove" if bpm > 120 else "sludgy rhythmic groove"
|
128 |
+
)
|
129 |
+
|
130 |
+
def set_foo_fighters_prompt(bpm, drum, synth, steps, bass, gtr):
|
131 |
+
styles = ["anthemic", "gritty", "melodic", "fast-paced", "driving"]
|
132 |
+
moods = ["energetic", "introspective", "rebellious", "uplifting"]
|
133 |
+
return (
|
134 |
+
_make_prompt(
|
135 |
+
"Instrumental alternative rock", bpm, drum, synth, steps, bass, gtr,
|
136 |
+
"", f", {random.choice(styles)} guitar riffs",
|
137 |
+
"{bpm} BPM powerful groove" if bpm > 120 else "catchy rhythmic groove"
|
138 |
+
)
|
139 |
+
+ f", Foo Fighters–inspired {random.choice(moods)} vibe"
|
140 |
+
)
|
141 |
+
|
142 |
+
def set_smashing_pumpkins_prompt(bpm, drum, synth, steps, bass, gtr):
|
143 |
+
return _make_prompt(
|
144 |
+
"Instrumental alternative rock", bpm, drum, synth, steps, bass, gtr,
|
145 |
+
"", ", dreamy guitar textures",
|
146 |
+
"{bpm} BPM dynamic flow" if bpm > 120 else "dreamy rhythmic flow"
|
147 |
+
)
|
148 |
+
|
149 |
+
def set_radiohead_prompt(bpm, drum, synth, steps, bass, gtr):
|
150 |
+
return _make_prompt(
|
151 |
+
"Instrumental experimental rock", bpm, drum, synth, steps, bass, gtr,
|
152 |
+
"", ", intricate guitar layers",
|
153 |
+
"{bpm} BPM intricate pulse" if bpm > 120 else "intricate rhythmic pulse"
|
154 |
+
)
|
155 |
+
|
156 |
+
def set_classic_rock_prompt(bpm, drum, synth, steps, bass, gtr):
|
157 |
+
return _make_prompt(
|
158 |
+
"Instrumental classic rock", bpm, drum, synth, steps, bass, gtr,
|
159 |
+
", groovy bass", ", bluesy electric guitars",
|
160 |
+
"{bpm} BPM bluesy steps" if bpm > 120 else "steady rhythmic groove"
|
161 |
+
)
|
162 |
+
|
163 |
+
def set_alternative_rock_prompt(bpm, drum, synth, steps, bass, gtr):
|
164 |
+
return _make_prompt(
|
165 |
+
"Instrumental alternative rock", bpm, drum, synth, steps, bass, gtr,
|
166 |
+
", melodic basslines", ", distorted guitar riffs",
|
167 |
+
"{bpm} BPM quirky steps" if bpm > 120 else "energetic rhythmic flow"
|
168 |
+
)
|
169 |
+
|
170 |
+
def set_post_punk_prompt(bpm, drum, synth, steps, bass, gtr):
|
171 |
+
return _make_prompt(
|
172 |
+
"Instrumental post-punk", bpm, drum, synth, steps, bass, gtr,
|
173 |
+
", driving basslines", ", jangly guitars",
|
174 |
+
"{bpm} BPM sharp steps" if bpm > 120 else "moody rhythmic pulse"
|
175 |
+
)
|
176 |
+
|
177 |
+
def set_indie_rock_prompt(bpm, drum, synth, steps, bass, gtr):
|
178 |
+
return _make_prompt(
|
179 |
+
"Instrumental indie rock", bpm, drum, synth, steps, bass, gtr,
|
180 |
+
"", ", jangly guitars",
|
181 |
+
"{bpm} BPM catchy steps" if bpm > 120 else "jangly rhythmic flow"
|
182 |
+
)
|
183 |
+
|
184 |
+
def set_funk_rock_prompt(bpm, drum, synth, steps, bass, gtr):
|
185 |
+
return _make_prompt(
|
186 |
+
"Instrumental funk rock", bpm, drum, synth, steps, bass, gtr,
|
187 |
+
", slap bass", ", funky guitar chords",
|
188 |
+
"{bpm} BPM aggressive steps" if bpm > 120 else "funky rhythmic groove"
|
189 |
+
)
|
190 |
+
|
191 |
+
def set_detroit_techno_prompt(bpm, drum, synth, steps, bass, gtr):
|
192 |
+
return _make_prompt(
|
193 |
+
"Instrumental Detroit techno", bpm, drum, synth, steps, bass, gtr,
|
194 |
+
", driving basslines", "",
|
195 |
+
"{bpm} BPM pulsing steps" if bpm > 120 else "deep rhythmic groove"
|
196 |
+
)
|
197 |
+
|
198 |
+
def set_deep_house_prompt(bpm, drum, synth, steps, bass, gtr):
|
199 |
+
return _make_prompt(
|
200 |
+
"Instrumental deep house", bpm, drum, synth, steps, bass, gtr,
|
201 |
+
", deep basslines", "",
|
202 |
+
"{bpm} BPM soulful steps" if bpm > 120 else "laid-back rhythmic flow"
|
203 |
+
)
|
204 |
+
|
205 |
+
# ----------------------------------------------------------------------
|
206 |
+
# Audio post-processing
|
207 |
+
# ----------------------------------------------------------------------
|
208 |
def apply_eq(seg: AudioSegment):
|
209 |
return seg.low_pass_filter(8000).high_pass_filter(80)
|
210 |
|
211 |
def apply_fade(seg: AudioSegment, fin=1000, fout=1000):
|
212 |
return seg.fade_in(fin).fade_out(fout)
|
213 |
|
214 |
+
# ----------------------------------------------------------------------
|
215 |
+
# Core generation function
|
216 |
+
# ----------------------------------------------------------------------
|
217 |
+
def generate_music(
|
218 |
+
prompt, cfg, top_k, top_p, temp,
|
219 |
+
total_len, chunk_len, crossfade,
|
220 |
+
bpm, drum, synth, steps, bass, gtr
|
221 |
+
):
|
222 |
if not prompt.strip():
|
223 |
+
return None, "⚠️ Prompt cannot be empty."
|
224 |
if not vram_ok():
|
225 |
+
return None, "⚠️ Insufficient VRAM."
|
226 |
+
|
227 |
+
total_len = int(total_len)
|
228 |
+
chunk_len = int(max(5, min(chunk_len, 15)))
|
229 |
+
n_chunks = max(1, total_len // chunk_len)
|
230 |
+
chunk_len = total_len / n_chunks
|
231 |
+
overlap = min(1.0, crossfade / 1000.0)
|
232 |
+
render_len = chunk_len + overlap
|
233 |
sr = musicgen.sample_rate
|
234 |
+
segments = []
|
235 |
|
236 |
torch.manual_seed(42)
|
237 |
np.random.seed(42)
|
238 |
|
239 |
+
t0 = time.time()
|
240 |
+
for i in range(n_chunks):
|
241 |
+
log_resources(f"Before chunk {i+1}")
|
242 |
musicgen.set_generation_params(
|
243 |
+
duration=render_len,
|
244 |
+
use_sampling=True,
|
245 |
+
top_k=top_k,
|
246 |
+
top_p=top_p,
|
247 |
+
temperature=temp,
|
248 |
+
cfg_coef=cfg
|
249 |
+
)
|
250 |
+
with torch.no_grad(), autocast():
|
251 |
+
audio = musicgen.generate([prompt], progress=False)[0]
|
252 |
+
|
253 |
+
audio = audio.cpu().to(torch.float32)
|
254 |
+
if audio.dim() == 1:
|
255 |
+
audio = torch.stack([audio, audio])
|
256 |
+
elif audio.shape[0] == 1:
|
257 |
+
audio = torch.cat([audio, audio], dim=0)
|
258 |
+
elif audio.shape[0] != 2:
|
259 |
+
audio = torch.cat([audio[:1], audio[:1]], dim=0)
|
260 |
+
|
261 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
262 |
+
torchaudio.save(tmp.name, audio, sr, bits_per_sample=24)
|
263 |
+
seg = AudioSegment.from_wav(tmp.name)
|
264 |
os.unlink(tmp.name)
|
265 |
+
segments.append(seg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
|
267 |
+
gpu_clean()
|
268 |
+
log_resources(f"After chunk {i+1}")
|
269 |
+
|
270 |
+
final = segments[0]
|
271 |
+
for seg in segments[1:]:
|
272 |
+
final = final.append(seg + 1, crossfade=crossfade)
|
273 |
+
final = final[: total_len * 1000]
|
274 |
+
final = apply_fade(apply_eq(final).normalize(headroom=-9.0))
|
275 |
+
|
276 |
+
out_path = "output_cleaned.mp3"
|
277 |
+
final.export(
|
278 |
+
out_path,
|
279 |
+
format="mp3",
|
280 |
+
bitrate="128k",
|
281 |
+
tags={"title": "GhostAI Instrumental", "artist": "GhostAI"}
|
282 |
+
)
|
283 |
+
log_resources("After final")
|
284 |
+
print(f"Total generation time: {time.time() - t0:.2f}s")
|
285 |
+
return out_path, "✅ Done!"
|
286 |
|
287 |
def clear_inputs():
|
288 |
+
return (
|
289 |
+
"", 3.0, 250, 0.9, 1.0,
|
290 |
+
30, 10, 1000,
|
291 |
+
120, "none", "none", "none", "none", "none"
|
292 |
+
)
|
293 |
+
|
294 |
+
# ----------------------------------------------------------------------
|
295 |
+
# Gradio UI
|
296 |
+
# ----------------------------------------------------------------------
|
297 |
+
css = """
|
298 |
+
body {
|
299 |
+
background: linear-gradient(135deg, #0A0A0A 0%, #1C2526 100%);
|
300 |
+
color: #E0E0E0; font-family: 'Orbitron', sans-serif;
|
301 |
+
}
|
302 |
+
.header {
|
303 |
+
text-align: center; padding: 10px; background: rgba(0,0,0,0.9);
|
304 |
+
border-bottom: 1px solid #00FF9F;
|
305 |
+
}
|
306 |
"""
|
307 |
|
308 |
with gr.Blocks(css=css) as demo:
|
309 |
+
gr.HTML('<div class="header"><h1>👻 GhostAI Music Generator</h1></div>')
|
310 |
+
|
311 |
+
prompt_box = gr.Textbox(label="Instrumental Prompt ✍️", lines=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
with gr.Row():
|
313 |
+
gr.Button("RHCP 🌶️").click(
|
314 |
+
set_red_hot_chili_peppers_prompt,
|
315 |
+
inputs=[gr.State(120), gr.State("none"), gr.State("none"),
|
316 |
+
gr.State("none"), gr.State("none"), gr.State("none")],
|
317 |
+
outputs=prompt_box
|
318 |
+
)
|
319 |
+
gr.Button("Nirvana 🎸").click(
|
320 |
+
set_nirvana_grunge_prompt,
|
321 |
+
inputs=[gr.State(120), gr.State("none"), gr.State("none"),
|
322 |
+
gr.State("none"), gr.State("none"), gr.State("none")],
|
323 |
+
outputs=prompt_box
|
324 |
+
)
|
325 |
+
# … add the other genre buttons in the same pattern …
|
326 |
+
|
327 |
+
with gr.Group():
|
328 |
+
cfg_scale = gr.Slider(1.0, 10.0, value=3.0, step=0.1, label="CFG Scale")
|
329 |
+
top_k = gr.Slider(10, 500, value=250, step=10, label="Top-K")
|
330 |
+
top_p = gr.Slider(0.0, 1.0, value=0.9, step=0.05, label="Top-P")
|
331 |
+
temperature = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="Temperature")
|
332 |
+
total_len = gr.Radio([30, 60, 90, 120], value=30, label="Length (s)")
|
333 |
+
chunk_len = gr.Slider(5, 15, value=10, step=1, label="Chunk (s)")
|
334 |
+
crossfade = gr.Slider(100, 2000, value=1000, step=100, label="Crossfade (ms)")
|
335 |
+
|
336 |
+
bpm = gr.Slider(60, 180, value=120, label="Tempo (BPM)")
|
337 |
+
drum_beat = gr.Dropdown(["none","standard rock","funk groove","techno kick","jazz swing"], value="none", label="Drum Beat")
|
338 |
+
synthesizer = gr.Dropdown(["none","analog synth","digital pad","arpeggiated synth"], value="none", label="Synthesizer")
|
339 |
+
steps = gr.Dropdown(["none","syncopated steps","steady steps","complex steps"], value="none", label="Rhythmic Steps")
|
340 |
+
bass_style = gr.Dropdown(["none","slap bass","deep bass","melodic bass"], value="none", label="Bass Style")
|
341 |
+
guitar_style = gr.Dropdown(["none","distorted","clean","jangle"], value="none", label="Guitar Style")
|
342 |
+
|
343 |
+
gen_btn = gr.Button("Generate Music 🚀")
|
344 |
+
clr_btn = gr.Button("Clear 🧹")
|
345 |
+
out_audio= gr.Audio(label="Generated Track", type="filepath")
|
346 |
+
status = gr.Textbox(label="Status", interactive=False)
|
347 |
+
|
348 |
+
gen_btn.click(
|
349 |
+
generate_music,
|
350 |
+
inputs=[
|
351 |
+
prompt_box, cfg_scale, top_k, top_p, temperature,
|
352 |
+
total_len, chunk_len, crossfade,
|
353 |
+
bpm, drum_beat, synthesizer, steps, bass_style, guitar_style
|
354 |
+
],
|
355 |
+
outputs=[out_audio, status]
|
356 |
+
)
|
357 |
+
clr_btn.click(
|
358 |
+
clear_inputs, None,
|
359 |
+
[
|
360 |
+
prompt_box, cfg_scale, top_k, top_p, temperature,
|
361 |
+
total_len, chunk_len, crossfade,
|
362 |
+
bpm, drum_beat, synthesizer, steps, bass_style, guitar_style
|
363 |
+
]
|
364 |
+
)
|
365 |
+
|
366 |
+
app = demo.launch(share=False, show_error=True)
|
367 |
try:
|
368 |
+
demo._server.app.docs_url = demo._server.app.redoc_url = demo._server.app.openapi_url = None
|
369 |
except Exception:
|
370 |
pass
|