Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
-
# GhostAI Music Generator
|
3 |
import os, sys, gc, time, warnings, tempfile
|
4 |
import torch, torchaudio, numpy as np, gradio as gr
|
5 |
from pydub import AudioSegment
|
@@ -8,47 +8,44 @@ from huggingface_hub import login
|
|
8 |
|
9 |
warnings.filterwarnings("ignore")
|
10 |
|
11 |
-
# Hugging Face token authentication
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
13 |
if not HF_TOKEN:
|
14 |
sys.exit("ERROR: HF_TOKEN not set.")
|
15 |
login(HF_TOKEN)
|
16 |
|
17 |
-
# Simple GPU check suitable for Hugging Face
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
print(f"Running on {device.upper()}")
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
torch.cuda.empty_cache()
|
24 |
-
gc.collect()
|
25 |
-
|
26 |
-
clean_resources()
|
27 |
|
28 |
-
# Load MusicGen model explicitly on correct device
|
29 |
-
print("Loading MusicGen 'medium' model...")
|
30 |
musicgen = MusicGen.get_pretrained("medium")
|
31 |
musicgen.lm.to(device)
|
32 |
musicgen.set_generation_params(duration=10)
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
def generate_music(prompt, cfg, top_k, top_p, temp, total_len, chunk_len, crossfade):
|
36 |
if not prompt.strip():
|
37 |
return None, "⚠️ Enter a valid prompt."
|
38 |
|
39 |
-
|
|
|
|
|
40 |
chunks = max(1, total_len // chunk_len)
|
41 |
-
|
42 |
for _ in range(chunks):
|
43 |
with torch.no_grad():
|
44 |
audio = musicgen.generate(
|
45 |
[prompt],
|
46 |
-
progress=False,
|
47 |
temperature=temp,
|
48 |
cfg_coef=cfg,
|
49 |
top_k=top_k,
|
50 |
top_p=top_p,
|
51 |
-
duration=chunk_len
|
|
|
52 |
)[0].cpu().float()
|
53 |
|
54 |
if audio.dim() == 1:
|
@@ -57,14 +54,13 @@ def generate_music(prompt, cfg, top_k, top_p, temp, total_len, chunk_len, crossf
|
|
57 |
audio = audio.repeat(2, 1)
|
58 |
|
59 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
60 |
-
torchaudio.save(tmp.name, audio,
|
61 |
segment = AudioSegment.from_wav(tmp.name)
|
62 |
os.unlink(tmp.name)
|
63 |
segments.append(segment)
|
64 |
|
65 |
clean_resources()
|
66 |
|
67 |
-
# Concatenate audio segments
|
68 |
final = segments[0]
|
69 |
for seg in segments[1:]:
|
70 |
final = final.append(seg, crossfade=crossfade)
|
@@ -74,9 +70,8 @@ def generate_music(prompt, cfg, top_k, top_p, temp, total_len, chunk_len, crossf
|
|
74 |
out_path = "output_cleaned.mp3"
|
75 |
final.export(out_path, format="mp3", bitrate="128k", tags={"title": "GhostAI Track", "artist": "GhostAI"})
|
76 |
|
77 |
-
return out_path, "✅
|
78 |
|
79 |
-
# Simple Gradio Interface
|
80 |
demo = gr.Interface(
|
81 |
fn=generate_music,
|
82 |
inputs=[
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
+
# GhostAI Music Generator Hugging Face Spaces Version
|
3 |
import os, sys, gc, time, warnings, tempfile
|
4 |
import torch, torchaudio, numpy as np, gradio as gr
|
5 |
from pydub import AudioSegment
|
|
|
8 |
|
9 |
warnings.filterwarnings("ignore")
|
10 |
|
|
|
11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
if not HF_TOKEN:
|
13 |
sys.exit("ERROR: HF_TOKEN not set.")
|
14 |
login(HF_TOKEN)
|
15 |
|
|
|
16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
print(f"Running on {device.upper()}")
|
18 |
|
19 |
+
# Fix transformers compatibility manually
|
20 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
22 |
musicgen = MusicGen.get_pretrained("medium")
|
23 |
musicgen.lm.to(device)
|
24 |
musicgen.set_generation_params(duration=10)
|
25 |
|
26 |
+
def clean_resources():
|
27 |
+
if device == "cuda":
|
28 |
+
torch.cuda.empty_cache()
|
29 |
+
gc.collect()
|
30 |
+
|
31 |
def generate_music(prompt, cfg, top_k, top_p, temp, total_len, chunk_len, crossfade):
|
32 |
if not prompt.strip():
|
33 |
return None, "⚠️ Enter a valid prompt."
|
34 |
|
35 |
+
sample_rate = musicgen.sample_rate
|
36 |
+
segments = []
|
37 |
+
|
38 |
chunks = max(1, total_len // chunk_len)
|
|
|
39 |
for _ in range(chunks):
|
40 |
with torch.no_grad():
|
41 |
audio = musicgen.generate(
|
42 |
[prompt],
|
|
|
43 |
temperature=temp,
|
44 |
cfg_coef=cfg,
|
45 |
top_k=top_k,
|
46 |
top_p=top_p,
|
47 |
+
duration=chunk_len,
|
48 |
+
progress=False
|
49 |
)[0].cpu().float()
|
50 |
|
51 |
if audio.dim() == 1:
|
|
|
54 |
audio = audio.repeat(2, 1)
|
55 |
|
56 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
57 |
+
torchaudio.save(tmp.name, audio, sample_rate)
|
58 |
segment = AudioSegment.from_wav(tmp.name)
|
59 |
os.unlink(tmp.name)
|
60 |
segments.append(segment)
|
61 |
|
62 |
clean_resources()
|
63 |
|
|
|
64 |
final = segments[0]
|
65 |
for seg in segments[1:]:
|
66 |
final = final.append(seg, crossfade=crossfade)
|
|
|
70 |
out_path = "output_cleaned.mp3"
|
71 |
final.export(out_path, format="mp3", bitrate="128k", tags={"title": "GhostAI Track", "artist": "GhostAI"})
|
72 |
|
73 |
+
return out_path, "✅ Done!"
|
74 |
|
|
|
75 |
demo = gr.Interface(
|
76 |
fn=generate_music,
|
77 |
inputs=[
|