Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,258 Bytes
d2d09cc 8b4b52c 1381c8e d2d09cc 8b4b52c 64dad6c 8b4b52c 64dad6c e1d034f 64dad6c 8b4b52c 64dad6c 8b4b52c 64dad6c 9f2cee2 64dad6c 8b4b52c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import gradio as gr
import spaces
import torchaudio
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
model.set_generation_params(duration=8) # generate 8 seconds.
@spaces.GPU(duration=120) # Specify duration if the function is expected to take more than 60s
def generate_music(description, audio_file):
if audio_file is None:
wav = model.generate([description]) # generates 1 sample based on the provided description
else:
melody, sr = torchaudio.load(audio_file)
wav = model.generate_with_chroma([description], melody[None], sr) # generates using the melody from the given audio and the provided description
audio_write('output', wav[0].cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
return 'output.wav'
iface = gr.Interface(
fn=generate_music,
inputs=[
gr.Text(label="Description"),
gr.Audio(type="filepath", label="Audio File (optional)")
],
outputs=gr.Audio(type="filepath"),
title="MusicGen",
description="Generate music using the MusicGen model. Provide a description and optionally an audio file for melody.",
)
iface.launch()
|