Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import gradio as gr
|
|
| 3 |
import torchaudio
|
| 4 |
from audiocraft.models import MusicGen
|
| 5 |
from audiocraft.data.audio import audio_write
|
| 6 |
-
import logging
|
| 7 |
import os
|
| 8 |
import uuid
|
| 9 |
from torch.cuda.amp import autocast
|
|
@@ -12,54 +11,35 @@ import re
|
|
| 12 |
|
| 13 |
ZERO_GPU_PATCH_TORCH_DEVICE = 1
|
| 14 |
|
| 15 |
-
# Configura o logging
|
| 16 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 17 |
-
|
| 18 |
-
logging.info("Carregando o modelo pré-treinado.")
|
| 19 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
| 20 |
|
| 21 |
|
| 22 |
@spaces.GPU(duration=120)
|
| 23 |
def generate_music(description, melody_audio, duration):
|
| 24 |
-
# Remove spam da descrição
|
| 25 |
description = clean_text(description)
|
| 26 |
-
|
| 27 |
-
# Define a duração da música em segundos
|
| 28 |
model.set_generation_params(duration=int(duration * 1000))
|
| 29 |
|
| 30 |
with autocast():
|
| 31 |
-
logging.info("Iniciando a geração de música.")
|
| 32 |
if description:
|
| 33 |
description = [description]
|
| 34 |
if melody_audio:
|
| 35 |
-
logging.info(f"Carregando a melodia de áudio de: {melody_audio}")
|
| 36 |
melody, sr = torchaudio.load(melody_audio)
|
| 37 |
-
logging.info("Gerando música com descrição e melodia.")
|
| 38 |
wav = model.generate_with_chroma(description, melody[None], sr)
|
| 39 |
else:
|
| 40 |
-
logging.info("Gerando música apenas com descrição.")
|
| 41 |
wav = model.generate(description)
|
| 42 |
else:
|
| 43 |
-
logging.info("Gerando música de forma incondicional.")
|
| 44 |
wav = model.generate_unconditional(1)
|
| 45 |
filename = f'{str(uuid.uuid4())}.wav'
|
| 46 |
-
logging.info(f"Salvando a música gerada com o nome: {filename}")
|
| 47 |
path = audio_write(filename, wav[0].cpu().to(torch.float32), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 48 |
-
print("Música salva em", path, ".")
|
| 49 |
-
logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
|
| 50 |
-
logging.info("Música gerada e salva com sucesso.")
|
| 51 |
if not os.path.exists(path):
|
| 52 |
raise ValueError(f'Failed to save audio to {path}')
|
| 53 |
-
|
| 54 |
return path
|
| 55 |
|
| 56 |
def clean_text(text):
|
| 57 |
-
|
| 58 |
-
text = re.sub(r'
|
| 59 |
-
text = re.sub(r'[^a-zA-Z0-9\s]', '', text) # remove caracteres especiais
|
| 60 |
return text
|
| 61 |
|
| 62 |
-
# Define a interface Gradio
|
| 63 |
description = gr.Textbox(label="Descrição", placeholder="acústico, guitarra, melodia, trap, ré menor, 90 bpm")
|
| 64 |
melody_audio = gr.Audio(label="Melodia de Áudio (opcional)", type="filepath")
|
| 65 |
duration = gr.Number(label="Duração (segundos)", value=10)
|
|
|
|
| 3 |
import torchaudio
|
| 4 |
from audiocraft.models import MusicGen
|
| 5 |
from audiocraft.data.audio import audio_write
|
|
|
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
from torch.cuda.amp import autocast
|
|
|
|
| 11 |
|
| 12 |
ZERO_GPU_PATCH_TORCH_DEVICE = 1
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
| 15 |
|
| 16 |
|
| 17 |
@spaces.GPU(duration=120)
|
| 18 |
def generate_music(description, melody_audio, duration):
|
|
|
|
| 19 |
description = clean_text(description)
|
|
|
|
|
|
|
| 20 |
model.set_generation_params(duration=int(duration * 1000))
|
| 21 |
|
| 22 |
with autocast():
|
|
|
|
| 23 |
if description:
|
| 24 |
description = [description]
|
| 25 |
if melody_audio:
|
|
|
|
| 26 |
melody, sr = torchaudio.load(melody_audio)
|
|
|
|
| 27 |
wav = model.generate_with_chroma(description, melody[None], sr)
|
| 28 |
else:
|
|
|
|
| 29 |
wav = model.generate(description)
|
| 30 |
else:
|
|
|
|
| 31 |
wav = model.generate_unconditional(1)
|
| 32 |
filename = f'{str(uuid.uuid4())}.wav'
|
|
|
|
| 33 |
path = audio_write(filename, wav[0].cpu().to(torch.float32), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
|
|
|
|
|
|
|
|
|
| 34 |
if not os.path.exists(path):
|
| 35 |
raise ValueError(f'Failed to save audio to {path}')
|
|
|
|
| 36 |
return path
|
| 37 |
|
| 38 |
def clean_text(text):
|
| 39 |
+
text = re.sub(r'http\S+', '', text)
|
| 40 |
+
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
|
|
|
| 41 |
return text
|
| 42 |
|
|
|
|
| 43 |
description = gr.Textbox(label="Descrição", placeholder="acústico, guitarra, melodia, trap, ré menor, 90 bpm")
|
| 44 |
melody_audio = gr.Audio(label="Melodia de Áudio (opcional)", type="filepath")
|
| 45 |
duration = gr.Number(label="Duração (segundos)", value=10)
|