Spaces:
Runtime error
Runtime error
Commit
·
3c26e4b
1
Parent(s):
d3de81a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import MusicgenForConditionalGeneration, AutoProcessor, set_seed
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
6 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
7 |
+
|
8 |
+
device = "cuda:0"
|
9 |
+
model.to(device)
|
10 |
+
|
11 |
+
sample_rate = model.audio_encoder.config.sample_rate
|
12 |
+
|
13 |
+
|
14 |
+
def generate_audio(prompt, negative_prompt, guidance_scale=3, seed=0):
|
15 |
+
inputs = processor(
|
16 |
+
text=[prompt, negative_prompt],
|
17 |
+
padding=True,
|
18 |
+
return_tensors="pt",
|
19 |
+
).to(device)
|
20 |
+
|
21 |
+
with torch.no_grad():
|
22 |
+
encoder_outputs = text_encoder(**inputs)
|
23 |
+
|
24 |
+
set_seed(seed)
|
25 |
+
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)
|
26 |
+
|
27 |
+
audio_values = (audio_values.cpu().numpy() * 32767).astype(np.int16)
|
28 |
+
return (sample_rate, audio_values)
|
29 |
+
|
30 |
+
|
31 |
+
gr.Interface(
|
32 |
+
fn=generate_audio,
|
33 |
+
inputs=[
|
34 |
+
gr.Text(label="Prompt", value="80s pop track with synth and instrumentals"),
|
35 |
+
gr.Text(label="Negative prompt", value="drums"),
|
36 |
+
gr.Slider(1.5, 10, value=3, step=0.5, label="Guidance scale"),
|
37 |
+
gr.Slider(0, 10, value=0, step=1, label="Seed"),
|
38 |
+
],
|
39 |
+
outputs=[
|
40 |
+
gr.Audio(label="Generated Music", type="numpy"),
|
41 |
+
],
|
42 |
+
).launch()
|