sanchit-gandhi's picture
Create app.py
3c26e4b
raw
history blame
1.4 kB
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()