Download app.py from Taads/XTTS: direct link, hf CLI and curl.
- Browser
- Download file 2.28 kB
-
https://huggingface.co/spaces/Taads/XTTS/resolve/main/app.py
- Command line
-
hf download hf://spaces/Taads/XTTS/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Taads/XTTS/resolve/main/app.py
2.28 kB
| import os, textwrap, zipfile | |
| from TTS.api import TTS | |
| import gradio as gr | |
| import torch | |
| # Confirmar licença do modelo XTTS | |
| os.environ["COQUI_TOS_AGREED"] = "1" | |
| # Inicializar modelo | |
| tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2") | |
| # Enviar modelo para GPU se disponível | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| tts.to(device) | |
| # Pastas | |
| os.makedirs("audios", exist_ok=True) | |
| os.makedirs("zips", exist_ok=True) | |
| def gerar_audio(texto, idioma, arquivo_voz): | |
| try: | |
| if not texto or len(texto.strip()) < 5: | |
| return "Texto muito curto.", None | |
| for f in os.listdir("audios"): | |
| os.remove(os.path.join("audios", f)) | |
| blocos = textwrap.wrap(texto, width=200) | |
| total = len(blocos) | |
| arquivos = [] | |
| log_status = f"Iniciando geração de {total} partes...\n\n" | |
| for idx, bloco in enumerate(blocos, start=1): | |
| nome = f"{idioma}_parte_{str(idx).zfill(3)}.wav" | |
| caminho = os.path.join("audios", nome) | |
| tts.tts_to_file( | |
| text=bloco, | |
| file_path=caminho, | |
| speaker_wav=arquivo_voz, | |
| language=idioma | |
| ) | |
| arquivos.append(caminho) | |
| faltam = total - idx | |
| log_status += f"✅ Parte {idx}/{total} gerada. Faltam {faltam}...\n" | |
| if not arquivos: | |
| return "Erro: nada gerado.", None | |
| zip_path = f"zips/audios_{idioma}.zip" | |
| with zipfile.ZipFile(zip_path, "w") as zipf: | |
| for file in arquivos: | |
| zipf.write(file, arcname=os.path.basename(file)) | |
| log_status += f"\n🎉 Concluído! {len(arquivos)} áudios gerados." | |
| return log_status, zip_path | |
| except Exception as e: | |
| return f"Ocorreu um erro: {str(e)}", None | |
| gr.Interface( | |
| fn=gerar_audio, | |
| inputs=[ | |
| gr.Textbox(lines=10, label="Texto"), | |
| gr.Textbox(label="Idioma (ex: pt, en, es)"), | |
| gr.Audio(label="Voz para clonar (.wav)", type="filepath") | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Status"), | |
| gr.File(label="Download ZIP") | |
| ], | |
| title="XTTS V2 – Gerador Multilíngue", | |
| description="Envie um texto, idioma e uma voz .wav para gerar áudios com clonagem de voz." | |
| ).launch() | |