Master0fNone commited on
Commit
87a8aff
·
verified ·
1 Parent(s): 0318775

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -1,24 +1,37 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- import soundfile as sf
4
- import io
5
- import numpy as np
6
 
7
- model = pipeline("text-to-music", model="facebook/musicgen-small")
 
 
8
 
9
- def generate_music(text_input):
10
- music_output = model(text_input)
11
 
12
- audio_buffer = io.BytesIO()
13
- sf.write(audio_buffer, music_output, 22050, format='WAV')
14
- audio_buffer.seek(0)
15
-
16
- return audio_buffer
 
17
 
 
 
 
 
 
 
18
  iface = gr.Interface(
19
- fn=generate_music,
20
- inputs=gr.Textbox(label="Enter Text for Music"),
21
- outputs=gr.Audio(label="Generated Music")
 
 
 
 
 
22
  )
23
 
24
- iface.launch()
 
 
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()