File size: 2,895 Bytes
b59e10d
 
 
 
 
69aeb7e
b59e10d
 
69aeb7e
 
 
b59e10d
 
69aeb7e
b59e10d
69aeb7e
 
b59e10d
 
69aeb7e
b59e10d
69aeb7e
 
b59e10d
 
69aeb7e
 
 
 
 
 
 
 
b59e10d
69aeb7e
b59e10d
69aeb7e
 
b59e10d
 
69aeb7e
b59e10d
69aeb7e
 
 
 
b59e10d
 
 
 
 
69aeb7e
 
b59e10d
 
 
 
 
 
 
 
 
 
 
 
69aeb7e
 
 
 
 
 
 
 
 
b59e10d
69aeb7e
 
 
 
 
 
 
b59e10d
 
69aeb7e
 
 
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
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
import tempfile
from TTS.utils.synthesizer import Synthesizer
from huggingface_hub import hf_hub_download

# Repositório do modelo no Hugging Face Hub
REPO_ID = "mbarnig/lb-de-fr-en-pt-coqui-vits-tts"

# Configurações da interface
my_title = "🇵🇹 Sintetizador de Fala em Português com Coqui TTS"
my_description = "Um sintetizador de fala em português baseado no modelo YourTTS da Coqui.ai. Insira o texto e gere o áudio!"
pt_text = "O vento norte e o Sol discutiam quem era o mais forte, quando surgiu um viajante envolvido numa capa."

# Vozes disponíveis para português
TTS_VOICES = [
    "Ed",  # Voz masculina
    "Linda"  # Voz feminina
]

# Exemplo de uso
my_examples = [
    [pt_text, "Ed"],  # Texto em português com voz masculina
    [pt_text, "Linda"]  # Texto em português com voz feminina
]

# Artigo com informações adicionais
my_article = """
<h3>Guia do Usuário</h3>
<p>1. Insira o texto em português no campo de entrada.</p>
<p>2. Selecione a voz desejada (masculina ou feminina).</p>
<p>3. Clique em "Submit" para gerar o áudio.</p>
<p>4. Reproduza o áudio gerado ou faça o download.</p>
"""

# Componentes de entrada e saída
my_inputs = [
    gr.Textbox(lines=5, label="Texto em Português", placeholder="Insira o texto aqui..."),
    gr.Radio(label="Voz", choices=TTS_VOICES, value="Ed")  # Voz padrão: masculina
]

my_outputs = gr.Audio(type="filepath", label="Áudio Gerado")

# Função para sintetizar a fala
def tts(text: str, speaker_idx: str):
    # Baixar os arquivos do modelo
    best_model_path = hf_hub_download(repo_id=REPO_ID, filename="best_model.pth")
    config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
    speakers_path = hf_hub_download(repo_id=REPO_ID, filename="speakers.pth")
    languages_path = hf_hub_download(repo_id=REPO_ID, filename="language_ids.json")
    speaker_encoder_model_path = hf_hub_download(repo_id=REPO_ID, filename="model_se.pth")
    speaker_encoder_config_path = hf_hub_download(repo_id=REPO_ID, filename="config_se.json")

    # Inicializar o sintetizador
    synthesizer = Synthesizer(
        best_model_path,
        config_path,
        speakers_path,
        languages_path,
        None,
        None,
        speaker_encoder_model_path,
        speaker_encoder_config_path,
        False
    )

    # Gerar o áudio
    wavs = synthesizer.tts(text, speaker_idx, "Português")  # Idioma fixo: Português

    # Salvar o áudio em um arquivo temporário
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
        synthesizer.save_wav(wavs, fp)
    return fp.name

# Criar a interface Gradio
iface = gr.Interface(
    fn=tts,
    inputs=my_inputs,
    outputs=my_outputs,
    title=my_title,
    description=my_description,
    article=my_article,
    examples=my_examples,
    allow_flagging=False
)

# Iniciar a interface
iface.launch()