Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from audiocraft.data.audio import audio_write
|
| 3 |
+
import audiocraft.models
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import os
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# download models
|
| 10 |
+
huggingface_hub.hf_hub_download(
|
| 11 |
+
repo_id='Cyan0731/MusiConGen',
|
| 12 |
+
filename='compression_state_dict.bin',
|
| 13 |
+
local_dir='./audiocraft/ckpt/musicongen'
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
huggingface_hub.hf_hub_download(
|
| 17 |
+
repo_id='Cyan0731/MusiConGen',
|
| 18 |
+
filename='state_dict.bin',
|
| 19 |
+
local_dir='./audiocraft/ckpt/musicongen'
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def print_directory_contents(path):
|
| 23 |
+
for root, dirs, files in os.walk(path):
|
| 24 |
+
level = root.replace(path, '').count(os.sep)
|
| 25 |
+
indent = ' ' * 4 * (level)
|
| 26 |
+
print(f"{indent}{os.path.basename(root)}/")
|
| 27 |
+
subindent = ' ' * 4 * (level + 1)
|
| 28 |
+
for f in files:
|
| 29 |
+
print(f"{subindent}{f}")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def infer(text):
|
| 33 |
+
|
| 34 |
+
# set hparams
|
| 35 |
+
output_dir = 'example_1' ### change this output directory
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
duration = 30
|
| 39 |
+
num_samples = 5
|
| 40 |
+
bs = 1
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# load your model
|
| 44 |
+
musicgen = audiocraft.models.MusicGen.get_pretrained('./audiocraft/ckpt/musicongen') ### change this path
|
| 45 |
+
musicgen.set_generation_params(duration=duration, extend_stride=duration//2, top_k = 250)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
chords = ['C G A:min F',
|
| 49 |
+
'A:min F C G',
|
| 50 |
+
'C F G F',
|
| 51 |
+
'C A:min F G',
|
| 52 |
+
'D:min G C A:min',
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
descriptions = ["A laid-back blues shuffle with a relaxed tempo, warm guitar tones, and a comfortable groove, perfect for a slow dance or a night in. Instruments: electric guitar, bass, drums."] * num_samples
|
| 56 |
+
|
| 57 |
+
bpms = [120] * num_samples
|
| 58 |
+
|
| 59 |
+
meters = [4] * num_samples
|
| 60 |
+
|
| 61 |
+
wav = []
|
| 62 |
+
for i in range(num_samples//bs):
|
| 63 |
+
print(f"starting {i} batch...")
|
| 64 |
+
temp = musicgen.generate_with_chords_and_beats(descriptions[i*bs:(i+1)*bs],
|
| 65 |
+
chords[i*bs:(i+1)*bs],
|
| 66 |
+
bpms[i*bs:(i+1)*bs],
|
| 67 |
+
meters[i*bs:(i+1)*bs]
|
| 68 |
+
)
|
| 69 |
+
wav.extend(temp.cpu())
|
| 70 |
+
|
| 71 |
+
# save and display generated audio
|
| 72 |
+
for idx, one_wav in enumerate(wav):
|
| 73 |
+
|
| 74 |
+
sav_path = os.path.join('./output_samples', output_dir, chords[idx] + "|" + descriptions[idx]).replace(" ", "_")
|
| 75 |
+
audio_write(sav_path, one_wav.cpu(), musicgen.sample_rate, strategy='loudness', loudness_compressor=True)
|
| 76 |
+
|
| 77 |
+
# Print the outputs directory contents
|
| 78 |
+
print_directory_contents('./output_samples')
|
| 79 |
+
return "done"
|
| 80 |
+
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
with gr.Column():
|
| 83 |
+
gr.Markdown("#MusiConGen")
|
| 84 |
+
with gr.Row():
|
| 85 |
+
text_in = gr.Textbox()
|
| 86 |
+
submit_btn = gr.Button("Submit")
|
| 87 |
+
text_out = gr.Textbox()
|
| 88 |
+
submit_btn.click(
|
| 89 |
+
fn = infer,
|
| 90 |
+
inputs = [text_in],
|
| 91 |
+
outputs = [text_out]
|
| 92 |
+
)
|
| 93 |
+
demo.launch()
|