Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torchaudio
|
| 3 |
from audiocraft.models import MusicGen
|
|
|
|
| 4 |
import spaces
|
| 5 |
import logging
|
| 6 |
-
|
|
|
|
| 7 |
# Configura o logging
|
| 8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 9 |
|
|
@@ -29,25 +31,32 @@ def generate_music(description, melody_audio):
|
|
| 29 |
else:
|
| 30 |
logging.info("Gerando música de forma incondicional.")
|
| 31 |
wav = model.generate_unconditional(1)
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
|
| 34 |
-
logging.info("Música gerada com sucesso.")
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
|
|
|
|
|
|
| 38 |
# Define a interface Gradio
|
| 39 |
description = gr.Textbox(label="Description", placeholder="acoustic, guitar, melody, trap, d minor, 90 bpm")
|
| 40 |
melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
|
| 41 |
-
|
| 42 |
|
| 43 |
gr.Interface(
|
| 44 |
fn=generate_music,
|
| 45 |
inputs=[description, melody_audio],
|
| 46 |
-
outputs=
|
| 47 |
title="MusicGen Demo",
|
| 48 |
description="Generate music using the MusicGen model.",
|
| 49 |
examples=[
|
| 50 |
["trap, synthesizer, songstarters, dark, G# minor, 140 bpm", "./assets/kalhonaho.mp3"],
|
| 51 |
["upbeat, electronic, synth, dance, 120 bpm", None]
|
| 52 |
]
|
| 53 |
-
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torchaudio
|
| 3 |
from audiocraft.models import MusicGen
|
| 4 |
+
from audiocraft.data.audio import audio_write
|
| 5 |
import spaces
|
| 6 |
import logging
|
| 7 |
+
import os
|
| 8 |
+
import uuid
|
| 9 |
# Configura o logging
|
| 10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
|
|
|
|
| 31 |
else:
|
| 32 |
logging.info("Gerando música de forma incondicional.")
|
| 33 |
wav = model.generate_unconditional(1)
|
| 34 |
+
filename = f'{str(uuid.uuid4())}.wav'
|
| 35 |
+
output_path = os.path.join('./', filename) # Salva o arquivo no diretório atual
|
| 36 |
+
logging.info(f"Salvando a música gerada em: {output_path}")
|
| 37 |
+
audio_write(output_path, wav[0].cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
| 38 |
+
|
| 39 |
+
# Verifica a forma do tensor de áudio e se foi salvo corretamente
|
| 40 |
logging.info(f"A forma do tensor de áudio gerado: {wav[0].shape}")
|
| 41 |
+
logging.info("Música gerada e salva com sucesso.")
|
| 42 |
+
if not os.path.exists(output_path):
|
| 43 |
+
raise ValueError(f'Failed to save audio to {output_path}')
|
| 44 |
|
| 45 |
+
return output_path
|
| 46 |
+
|
| 47 |
# Define a interface Gradio
|
| 48 |
description = gr.Textbox(label="Description", placeholder="acoustic, guitar, melody, trap, d minor, 90 bpm")
|
| 49 |
melody_audio = gr.Audio(label="Melody Audio (optional)", type="filepath")
|
| 50 |
+
output_path = gr.Audio(label="Generated Music", type="filepath")
|
| 51 |
|
| 52 |
gr.Interface(
|
| 53 |
fn=generate_music,
|
| 54 |
inputs=[description, melody_audio],
|
| 55 |
+
outputs=output_path,
|
| 56 |
title="MusicGen Demo",
|
| 57 |
description="Generate music using the MusicGen model.",
|
| 58 |
examples=[
|
| 59 |
["trap, synthesizer, songstarters, dark, G# minor, 140 bpm", "./assets/kalhonaho.mp3"],
|
| 60 |
["upbeat, electronic, synth, dance, 120 bpm", None]
|
| 61 |
]
|
| 62 |
+
).launch()
|