Spaces:
Runtime error
Runtime error
Hua Ruochen
commited on
Commit
•
56798b8
1
Parent(s):
b66115a
basic music generation
Browse files- app.py +17 -4
- requirements.txt +4 -0
app.py
CHANGED
@@ -1,7 +1,20 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
import gradio as gr
|
3 |
|
4 |
+
from transformers import MusicgenForConditionalGeneration
|
5 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
6 |
+
sampling_rate = model.config.audio_encoder.sampling_rate
|
7 |
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
model.to(device)
|
10 |
+
|
11 |
+
from transformers import AutoProcessor
|
12 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
13 |
+
|
14 |
+
|
15 |
+
def generate_music(desc):
|
16 |
+
inputs = processor(text=[desc], padding=True, return_tensors="pt")
|
17 |
+
audio_values = model.generate(**inputs.to(device), do_sample=True, guidance_scale=3, max_new_tokens=256)
|
18 |
+
return sampling_rate, audio_values[0][0].cpu().numpy()
|
19 |
+
|
20 |
+
gr.Interface(fn=generate_music, inputs="text", outputs="audio").launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
torchvision
|
4 |
+
torchaudio
|