Create generativemusiccreator.py
Browse files- generativemusiccreator.py +63 -0
generativemusiccreator.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
# audiocraft's pinned torchaudio<2.1 would drag torch back; install without deps.
|
| 5 |
+
subprocess.run(
|
| 6 |
+
[sys.executable, "-m", "pip", "install", "--no-deps", "audiocraft==1.3.0", "demucs"],
|
| 7 |
+
check=True,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
import spaces
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import torchaudio
|
| 13 |
+
from audiocraft.models import AudioGen
|
| 14 |
+
from audiocraft.data.audio import audio_write
|
| 15 |
+
|
| 16 |
+
model = AudioGen.get_pretrained('facebook/audiogen-medium')
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@spaces.GPU
|
| 20 |
+
def infer(prompt, duration):
|
| 21 |
+
|
| 22 |
+
model.set_generation_params(duration=duration) # generate 5 seconds.
|
| 23 |
+
descriptions = [prompt]
|
| 24 |
+
wav = model.generate(descriptions) # generates n samples (referring to the number n of prompts in descriptions)
|
| 25 |
+
|
| 26 |
+
for idx, one_wav in enumerate(wav):
|
| 27 |
+
# Will save under {idx}.wav, with loudness normalization at -14 db LUFS.
|
| 28 |
+
audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 29 |
+
|
| 30 |
+
return "0.wav"
|
| 31 |
+
|
| 32 |
+
css="""
|
| 33 |
+
#col-container{
|
| 34 |
+
margin: 0 auto;
|
| 35 |
+
max-width: 640px;
|
| 36 |
+
}
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(css=css) as demo:
|
| 40 |
+
with gr.Column(elem_id="col-container"):
|
| 41 |
+
gr.HTML("""
|
| 42 |
+
<h2 style="text-align: center;">
|
| 43 |
+
Generative Music Creation: Textually-guided music generation
|
| 44 |
+
</h2>
|
| 45 |
+
<p style="text-align: center;">
|
| 46 |
+
</p>
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
prompt_in = gr.Textbox(label="music prompt")
|
| 50 |
+
with gr.Row():
|
| 51 |
+
duration = gr.Slider(label="Duration", minimum=1, maximum=30, step=1, value=5)
|
| 52 |
+
submit_btn = gr.Button("Submit")
|
| 53 |
+
audio_o = gr.Audio(label="Generative Music result")
|
| 54 |
+
|
| 55 |
+
submit_btn.click(
|
| 56 |
+
fn=infer,
|
| 57 |
+
inputs=[prompt_in, duration],
|
| 58 |
+
outputs=[audio_o]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# agent5.com music creator
|
| 62 |
+
|
| 63 |
+
demo.queue().launch()
|