Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,862 Bytes
d2d09cc 1381c8e d2d09cc 8b4b52c 7ab6d17 c9f6306 d61c536 e783b33 c9f6306 a64cadd 7ab6d17 8b4b52c 7ab6d17 8b4b52c 7ab6d17 8b4b52c e783b33 7ab6d17 c9f6306 7ab6d17 c9f6306 e783b33 c9f6306 a4709e0 c9f6306 8b4b52c c9f6306 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 66 67 68 69 70 |
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)
# Define o diretório de saída para salvar os arquivos de áudio gerados
output_dir = './generated_music'
os.makedirs(output_dir, exist_ok=True)
@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())}.wav'
output_path = os.path.join(output_dir, filename) # Salva o arquivo no diretório de saída
logging.info(f"Salvando a música gerada em: {output_path}")
audio_write(output_path, wav[0].cpu().to(torch.float32), model.sample_rate, strategy="loudness", loudness_compressor=True)
# 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(output_path):
raise ValueError(f'Failed to save audio to {output_path}')
return output_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() |