MadhavManoj commited on
Commit
e115715
·
verified ·
1 Parent(s): c3dc362

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -1,21 +1,32 @@
 
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # Load the MusicGen model
5
- musicgen = pipeline("text-to-audio", model="facebook/musicgen-small")
 
6
 
7
- def text_to_music(prompt):
8
- output = musicgen(prompt)
9
- return output["audio"]
 
 
10
 
11
- # Create Gradio interface
 
 
 
 
 
12
  iface = gr.Interface(
13
- fn=text_to_music,
14
- inputs=gr.Textbox(label="Enter a music description"),
15
  outputs=gr.Audio(label="Generated Music"),
16
  title="AI Music Generator",
17
- description="Enter a description (e.g., 'happy jazz melody') and generate AI music."
18
  )
19
 
20
  if __name__ == "__main__":
21
  iface.launch()
 
 
1
+ import torch
2
  import gradio as gr
3
+ from transformers import AutoProcessor, MusicgenForConditionalGeneration
4
+ import scipy
5
 
6
+ device = "cpu"
7
+ model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-stereo-32khz").to(device)
8
+ processor = AutoProcessor.from_pretrained("facebook/musicgen-stereo-32khz")
9
 
10
+ def generate_music(prompt):
11
+ inputs = processor(text=[prompt], return_tensors="pt").to(device)
12
+ audio_values = model.generate(**inputs, max_new_tokens=256) # Adjust length
13
+ audio_array = audio_values[0].cpu().numpy()
14
+ sample_rate = model.config.audio_encoder.sampling_rate
15
 
16
+ # Save as WAV file
17
+ output_path = "generated_music.wav"
18
+ scipy.io.wavfile.write(output_path, sample_rate, audio_array.T)
19
+ return output_path
20
+
21
+ # Gradio UI
22
  iface = gr.Interface(
23
+ fn=generate_music,
24
+ inputs=gr.Textbox(label="Enter a music prompt (e.g., 'calm piano melody')"),
25
  outputs=gr.Audio(label="Generated Music"),
26
  title="AI Music Generator",
27
+ description="Describe the music style and let AI generate it."
28
  )
29
 
30
  if __name__ == "__main__":
31
  iface.launch()
32
+