Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# MusicGen + Gradio + GPT Demo App (CPU-Optimized
|
2 |
|
3 |
import gradio as gr
|
4 |
import os
|
@@ -33,7 +33,6 @@ def refine_prompt(user_input):
|
|
33 |
# Generate music (shorter tokens for CPU speed)
|
34 |
def generate_music(prompt, max_new_tokens: int = 128):
|
35 |
inputs = processor(text=[prompt], return_tensors="pt").to(device)
|
36 |
-
# Warning: Generation on CPU may be slow
|
37 |
audio_values = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
38 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
39 |
audio = audio_values[0].cpu().numpy()
|
@@ -42,10 +41,14 @@ def generate_music(prompt, max_new_tokens: int = 128):
|
|
42 |
audio = audio / np.max(np.abs(audio))
|
43 |
audio = audio.astype(np.float32)
|
44 |
|
45 |
-
#
|
46 |
int_audio = (audio * 32767).astype(np.int16)
|
47 |
-
|
|
|
|
|
48 |
|
|
|
|
|
49 |
return sampling_rate, audio
|
50 |
|
51 |
# Combined Gradio function
|
@@ -55,7 +58,8 @@ def main(user_input, max_new_tokens):
|
|
55 |
return detailed_prompt, (sampling_rate, audio), "/tmp/output.wav"
|
56 |
|
57 |
# Build Gradio UI
|
58 |
-
|
|
|
59 |
gr.Markdown("""# π΅ AI Music Generator
|
60 |
Enter a music idea or mood and get a short AI-generated track. (CPU mode)""")
|
61 |
|
@@ -73,5 +77,9 @@ Enter a music idea or mood and get a short AI-generated track. (CPU mode)""")
|
|
73 |
outputs=[refined_output, audio_output, download_wav]
|
74 |
)
|
75 |
|
76 |
-
# Launch
|
77 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
# MusicGen + Gradio + GPT Demo App (CPU-Optimized with MCP Server)
|
2 |
|
3 |
import gradio as gr
|
4 |
import os
|
|
|
33 |
# Generate music (shorter tokens for CPU speed)
|
34 |
def generate_music(prompt, max_new_tokens: int = 128):
|
35 |
inputs = processor(text=[prompt], return_tensors="pt").to(device)
|
|
|
36 |
audio_values = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
37 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
38 |
audio = audio_values[0].cpu().numpy()
|
|
|
41 |
audio = audio / np.max(np.abs(audio))
|
42 |
audio = audio.astype(np.float32)
|
43 |
|
44 |
+
# Prepare int16 version and ensure 1D for WAV
|
45 |
int_audio = (audio * 32767).astype(np.int16)
|
46 |
+
int_audio = np.squeeze(int_audio)
|
47 |
+
if int_audio.ndim > 1:
|
48 |
+
int_audio = int_audio[:, 0]
|
49 |
|
50 |
+
# Save as .wav file (in /tmp for Spaces)
|
51 |
+
scipy.io.wavfile.write("/tmp/output.wav", sampling_rate, int_audio)
|
52 |
return sampling_rate, audio
|
53 |
|
54 |
# Combined Gradio function
|
|
|
58 |
return detailed_prompt, (sampling_rate, audio), "/tmp/output.wav"
|
59 |
|
60 |
# Build Gradio UI
|
61 |
+
demo = gr.Blocks()
|
62 |
+
with demo:
|
63 |
gr.Markdown("""# π΅ AI Music Generator
|
64 |
Enter a music idea or mood and get a short AI-generated track. (CPU mode)""")
|
65 |
|
|
|
77 |
outputs=[refined_output, audio_output, download_wav]
|
78 |
)
|
79 |
|
80 |
+
# Launch with Gradio MCP Server
|
81 |
+
from gradio.mcp_server import MCPServer
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
server = MCPServer(demo, host="0.0.0.0", port=7860)
|
85 |
+
server.run()
|