Created
December 11, 2024 18:43
-
-
Save komodoooo/c7ae1e1814285146d1fe75c545488345 to your computer and use it in GitHub Desktop.
Generate musical instrumentals using Gradio and Facebook MusicGen AI models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gradio as gr | |
from os import mkdir | |
from random import random | |
from audiocraft.models import MusicGen | |
from audiocraft.data.audio import audio_write | |
try: | |
mkdir("logs") | |
except FileExistsError: | |
pass | |
models = ["small", "medium", "large", "melody"] | |
strategies = ["loudness", "peak"] | |
def gen_music(size, description, duration, strategy): | |
model = MusicGen.get_pretrained(models[size]) | |
model.set_generation_params(duration=duration) | |
wav = model.generate([description]) | |
for idx, one_wav in enumerate(wav): | |
name = f"logs/{idx}{random()}" | |
audio_write(name, wav[0].cpu(), model.sample_rate, strategy=strategies[strategy]) | |
return name+".wav" | |
with gr.Blocks() as interface: | |
gr.Markdown("# Music generator\nSelect model size, strategy, duration and write a description!") | |
with gr.Row(): | |
with gr.Column(): | |
msize = gr.Dropdown(models, label="Model size", type="index") | |
strategy = gr.Dropdown(strategies, label="Generation strategy", type="index") | |
duration = gr.Slider(10,180, label="Duration in seconds") | |
description = gr.Textbox(label="Music description") | |
output = gr.Audio(label="Generated audio") | |
button = gr.Button("Generate music!") | |
button.click(gen_music, inputs=[msize, description, duration, strategy], outputs=output) | |
interface.launch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment