Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,39 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torchaudio
|
| 3 |
-
from transformers import MusicgenForConditionalGeneration,
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
def generate_music(prompt, melody):
|
| 9 |
if melody is None:
|
| 10 |
return None
|
| 11 |
|
| 12 |
-
# Load audio
|
| 13 |
-
|
| 14 |
-
if
|
| 15 |
-
resampler = torchaudio.transforms.Resample(orig_freq=
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return (audio_values[0].numpy(), model.config.audio_encoder.sampling_rate)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
fn=generate_music,
|
| 24 |
inputs=[
|
| 25 |
-
gr.Textbox(label="Prompt", placeholder="e.g.,
|
| 26 |
-
gr.Audio(source="upload", type="filepath", label="Melody Input (
|
| 27 |
],
|
| 28 |
-
outputs=gr.Audio(label="Generated
|
| 29 |
title="π΅ MusicGen-Melody AI Generator",
|
| 30 |
-
description="Upload a melody and describe the vibe
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
import torchaudio
|
| 4 |
+
from transformers import MusicgenForConditionalGeneration, MusicgenProcessor
|
| 5 |
|
| 6 |
+
# Load melody-capable model
|
| 7 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-melody", torch_dtype=torch.float32)
|
| 8 |
+
processor = MusicgenProcessor.from_pretrained("facebook/musicgen-melody")
|
| 9 |
|
| 10 |
def generate_music(prompt, melody):
|
| 11 |
if melody is None:
|
| 12 |
return None
|
| 13 |
|
| 14 |
+
# Load melody audio file
|
| 15 |
+
melody_waveform, melody_sr = torchaudio.load(melody)
|
| 16 |
+
if melody_sr != 16000:
|
| 17 |
+
resampler = torchaudio.transforms.Resample(orig_freq=melody_sr, new_freq=16000)
|
| 18 |
+
melody_waveform = resampler(melody_waveform)
|
| 19 |
|
| 20 |
+
# Trim or pad to 30 seconds
|
| 21 |
+
melody_waveform = melody_waveform[:, :16000 * 30]
|
|
|
|
| 22 |
|
| 23 |
+
inputs = processor(audio=melody_waveform, sampling_rate=16000, text=[prompt], return_tensors="pt")
|
| 24 |
+
outputs = model.generate(**inputs, max_new_tokens=1024)
|
| 25 |
+
audio_array = outputs[0].cpu().numpy()
|
| 26 |
+
return (audio_array, model.config.audio_encoder.sampling_rate)
|
| 27 |
+
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
fn=generate_music,
|
| 30 |
inputs=[
|
| 31 |
+
gr.Textbox(label="Prompt", placeholder="e.g., Funky jazz with synths"),
|
| 32 |
+
gr.Audio(source="upload", type="filepath", label="Melody Input (WAV or MP3)")
|
| 33 |
],
|
| 34 |
+
outputs=gr.Audio(label="Generated Track"),
|
| 35 |
title="π΅ MusicGen-Melody AI Generator",
|
| 36 |
+
description="Upload a melody and describe the vibe. Generates music using Metaβs MusicGen-Melody model."
|
| 37 |
)
|
| 38 |
|
| 39 |
+
demo.launch()
|