Dannyar608 commited on
Commit
7edab61
·
verified ·
1 Parent(s): 8486c25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -29
app.py CHANGED
@@ -1,41 +1,44 @@
1
- import gradio as gr
2
- from transformers import MusicGenForConditionalGeneration, MusicGenProcessor
3
- import torch
4
- import soundfile as sf
 
 
 
 
5
 
6
- from transformers import AutoModel, AutoProcessor
7
 
8
- # Use AutoModel and AutoProcessor to automatically select the correct model and processor
9
- model_name = "facebook/musicgen-small"
10
- model = AutoModel.from_pretrained(model_name)
11
- processor = AutoProcessor.from_pretrained(model_name)
12
 
13
- # Define a function to generate audio from text
14
- def generate_audio(text):
15
- # Tokenize the input text
16
- inputs = processor(text, return_tensors="pt")
17
 
18
- # Generate audio (samples)
19
- with torch.no_grad():
20
- generated_audio = model.generate(**inputs)
21
 
22
- # Convert tensor to numpy and save as a WAV file
23
- audio_path = "/tmp/generated_audio.wav"
24
- audio_data = generated_audio[0].cpu().numpy() # Access the first sample
25
 
26
- # Save the generated audio
27
- sf.write(audio_path, audio_data, 16000) # Assuming a sample rate of 16kHz
 
 
 
 
 
 
 
28
 
29
- return audio_path
30
 
31
- # Set up the Gradio interface
32
  iface = gr.Interface(
33
- fn=generate_audio,
34
- inputs=gr.Textbox(label="Enter Text"),
35
- outputs=gr.Audio(type="file", label="Generated Audio"),
36
- title="Text-to-Audio Chatbot",
37
- description="Enter a text prompt and get a music clip generated by the MusicGen model."
 
38
  )
39
 
40
- iface.launch()
41
 
 
 
1
+ import tempfile
2
+ from audiocraft.models import MusicGen
3
+ from audiocraft.data.audio import audio_write
4
+ import gradio as gr
5
+ import torch
6
+ import uuid
7
+ import os
8
+ from scipy.io.wavfile import write
9
 
 
10
 
11
+ model = MusicGen.get_pretrained("facebook/musicgen-small")
12
+ model.set_generation_params(duration=5)
 
 
13
 
14
+ def generate_music(description):
15
+ wav = model.generate([description])
16
+ audio_array = wav.cpu().numpy().squeeze()
17
+ sample_rate = model.sample_rate
18
 
 
 
 
19
 
20
+ file_id = uuid.uuid1()
 
 
21
 
22
+ file_path = os.path.join(
23
+ tempfile.gettempdir(),
24
+ f'{file_id}.wav'
25
+ )
26
+
27
+ print(f"Temporary directory: {tempfile.gettempdir()}")
28
+ print(f"File path: {file_path}")
29
+
30
+ write(file_path, rate=sample_rate, data=audio_array)
31
 
32
+ return file_path
33
 
 
34
  iface = gr.Interface(
35
+ fn=generate_music,
36
+ inputs="text",
37
+ outputs=gr.components.Audio(type="filepath", label="Audio"),
38
+ title="Text to Audio Generation",
39
+ description="Generate audio based on text descriptions.",
40
+ live=False,
41
  )
42
 
 
43
 
44
+ iface.launch(debug=True)