Spaces:
Runtime error
Runtime error
File size: 2,635 Bytes
d2d09cc 1381c8e d2d09cc 8b4b52c 7ab6d17 c9f6306 d61c536 e783b33 c9f6306 a64cadd 7ab6d17 8b4b52c 7ab6d17 8b4b52c 7ab6d17 8b4b52c 7ab6d17 c9f6306 7ab6d17 c9f6306 67dc8c2 4b07798 67dc8c2 c9f6306 67dc8c2 8b4b52c 67dc8c2 7ab6d17 8b4b52c 7ab6d17 |
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 |
import gradio as gr
import torchaudio
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
import spaces
import logging
import os
import uuid
from torch.cuda.amp import autocast
import torch
ZERO_GPU_PATCH_TORCH_DEVICE = 1
# Configura o logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info("Carregando o modelo pré-treinado.")
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
model.set_generation_params(duration=8)
@spaces.GPU(duration=120)
def generate_music(description, melody_audio):
with autocast():
logging.info("Iniciando a geração de música.")
if description:
description = [description]
if melody_audio:
logging.info(f"Carregando a melodia de áudio de: {melody_audio}")
melody, sr = torchaudio.load(melody_audio)
logging.info("Gerando música com descrição e melodia.")
wav = model.generate_with_chroma(description, melody[None], sr)
else:
logging.info("Gerando música apenas com descrição.")
wav = model.generate(description)
else:
logging.info("Gerando música de forma incondicional.")
wav = model.generate_unconditional(1)
filename = f'{str(uuid.uuid4())}'
logging.info(f"Salvando a música gerada com o nome: {filename}")
path = audio_write(filename, wav[0].cpu().to(torch.float32), model.sample_rate, strategy="loudness", loudness_compressor=True)
print("Música salva em", path, ".")
# Verifica a forma do tensor de áudio e se foi salvo corretamente
logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
logging.info("Música gerada e salva com sucesso.")
if not os.path.exists(path):
raise ValueError(f'Failed to save audio to {path}')
return path
# Define a interface 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")
output_path = gr.Audio(label="Generated Music", type="filepath")
gr.Interface(
fn=generate_music,
inputs=[description, melody_audio],
outputs=output_path,
title="MusicGen Demo",
description="Generate music using the MusicGen model.",
examples=[
["trap, synthesizer, songstarters, dark, G# minor, 140 bpm", "./assets/kalhonaho.mp3"],
["upbeat, electronic, synth, dance, 120 bpm", None]
]
).launch() |