File size: 1,121 Bytes
56798b8
2cb7990
b66115a
 
56798b8
 
 
b66115a
56798b8
 
 
 
 
 
2cb7990
56798b8
 
 
 
 
2cb7990
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import torch
import spaces
import gradio as gr

from transformers import MusicgenForConditionalGeneration
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
sampling_rate = model.config.audio_encoder.sampling_rate

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

from transformers import AutoProcessor
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")

@spaces.GPU
def generate_music(desc):
    inputs = processor(text=[desc], padding=True, return_tensors="pt")
    audio_values = model.generate(**inputs.to(device), do_sample=True, guidance_scale=3, max_new_tokens=256)
    return sampling_rate, audio_values[0][0].cpu().numpy()
    
# gr.Interface(fn=generate_music, inputs="text", outputs="audio").launch()

with gr.Blocks() as app:
    with gr.Row():
        music_desc = gr.TextArea(label="Music Description")
        music_player = gr.Audio(label="Play My Tune")

    gen_btn = gr.Button("Get Some Tune!!")
    gen_btn.click(fn=generate_music, inputs=[music_desc], outputs=[music_player])
    
if __name__ == '__main__':
    app.launch()