File size: 1,399 Bytes
3c26e4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
from transformers import MusicgenForConditionalGeneration, AutoProcessor, set_seed
import torch
import gradio as gr

model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")

device = "cuda:0"
model.to(device)

sample_rate = model.audio_encoder.config.sample_rate


def generate_audio(prompt, negative_prompt, guidance_scale=3, seed=0):
    inputs = processor(
        text=[prompt, negative_prompt],
        padding=True,
        return_tensors="pt",
        ).to(device)

    with torch.no_grad():
        encoder_outputs = text_encoder(**inputs)

    set_seed(seed)
    audio_values = model.generate(inputs.input_ids[0][None, :], attention_mask=inputs.attention_mask, encoder_outputs=encoder_outputs, do_sample=True, guidance_scale=guidance_scale, max_new_tokens=1028)

    audio_values = (audio_values.cpu().numpy() * 32767).astype(np.int16)
    return (sample_rate, audio_values)


gr.Interface(
    fn=generate_audio,
    inputs=[
        gr.Text(label="Prompt", value="80s pop track with synth and instrumentals"),
        gr.Text(label="Negative prompt", value="drums"),
        gr.Slider(1.5, 10, value=3, step=0.5, label="Guidance scale"),
        gr.Slider(0, 10, value=0, step=1, label="Seed"),
    ],
    outputs=[
        gr.Audio(label="Generated Music", type="numpy"),
    ],
).launch()