Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import MusicGenForConditionalGeneration, MusicGenProcessor
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the MusicGen model and processor
|
6 |
+
model_name = "facebook/musicgen-small"
|
7 |
+
model = MusicGenForConditionalGeneration.from_pretrained(model_name)
|
8 |
+
processor = MusicGenProcessor.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Define a function to generate audio from text
|
11 |
+
def generate_audio(text):
|
12 |
+
# Tokenize the input text
|
13 |
+
inputs = processor(text, return_tensors="pt")
|
14 |
+
|
15 |
+
# Generate audio (samples)
|
16 |
+
with torch.no_grad():
|
17 |
+
generated_audio = model.generate_audio(**inputs)
|
18 |
+
|
19 |
+
# Save the generated audio
|
20 |
+
audio_path = "/tmp/generated_audio.wav"
|
21 |
+
generated_audio.squeeze().cpu().numpy().tofile(audio_path)
|
22 |
+
|
23 |
+
return audio_path
|
24 |
+
|
25 |
+
# Set up the Gradio interface
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=generate_audio,
|
28 |
+
inputs=gr.Textbox(label="Enter Text"),
|
29 |
+
outputs=gr.Audio(type="file", label="Generated Audio"),
|
30 |
+
title="Text-to-Audio Chatbot",
|
31 |
+
description="Enter a text prompt and get a music clip generated by the MusicGen model."
|
32 |
+
)
|
33 |
+
|
34 |
+
iface.launch()
|