File size: 2,284 Bytes
b634297
e14b7ee
b634297
e14b7ee
b506a5c
e14b7ee
1016fa4
 
e14b7ee
1016fa4
b506a5c
e14b7ee
 
 
 
 
b506a5c
 
 
b634297
b506a5c
 
b634297
b506a5c
 
 
 
 
af435d3
b634297
af435d3
b506a5c
 
 
 
af435d3
b506a5c
 
 
 
 
 
af435d3
b634297
af435d3
 
b506a5c
b634297
e14b7ee
b506a5c
 
 
b634297
b506a5c
 
af435d3
 
b506a5c
 
b634297
 
 
 
 
 
 
 
 
 
 
 
 
 
e14b7ee
b634297
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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()