Lucas Gris commited on
Commit ·
ef0bc52
1
Parent(s): 0cdd8ac
Initial commit
Browse files- README.md +5 -4
- app.py +85 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
---
|
| 2 |
title: Audiogen
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
title: Audiogen
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.24.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
short_description: audiogen-space
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from audiocraft.models import MusicGen, AudioGen
|
| 3 |
+
from audiocraft.data.audio import audio_write
|
| 4 |
+
import os
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Verificar dispositivo
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
print(f"Usando dispositivo: {device}")
|
| 10 |
+
|
| 11 |
+
# Carregar modelos corretamente
|
| 12 |
+
try:
|
| 13 |
+
# Não usamos .to(device) aqui - o AudioCraft cuida disso internamente
|
| 14 |
+
musicgen = MusicGen.get_pretrained('facebook/musicgen-small')
|
| 15 |
+
audiogen = AudioGen.get_pretrained('facebook/audiogen-medium')
|
| 16 |
+
|
| 17 |
+
# Configurar parâmetros de geração
|
| 18 |
+
musicgen.set_generation_params(duration=5)
|
| 19 |
+
audiogen.set_generation_params(duration=5)
|
| 20 |
+
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Erro ao carregar modelos: {e}")
|
| 23 |
+
raise
|
| 24 |
+
|
| 25 |
+
def generate_audio(prompt, model, model_type):
|
| 26 |
+
try:
|
| 27 |
+
|
| 28 |
+
# Caso 1: Se vier da API (JSON com lista), pega o primeiro item
|
| 29 |
+
if isinstance(prompt, list):
|
| 30 |
+
prompt = prompt[0]
|
| 31 |
+
|
| 32 |
+
print(f"Gerando {model_type} para:", prompt)
|
| 33 |
+
wav = model.generate([prompt])[0] # Gera o áudio
|
| 34 |
+
|
| 35 |
+
# Diretório para salvar os arquivos
|
| 36 |
+
temp_dir = "generated_audio"
|
| 37 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 38 |
+
|
| 39 |
+
# Caminho do arquivo
|
| 40 |
+
output_path = os.path.join(temp_dir, f"{model_type}_{hash(prompt)}.wav")
|
| 41 |
+
|
| 42 |
+
# Salvar o áudio
|
| 43 |
+
audio_write(
|
| 44 |
+
output_path[:-4], # Remove a extensão .wav
|
| 45 |
+
wav.cpu(), # Garante que está na CPU para salvar
|
| 46 |
+
model.sample_rate,
|
| 47 |
+
strategy="loudness",
|
| 48 |
+
loudness_compressor=True
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
print(f"Áudio gerado com sucesso em: {output_path}")
|
| 52 |
+
return output_path
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Erro ao gerar áudio: {e}")
|
| 56 |
+
raise gr.Error(f"Falha na geração: {str(e)}")
|
| 57 |
+
|
| 58 |
+
def generate_music(prompt):
|
| 59 |
+
return generate_audio(prompt, musicgen, "música")
|
| 60 |
+
|
| 61 |
+
def generate_sound_effect(prompt):
|
| 62 |
+
return generate_audio(prompt, audiogen, "efeito sonoro")
|
| 63 |
+
|
| 64 |
+
# Configuração da interface
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
gr.Markdown("# 🎵 Gerador de Áudio com MusicGen e AudioGen")
|
| 67 |
+
with gr.Tabs():
|
| 68 |
+
with gr.TabItem("MusicGen 🎵"):
|
| 69 |
+
gr.Markdown("## Gerar Música")
|
| 70 |
+
with gr.Row():
|
| 71 |
+
music_input = gr.Text(label="Descreva a música que deseja")
|
| 72 |
+
music_output = gr.Audio(label="Música Gerada", type="filepath")
|
| 73 |
+
music_button = gr.Button("Gerar Música")
|
| 74 |
+
music_button.click(fn=generate_music, inputs=music_input, outputs=music_output)
|
| 75 |
+
|
| 76 |
+
with gr.TabItem("AudioGen 🔊"):
|
| 77 |
+
gr.Markdown("## Gerar Efeitos Sonoros")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
sound_input = gr.Text(label="Descreva o efeito sonoro que deseja")
|
| 80 |
+
sound_output = gr.Audio(label="Som Gerado", type="filepath")
|
| 81 |
+
sound_button = gr.Button("Gerar Som")
|
| 82 |
+
sound_button.click(fn=generate_sound_effect, inputs=sound_input, outputs=sound_output)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/facebookresearch/audiocraft.git
|
| 2 |
+
torchaudio
|
| 3 |
+
ffmpeg-python
|