Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,29 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
"""
|
3 |
-
GhostAI Music Generator
|
4 |
-
Streams facebook/musicgen-medium
|
|
|
|
|
5 |
"""
|
6 |
|
7 |
-
#
|
8 |
-
import spaces
|
9 |
|
10 |
-
#
|
11 |
-
import os, sys, gc, time,
|
12 |
import numpy as np, psutil
|
13 |
|
14 |
-
#
|
15 |
import torch, torchaudio
|
16 |
|
17 |
-
#
|
18 |
import gradio as gr
|
19 |
from pydub import AudioSegment
|
20 |
from audiocraft.models import MusicGen
|
21 |
from huggingface_hub import login
|
22 |
from torch.cuda.amp import autocast
|
23 |
|
24 |
-
#
|
25 |
-
# Torch <2.3 shim (transformers may call torch.get_default_device)
|
26 |
if not hasattr(torch, "get_default_device"):
|
27 |
torch.get_default_device = lambda: torch.device(
|
28 |
"cuda" if torch.cuda.is_available() else "cpu"
|
@@ -31,32 +32,27 @@ if not hasattr(torch, "get_default_device"):
|
|
31 |
warnings.filterwarnings("ignore")
|
32 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
33 |
|
34 |
-
#
|
35 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
36 |
if not HF_TOKEN:
|
37 |
-
sys.exit("ERROR:
|
38 |
login(HF_TOKEN)
|
39 |
|
40 |
-
|
41 |
-
print(f"ZeroGPU detected โ initial device is {device.upper()}")
|
42 |
-
|
43 |
-
# ๐ฅ Download model from Hub
|
44 |
-
print("Loading facebook/musicgen-medium โฆ (first run may take ~6 GB download)")
|
45 |
musicgen = MusicGen.get_pretrained("facebook/musicgen-medium")
|
|
|
46 |
musicgen.set_generation_params(duration=10, two_step_cfg=False)
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
# โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
|
53 |
-
def _p(base,bpm,dr,syn,st,bass,gtr,dflt_bass,dflt_gtr,flow):
|
54 |
step = f" with {st}" if st!="none" else flow.format(bpm=bpm)
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
def set_red_hot_chili_peppers_prompt(bpm,dr,syn,st,bass,gtr):
|
62 |
return _p("Instrumental funk rock",bpm,dr,syn,st,bass,gtr,
|
@@ -66,25 +62,22 @@ def set_nirvana_grunge_prompt(bpm,dr,syn,st,bass,gtr):
|
|
66 |
return _p("Instrumental grunge",bpm,dr,syn,st,bass,gtr,
|
67 |
", melodic basslines",", raw distorted guitar riffs",
|
68 |
"{bpm} BPM grungy pulse" if bpm>120 else "grungy rhythmic pulse")
|
69 |
-
#
|
70 |
|
71 |
-
# Audio FX
|
72 |
def apply_eq(s): return s.low_pass_filter(8000).high_pass_filter(80)
|
73 |
def apply_fade(s): return s.fade_in(1000).fade_out(1000)
|
74 |
|
75 |
def log(stage=""):
|
76 |
if stage: print(f"โโ {stage} โโ")
|
77 |
if torch.cuda.is_available():
|
78 |
-
|
79 |
-
|
80 |
-
print(f"GPU mem alloc {
|
81 |
print(f"CPU mem {psutil.virtual_memory().percent}% used")
|
82 |
|
83 |
-
|
84 |
-
#
|
85 |
-
# โ Core generator โ wrapped with @spaces.GPU โ
|
86 |
-
# โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
|
87 |
-
@spaces.GPU
|
88 |
def generate_music(prompt,cfg,k,p,temp,
|
89 |
total_len,chunk_len,xfade,
|
90 |
bpm,dr,syn,step,bass,gtr):
|
@@ -92,36 +85,35 @@ def generate_music(prompt,cfg,k,p,temp,
|
|
92 |
if not prompt.strip():
|
93 |
return None, "โ ๏ธ Prompt is empty."
|
94 |
|
95 |
-
total_len
|
96 |
-
chunk_len
|
97 |
-
n_chunks
|
98 |
-
chunk_len
|
99 |
-
overlap
|
100 |
-
|
101 |
-
pieces
|
102 |
|
103 |
torch.manual_seed(42); np.random.seed(42)
|
104 |
-
|
105 |
t0 = time.time()
|
|
|
106 |
for i in range(n_chunks):
|
107 |
log(f"before chunk {i+1}")
|
108 |
musicgen.set_generation_params(
|
109 |
-
duration=
|
110 |
top_k=k, top_p=p, temperature=temp, cfg_coef=cfg
|
111 |
)
|
112 |
with torch.no_grad(), autocast():
|
113 |
audio = musicgen.generate([prompt], progress=False)[0]
|
114 |
|
115 |
audio = audio.cpu().float()
|
116 |
-
if audio.dim()==1
|
117 |
-
|
118 |
-
elif audio.shape[0]!=2: audio = audio[:1].repeat(2,1)
|
119 |
-
|
120 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
121 |
-
torchaudio.save(tmp.name, audio,
|
122 |
seg = AudioSegment.from_wav(tmp.name)
|
123 |
os.unlink(tmp.name)
|
124 |
pieces.append(seg)
|
|
|
125 |
torch.cuda.empty_cache(); gc.collect()
|
126 |
log(f"after chunk {i+1}")
|
127 |
|
@@ -131,25 +123,22 @@ def generate_music(prompt,cfg,k,p,temp,
|
|
131 |
track = track[: total_len*1000]
|
132 |
track = apply_fade(apply_eq(track).normalize(headroom=-9.0))
|
133 |
|
134 |
-
|
135 |
-
track.export(
|
136 |
tags={"title":"GhostAI Track","artist":"GhostAI"})
|
137 |
-
log("final"); print(f"
|
138 |
-
return
|
139 |
|
140 |
def clear_inputs():
|
141 |
return ("",3.0,250,0.9,1.0,30,10,1000,
|
142 |
120,"none","none","none","none","none")
|
143 |
|
144 |
-
#
|
145 |
-
# โ Gradio Blocks UI with your CSS & controls โ
|
146 |
-
# โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
|
147 |
css = "body{background:#0A0A0A;color:#E0E0E0;font-family:'Orbitron',sans-serif}"
|
148 |
|
149 |
with gr.Blocks(css=css) as demo:
|
150 |
gr.HTML("<h1 style='text-align:center'>๐ป GhostAI Music Generator</h1>")
|
151 |
-
prompt = gr.Textbox(label="Prompt"
|
152 |
-
|
153 |
with gr.Row():
|
154 |
gr.Button("RHCP ๐ถ๏ธ").click(set_red_hot_chili_peppers_prompt,
|
155 |
inputs=[gr.State(120),"none","none","none","none","none"],
|
@@ -157,13 +146,13 @@ with gr.Blocks(css=css) as demo:
|
|
157 |
gr.Button("Nirvana ๐ธ").click(set_nirvana_grunge_prompt,
|
158 |
inputs=[gr.State(120),"none","none","none","none","none"],
|
159 |
outputs=prompt)
|
160 |
-
# add
|
161 |
|
162 |
-
cfg = gr.Slider(1,10,3,label="CFG")
|
163 |
top_k = gr.Slider(10,500,250,step=10,label="Top-K")
|
164 |
top_p = gr.Slider(0,1,0.9,step=0.05,label="Top-P")
|
165 |
temp = gr.Slider(0.1,2,1,step=0.1,label="Temperature")
|
166 |
-
length= gr.Radio([30,60,90,120],value=30,label="Length (s)")
|
167 |
chunk = gr.Slider(5,15,10,step=1,label="Chunk (s)")
|
168 |
xfade = gr.Slider(100,2000,1000,step=100,label="Cross-fade (ms)")
|
169 |
|
@@ -174,17 +163,17 @@ with gr.Blocks(css=css) as demo:
|
|
174 |
bass = gr.Dropdown(["none","slap bass","deep bass","melodic bass"],"none","Bass")
|
175 |
gtr = gr.Dropdown(["none","distorted","clean","jangle"],"none","Guitar")
|
176 |
|
177 |
-
gen = gr.Button("Generate
|
178 |
clr = gr.Button("Clear ๐งน")
|
179 |
-
|
180 |
-
status= gr.Textbox(interactive=False)
|
181 |
|
182 |
gen.click(generate_music,
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
clr.click(clear_inputs,None,
|
187 |
-
|
188 |
-
|
189 |
|
190 |
demo.launch(share=False)
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
"""
|
3 |
+
GhostAI Music Generator โ ZeroGPU Edition
|
4 |
+
โข Streams facebook/musicgen-medium from the Hub using your HF_TOKEN
|
5 |
+
โข Runs on a transient GPU provided by the ZeroGPU scheduler
|
6 |
+
โข Keeps your genre prompt builders, EQ / fade FX, neon Gradio UI
|
7 |
"""
|
8 |
|
9 |
+
# โโ ZeroGPU decorator must be imported *before* torch/CUDA โโโโโโโโโโ
|
10 |
+
import spaces # ๐ FIRST import
|
11 |
|
12 |
+
# โโ Standard libs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
13 |
+
import os, sys, gc, time, random, warnings, tempfile
|
14 |
import numpy as np, psutil
|
15 |
|
16 |
+
# โโ Torch (CPU wheels; ZeroGPU moves tensors to the temp GPU) โโโโโโโ
|
17 |
import torch, torchaudio
|
18 |
|
19 |
+
# โโ Other deps โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
20 |
import gradio as gr
|
21 |
from pydub import AudioSegment
|
22 |
from audiocraft.models import MusicGen
|
23 |
from huggingface_hub import login
|
24 |
from torch.cuda.amp import autocast
|
25 |
|
26 |
+
# โโ Torch <2.3 shim (transformers 4.35 calls get_default_device) โโโ
|
|
|
27 |
if not hasattr(torch, "get_default_device"):
|
28 |
torch.get_default_device = lambda: torch.device(
|
29 |
"cuda" if torch.cuda.is_available() else "cpu"
|
|
|
32 |
warnings.filterwarnings("ignore")
|
33 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
34 |
|
35 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโ HF authentication โโโโโโโโโโโโโโโโโโโโโโ
|
36 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
37 |
if not HF_TOKEN:
|
38 |
+
sys.exit("ERROR: add HF_TOKEN as a secret in your Space settings.")
|
39 |
login(HF_TOKEN)
|
40 |
|
41 |
+
print("๐ Loading facebook/musicgen-medium (first build โ 6 GB)โฆ")
|
|
|
|
|
|
|
|
|
42 |
musicgen = MusicGen.get_pretrained("facebook/musicgen-medium")
|
43 |
+
musicgen.to(torch.get_default_device()) # move entire model
|
44 |
musicgen.set_generation_params(duration=10, two_step_cfg=False)
|
45 |
+
SR = musicgen.sample_rate
|
|
|
46 |
|
47 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโ Prompt helpers โโโโโโโโโโโโโโโโโโโโโโโโโ
|
48 |
+
def _p(base,bpm,dr,syn,st,bass,gtr,db,dg,flow):
|
|
|
|
|
49 |
step = f" with {st}" if st!="none" else flow.format(bpm=bpm)
|
50 |
+
return (f"{base}"
|
51 |
+
f"{', '+bass if bass!='none' else db}"
|
52 |
+
f"{', '+gtr+' guitar riffs' if gtr!='none' else dg}"
|
53 |
+
f"{', '+dr+' drums' if dr!='none' else ''}"
|
54 |
+
f"{', '+syn+' accents' if syn!='none' else ''}"
|
55 |
+
f"{step} at {bpm} BPM.")
|
56 |
|
57 |
def set_red_hot_chili_peppers_prompt(bpm,dr,syn,st,bass,gtr):
|
58 |
return _p("Instrumental funk rock",bpm,dr,syn,st,bass,gtr,
|
|
|
62 |
return _p("Instrumental grunge",bpm,dr,syn,st,bass,gtr,
|
63 |
", melodic basslines",", raw distorted guitar riffs",
|
64 |
"{bpm} BPM grungy pulse" if bpm>120 else "grungy rhythmic pulse")
|
65 |
+
# ๐ add your other genre prompt functions here exactly as before โฆ
|
66 |
|
67 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโ Audio FX helpers โโโโโโโโโโโโโโโโโโโโโโโ
|
68 |
def apply_eq(s): return s.low_pass_filter(8000).high_pass_filter(80)
|
69 |
def apply_fade(s): return s.fade_in(1000).fade_out(1000)
|
70 |
|
71 |
def log(stage=""):
|
72 |
if stage: print(f"โโ {stage} โโ")
|
73 |
if torch.cuda.is_available():
|
74 |
+
a = torch.cuda.memory_allocated()/1024**3
|
75 |
+
r = torch.cuda.memory_reserved()/1024**3
|
76 |
+
print(f"GPU mem alloc {a:.2f} GB reserved {r:.2f} GB")
|
77 |
print(f"CPU mem {psutil.virtual_memory().percent}% used")
|
78 |
|
79 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโ Core generator API โโโโโโโโโโโโโโโโโโโโโโโ
|
80 |
+
@spaces.GPU # ๐ข ZeroGPU will allocate a GPU per call
|
|
|
|
|
|
|
81 |
def generate_music(prompt,cfg,k,p,temp,
|
82 |
total_len,chunk_len,xfade,
|
83 |
bpm,dr,syn,step,bass,gtr):
|
|
|
85 |
if not prompt.strip():
|
86 |
return None, "โ ๏ธ Prompt is empty."
|
87 |
|
88 |
+
total_len = int(total_len)
|
89 |
+
chunk_len = max(5, min(int(chunk_len), 15))
|
90 |
+
n_chunks = max(1, total_len // chunk_len)
|
91 |
+
chunk_len = total_len / n_chunks
|
92 |
+
overlap = min(1.0, xfade / 1000.0)
|
93 |
+
render_len = chunk_len + overlap
|
94 |
+
pieces = []
|
95 |
|
96 |
torch.manual_seed(42); np.random.seed(42)
|
|
|
97 |
t0 = time.time()
|
98 |
+
|
99 |
for i in range(n_chunks):
|
100 |
log(f"before chunk {i+1}")
|
101 |
musicgen.set_generation_params(
|
102 |
+
duration=render_len, use_sampling=True,
|
103 |
top_k=k, top_p=p, temperature=temp, cfg_coef=cfg
|
104 |
)
|
105 |
with torch.no_grad(), autocast():
|
106 |
audio = musicgen.generate([prompt], progress=False)[0]
|
107 |
|
108 |
audio = audio.cpu().float()
|
109 |
+
if audio.dim()==1 or audio.shape[0]==1:
|
110 |
+
audio = audio.repeat(2,1)
|
|
|
|
|
111 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
112 |
+
torchaudio.save(tmp.name, audio, SR)
|
113 |
seg = AudioSegment.from_wav(tmp.name)
|
114 |
os.unlink(tmp.name)
|
115 |
pieces.append(seg)
|
116 |
+
|
117 |
torch.cuda.empty_cache(); gc.collect()
|
118 |
log(f"after chunk {i+1}")
|
119 |
|
|
|
123 |
track = track[: total_len*1000]
|
124 |
track = apply_fade(apply_eq(track).normalize(headroom=-9.0))
|
125 |
|
126 |
+
out_path = "output_cleaned.mp3"
|
127 |
+
track.export(out_path, format="mp3", bitrate="128k",
|
128 |
tags={"title":"GhostAI Track","artist":"GhostAI"})
|
129 |
+
log("final"); print(f"โฑ {time.time()-t0:.1f}s total")
|
130 |
+
return out_path, "โ
Done!"
|
131 |
|
132 |
def clear_inputs():
|
133 |
return ("",3.0,250,0.9,1.0,30,10,1000,
|
134 |
120,"none","none","none","none","none")
|
135 |
|
136 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโ Gradio UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
137 |
css = "body{background:#0A0A0A;color:#E0E0E0;font-family:'Orbitron',sans-serif}"
|
138 |
|
139 |
with gr.Blocks(css=css) as demo:
|
140 |
gr.HTML("<h1 style='text-align:center'>๐ป GhostAI Music Generator</h1>")
|
141 |
+
prompt = gr.Textbox(lines=4, label="Instrumental Prompt")
|
|
|
142 |
with gr.Row():
|
143 |
gr.Button("RHCP ๐ถ๏ธ").click(set_red_hot_chili_peppers_prompt,
|
144 |
inputs=[gr.State(120),"none","none","none","none","none"],
|
|
|
146 |
gr.Button("Nirvana ๐ธ").click(set_nirvana_grunge_prompt,
|
147 |
inputs=[gr.State(120),"none","none","none","none","none"],
|
148 |
outputs=prompt)
|
149 |
+
# ๐ add remaining genre buttons here โฆ
|
150 |
|
151 |
+
cfg = gr.Slider(1,10,3,label="CFG Scale")
|
152 |
top_k = gr.Slider(10,500,250,step=10,label="Top-K")
|
153 |
top_p = gr.Slider(0,1,0.9,step=0.05,label="Top-P")
|
154 |
temp = gr.Slider(0.1,2,1,step=0.1,label="Temperature")
|
155 |
+
length= gr.Radio([30,60,90,120], value=30, label="Length (s)")
|
156 |
chunk = gr.Slider(5,15,10,step=1,label="Chunk (s)")
|
157 |
xfade = gr.Slider(100,2000,1000,step=100,label="Cross-fade (ms)")
|
158 |
|
|
|
163 |
bass = gr.Dropdown(["none","slap bass","deep bass","melodic bass"],"none","Bass")
|
164 |
gtr = gr.Dropdown(["none","distorted","clean","jangle"],"none","Guitar")
|
165 |
|
166 |
+
gen = gr.Button("Generate ๐")
|
167 |
clr = gr.Button("Clear ๐งน")
|
168 |
+
audio_out = gr.Audio(type="filepath")
|
169 |
+
status = gr.Textbox(interactive=False)
|
170 |
|
171 |
gen.click(generate_music,
|
172 |
+
inputs=[prompt,cfg,top_k,top_p,temp,length,chunk,xfade,
|
173 |
+
bpm,drum,synth,steps,bass,gtr],
|
174 |
+
outputs=[audio_out,status])
|
175 |
clr.click(clear_inputs,None,
|
176 |
+
[prompt,cfg,top_k,top_p,temp,length,chunk,xfade,
|
177 |
+
bpm,drum,synth,steps,bass,gtr])
|
178 |
|
179 |
demo.launch(share=False)
|