File size: 2,583 Bytes
098d582 8975d2a d2d09cc 1381c8e d2d09cc 8b4b52c e783b33 49fea73 b1094d4 a64cadd 098d582 0762138 e308500 243993a b4d6f6d 49fea73 04736e7 49fea73 b1094d4 49fea73 b1094d4 243993a b1094d4 243993a c9f6306 243993a b1094d4 ca45f45 04736e7 0c23cb1 04736e7 49fea73 b1094d4 49fea73 8b4b52c 7ab6d17 49fea73 7ab6d17 e308500 7ab6d17 49fea73 098d582 7ab6d17 b1094d4 | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import os
import uuid
import torch
import re
import spaces
import gradio as gr
import torchaudio
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
# Configuración del dispositivo
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Configurar ZERO_GPU_PATCH_TORCH_DEVICE
ZERO_GPU_PATCH_TORCH_DEVICE = 1
# Cargar el modelo `musicgen-melody` una única vez
model = MusicGen.get_pretrained("facebook/musicgen-melody")
@spaces.GPU
def generate_music(description, melody_audio, duration):
description = clean_text(description)
model.set_generation_params(duration=int(duration * 1000)) # Convertir segundos a milisegundos
try:
with torch.no_grad():
if description:
description = [description]
if melody_audio:
# Cargar el archivo de audio para remixar
melody, sr = torchaudio.load(melody_audio, normalize=True)
melody = melody.to(device) if torch.cuda.is_available() else melody
wav = model.generate_with_chroma(description, melody[None], sr)
else:
wav = model.generate(description)
else:
wav = model.generate_unconditional(1)
filename = f'{str(uuid.uuid4())}.wav'
path = audio_write(filename, wav[0].cpu().to(torch.float32), model.sample_rate, strategy="loudness", loudness_compressor=True)
if not os.path.exists(path):
raise ValueError(f'Failed to save audio to {path}')
return path
except Exception as e:
return str(e)
def clean_text(text):
text = re.sub(r'http\S+', '', text)
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
return text
# Definir la interfaz de Gradio
description = gr.Textbox(label="Description", placeholder="Acoustic, guitar, melody, trap, D minor, 90 bpm")
melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
duration = gr.Number(label="Duration (seconds)", value=10, precision=0)
output_path = gr.File(label="Generated Music")
gr.Interface(
fn=generate_music,
inputs=[description, melody_audio, duration],
outputs=output_path,
title="MusicGen Melody Demo",
description="Generate music using the MusicGen melody model. Optionally remix with an audio file. Download the generated audio file.",
examples=[
["happy rock", None, 8],
["energetic EDM", None, 8],
["chillwave", "./assets/example_melody.mp3", 10]
]
).launch()
|