Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import io
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
iface = gr.Interface(
|
| 19 |
-
fn=
|
| 20 |
-
inputs=
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load Meta's AudioGen model
|
| 6 |
+
model_id = "facebook/musicgen-small"
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 10 |
+
model = MusicgenForConditionalGeneration.from_pretrained(model_id).to(device)
|
| 11 |
|
| 12 |
+
def generate_audio(text_prompt, duration=5):
|
| 13 |
+
"""Generate music based on a text prompt."""
|
| 14 |
+
inputs = processor(
|
| 15 |
+
text=[text_prompt],
|
| 16 |
+
return_tensors="pt"
|
| 17 |
+
).to(device)
|
| 18 |
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
audio_waveform = model.generate(**inputs, max_new_tokens=duration * 50)
|
| 21 |
+
|
| 22 |
+
return audio_waveform[0].cpu().numpy(), 32000 # Sample rate
|
| 23 |
+
|
| 24 |
+
# Gradio UI
|
| 25 |
iface = gr.Interface(
|
| 26 |
+
fn=generate_audio,
|
| 27 |
+
inputs=[
|
| 28 |
+
gr.Textbox(label="Music Prompt", placeholder="e.g., A calm piano melody"),
|
| 29 |
+
gr.Slider(1, 30, value=5, step=1, label="Duration (seconds)")
|
| 30 |
+
],
|
| 31 |
+
outputs=gr.Audio(label="Generated Music"),
|
| 32 |
+
title="Text-to-Music Generator",
|
| 33 |
+
description="Enter a text prompt to generate music using Meta's MusicGen model.",
|
| 34 |
)
|
| 35 |
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|