Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,37 @@
|
|
| 1 |
import os, textwrap, zipfile
|
| 2 |
-
from TTS.api import TTS
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
tts.to("cuda" if tts.is_cuda_available() else "cpu")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
os.makedirs("audios", exist_ok=True)
|
| 14 |
os.makedirs("zips", exist_ok=True)
|
| 15 |
|
|
|
|
| 16 |
def gerar_audio(texto, idioma, arquivo_voz):
|
| 17 |
try:
|
| 18 |
if not texto or len(texto.strip()) < 5:
|
| 19 |
return "Texto muito curto.", None
|
| 20 |
|
| 21 |
-
# Limpar
|
| 22 |
for f in os.listdir("audios"):
|
| 23 |
os.remove(os.path.join("audios", f))
|
| 24 |
|
|
|
|
| 25 |
blocos = textwrap.wrap(texto, width=200)
|
| 26 |
arquivos = []
|
| 27 |
|
| 28 |
for idx, bloco in enumerate(blocos, start=1):
|
| 29 |
nome = f"{idioma}_parte_{str(idx).zfill(3)}.wav"
|
| 30 |
caminho = os.path.join("audios", nome)
|
|
|
|
| 31 |
tts.tts_to_file(
|
| 32 |
text=bloco,
|
| 33 |
file_path=caminho,
|
|
@@ -37,18 +41,20 @@ def gerar_audio(texto, idioma, arquivo_voz):
|
|
| 37 |
arquivos.append(caminho)
|
| 38 |
|
| 39 |
if not arquivos:
|
| 40 |
-
return "Erro:
|
| 41 |
|
|
|
|
| 42 |
zip_path = f"zips/audios_{idioma}.zip"
|
| 43 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 44 |
for file in arquivos:
|
| 45 |
zipf.write(file, arcname=os.path.basename(file))
|
| 46 |
|
| 47 |
-
return f"{len(arquivos)} áudios gerados.", zip_path
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
return f"Ocorreu um erro: {str(e)}", None
|
| 51 |
|
|
|
|
| 52 |
gr.Interface(
|
| 53 |
fn=gerar_audio,
|
| 54 |
inputs=[
|
|
@@ -61,5 +67,5 @@ gr.Interface(
|
|
| 61 |
gr.File(label="Download ZIP")
|
| 62 |
],
|
| 63 |
title="XTTS V2 – Gerador Multilíngue",
|
| 64 |
-
description="Envie um texto, idioma e uma voz .wav para gerar
|
| 65 |
).launch()
|
|
|
|
| 1 |
import os, textwrap, zipfile
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# ✅ Aceitar os termos de uso da Coqui antes de importar TTS
|
| 5 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 6 |
+
|
| 7 |
+
from TTS.api import TTS
|
| 8 |
+
|
| 9 |
+
# Inicializar o modelo XTTS
|
| 10 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
|
| 11 |
tts.to("cuda" if tts.is_cuda_available() else "cpu")
|
| 12 |
|
| 13 |
+
# Criar pastas necessárias
|
| 14 |
os.makedirs("audios", exist_ok=True)
|
| 15 |
os.makedirs("zips", exist_ok=True)
|
| 16 |
|
| 17 |
+
# Função principal
|
| 18 |
def gerar_audio(texto, idioma, arquivo_voz):
|
| 19 |
try:
|
| 20 |
if not texto or len(texto.strip()) < 5:
|
| 21 |
return "Texto muito curto.", None
|
| 22 |
|
| 23 |
+
# Limpar pastas antigas
|
| 24 |
for f in os.listdir("audios"):
|
| 25 |
os.remove(os.path.join("audios", f))
|
| 26 |
|
| 27 |
+
# Dividir texto em blocos de até 200 caracteres
|
| 28 |
blocos = textwrap.wrap(texto, width=200)
|
| 29 |
arquivos = []
|
| 30 |
|
| 31 |
for idx, bloco in enumerate(blocos, start=1):
|
| 32 |
nome = f"{idioma}_parte_{str(idx).zfill(3)}.wav"
|
| 33 |
caminho = os.path.join("audios", nome)
|
| 34 |
+
|
| 35 |
tts.tts_to_file(
|
| 36 |
text=bloco,
|
| 37 |
file_path=caminho,
|
|
|
|
| 41 |
arquivos.append(caminho)
|
| 42 |
|
| 43 |
if not arquivos:
|
| 44 |
+
return "Erro: nenhum áudio gerado.", None
|
| 45 |
|
| 46 |
+
# Compactar os arquivos em um .zip
|
| 47 |
zip_path = f"zips/audios_{idioma}.zip"
|
| 48 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 49 |
for file in arquivos:
|
| 50 |
zipf.write(file, arcname=os.path.basename(file))
|
| 51 |
|
| 52 |
+
return f"{len(arquivos)} áudios gerados com sucesso.", zip_path
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
return f"Ocorreu um erro: {str(e)}", None
|
| 56 |
|
| 57 |
+
# Interface Gradio
|
| 58 |
gr.Interface(
|
| 59 |
fn=gerar_audio,
|
| 60 |
inputs=[
|
|
|
|
| 67 |
gr.File(label="Download ZIP")
|
| 68 |
],
|
| 69 |
title="XTTS V2 – Gerador Multilíngue",
|
| 70 |
+
description="Envie um texto, o idioma e uma amostra de voz (.wav) para gerar áudio com clonagem de voz usando XTTS v2."
|
| 71 |
).launch()
|