Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,44 +4,34 @@ import torchaudio
|
|
| 4 |
from audiocraft.models import MusicGen
|
| 5 |
from audiocraft.data.audio import audio_write
|
| 6 |
|
| 7 |
-
#
|
| 8 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
| 9 |
-
model.set_generation_params(duration=8) #
|
| 10 |
|
| 11 |
-
@spaces.GPU(duration=120) #
|
| 12 |
-
def generate_audio(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if melody_path is None:
|
| 22 |
-
return "Melody path cannot be empty for melody mode."
|
| 23 |
-
melody, sr = torchaudio.load(melody_path)
|
| 24 |
-
if descriptions is None:
|
| 25 |
-
descriptions = ['acoustic, guitar, melody, trap, d minor, 90 bpm']
|
| 26 |
-
descriptions = descriptions.split(",") # Converte a string em lista
|
| 27 |
-
wav = model.generate_with_chroma(descriptions, melody[None].expand(3, -1, -1), sr)
|
| 28 |
|
| 29 |
-
# Salva
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
strategy="loudness", loudness_compressor=True)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
return [f"output_{idx}.wav" for idx in range(len(wav))]
|
| 36 |
|
| 37 |
-
#
|
| 38 |
iface = gr.Interface(
|
| 39 |
fn=generate_audio,
|
| 40 |
inputs=[
|
| 41 |
-
gr.
|
| 42 |
-
gr.
|
| 43 |
-
|
| 44 |
-
outputs=gr.File(label="Generated Audio", type="file", multiple=True)
|
| 45 |
)
|
| 46 |
|
| 47 |
iface.launch()
|
|
|
|
| 4 |
from audiocraft.models import MusicGen
|
| 5 |
from audiocraft.data.audio import audio_write
|
| 6 |
|
| 7 |
+
# Carrega o modelo pré-treinado
|
| 8 |
model = MusicGen.get_pretrained('nateraw/musicgen-songstarter-v0.2')
|
| 9 |
+
model.set_generation_params(duration=8) # Configura a duração da geração para 8 segundos
|
| 10 |
|
| 11 |
+
@spaces.GPU(duration=120) # Habilita o uso de GPU
|
| 12 |
+
def generate_audio(prompt, audio_input=None):
|
| 13 |
+
descriptions = [prompt] # Usa o prompt como descrição
|
| 14 |
+
if audio_input is None:
|
| 15 |
+
# Geração incondicional com descrições
|
| 16 |
+
wav = model.generate(descriptions)
|
| 17 |
+
else:
|
| 18 |
+
# Carrega o áudio de entrada e gera com base nele e nas descrições
|
| 19 |
+
melody, sr = torchaudio.load(audio_input)
|
| 20 |
+
wav = model.generate_with_chroma(descriptions, melody[None], sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Salva o arquivo de áudio gerado
|
| 23 |
+
output_path = 'generated_audio.wav'
|
| 24 |
+
audio_write(output_path, wav[0].cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
|
|
|
|
| 25 |
|
| 26 |
+
return output_path
|
|
|
|
| 27 |
|
| 28 |
+
# Cria a interface de usuário com Gradio
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=generate_audio,
|
| 31 |
inputs=[
|
| 32 |
+
gr.Textbox(label="Prompt", placeholder="Enter a description..."),
|
| 33 |
+
gr.File(label="Audio Input (optional)", type="filepath", optional=True)],
|
| 34 |
+
outputs=gr.File(label="Generated Audio")
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
iface.launch()
|