Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,9 @@ import spaces
|
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
import uuid
|
|
|
|
| 9 |
|
| 10 |
-
ZERO_GPU_PATCH_TORCH_DEVICE=1
|
| 11 |
|
| 12 |
# Configura o logging
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -18,33 +19,33 @@ model.set_generation_params(duration=8)
|
|
| 18 |
|
| 19 |
@spaces.GPU(duration=120)
|
| 20 |
def generate_music(description, melody_audio):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
-
logging.info("Gerando música
|
| 32 |
-
wav = model.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
logging.info("Música gerada e salva com sucesso.")
|
| 44 |
-
if not os.path.exists(output_path):
|
| 45 |
-
raise ValueError(f'Failed to save audio to {output_path}')
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
# Define a interface Gradio
|
| 50 |
description = gr.Textbox(label="Description", placeholder="acoustic, guitar, melody, trap, d minor, 90 bpm")
|
|
|
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
import uuid
|
| 9 |
+
from torch.cuda.amp import autocast
|
| 10 |
|
| 11 |
+
ZERO_GPU_PATCH_TORCH_DEVICE = 1
|
| 12 |
|
| 13 |
# Configura o logging
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 19 |
|
| 20 |
@spaces.GPU(duration=120)
|
| 21 |
def generate_music(description, melody_audio):
|
| 22 |
+
with autocast():
|
| 23 |
+
logging.info("Iniciando a geração de música.")
|
| 24 |
+
if description:
|
| 25 |
+
description = [description]
|
| 26 |
+
if melody_audio:
|
| 27 |
+
logging.info(f"Carregando a melodia de áudio de: {melody_audio}")
|
| 28 |
+
melody, sr = torchaudio.load(melody_audio)
|
| 29 |
+
logging.info("Gerando música com descrição e melodia.")
|
| 30 |
+
wav = model.generate_with_chroma(description, melody[None], sr)
|
| 31 |
+
else:
|
| 32 |
+
logging.info("Gerando música apenas com descrição.")
|
| 33 |
+
wav = model.generate(description)
|
| 34 |
else:
|
| 35 |
+
logging.info("Gerando música de forma incondicional.")
|
| 36 |
+
wav = model.generate_unconditional(1)
|
| 37 |
+
filename = f'{str(uuid.uuid4())}.wav'
|
| 38 |
+
output_path = os.path.join('./', filename) # Salva o arquivo no diretório atual
|
| 39 |
+
logging.info(f"Salvando a música gerada em: {output_path}")
|
| 40 |
+
audio_write(output_path, wav[0].cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 41 |
+
|
| 42 |
+
# Verifica a forma do tensor de áudio e se foi salvo corretamente
|
| 43 |
+
logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
|
| 44 |
+
logging.info("Música gerada e salva com sucesso.")
|
| 45 |
+
if not os.path.exists(output_path):
|
| 46 |
+
raise ValueError(f'Failed to save audio to {output_path}')
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
return output_path
|
| 49 |
|
| 50 |
# Define a interface Gradio
|
| 51 |
description = gr.Textbox(label="Description", placeholder="acoustic, guitar, melody, trap, d minor, 90 bpm")
|