Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
# Commented out IPython magic to ensure Python compatibility.
|
4 |
+
# %%capture
|
5 |
+
# ! pip install git+https://github.com/facebookresearch/audiocraft
|
6 |
+
# ! pip install torchvision==0.16.0
|
7 |
+
# ! pip install hf-transfer
|
8 |
+
# ! pip install gradio
|
9 |
+
|
10 |
+
import os
|
11 |
+
import gradio as gr
|
12 |
+
import torchaudio
|
13 |
+
from audiocraft.models import MusicGen
|
14 |
+
from audiocraft.data.audio import audio_write
|
15 |
+
from huggingface_hub import hf_hub_download
|
16 |
+
|
17 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
18 |
+
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
19 |
+
|
20 |
+
def generate_music(description, melody_path):
|
21 |
+
model.set_generation_params(duration=5)
|
22 |
+
wav = model.generate([description])
|
23 |
+
for idx, one_wav in enumerate(wav):
|
24 |
+
audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
25 |
+
return f'{idx}.wav'
|
26 |
+
|
27 |
+
def remix_music(description, melody_path):
|
28 |
+
melody, sr = torchaudio.load(melody_path)
|
29 |
+
wav = model.generate_with_chroma([description], melody[None].expand(1, -1, -1), sr)
|
30 |
+
for idx, one_wav in enumerate(wav):
|
31 |
+
audio_write(f'{idx}_bach', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
32 |
+
return f'{idx}_bach.wav'
|
33 |
+
|
34 |
+
examples = [
|
35 |
+
["acoustic, guitar, melody, trap, d minor, 90 bpm", None],
|
36 |
+
["piano, jazz, upbeat, c major, 120 bpm"],
|
37 |
+
["cinematic, orchestra, epic", None]
|
38 |
+
]
|
39 |
+
|
40 |
+
demo = gr.Interface(
|
41 |
+
fn=lambda description, melody_path: remix_music(description, melody_path) if melody_path else generate_music(description, None),
|
42 |
+
inputs=[
|
43 |
+
gr.Textbox(label="Enter Description"),
|
44 |
+
gr.File(label="Upload Melody")
|
45 |
+
],
|
46 |
+
outputs=gr.Audio(label="Generated Music"),
|
47 |
+
examples=examples,
|
48 |
+
title="Music Generation SongStarter App"
|
49 |
+
)
|
50 |
+
|
51 |
+
demo.launch(debug=True)
|