| import gradio as gr |
| import tempfile |
| from TTS.utils.synthesizer import Synthesizer |
| from huggingface_hub import hf_hub_download |
|
|
| |
| REPO_ID = "mbarnig/lb-de-fr-en-pt-coqui-vits-tts" |
|
|
| |
| 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 com a voz desejada (masculina ou feminina)! |
| """ |
|
|
| |
| max_tokens_text = """ |
| O vento norte e o Sol discutiam quem era o mais forte, quando surgiu um viajante envolvido numa capa. |
| O vento começou a soprar com toda a força, mas quanto mais soprava, mais o viajante se enrolava em sua capa. |
| Então, o Sol começou a brilhar suavemente, e o viajante, sentindo o calor, logo tirou a capa. |
| Assim, o Sol provou que o calor e a gentileza são mais eficazes do que a força bruta. |
| Esta história nos ensina que, muitas vezes, a delicadeza e a paciência são mais poderosas do que a agressividade. |
| """ |
|
|
| |
| TTS_VOICES = [ |
| "Ed", |
| "Linda" |
| ] |
|
|
| |
| def tts(text: str, speaker_idx: str): |
| |
| 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") |
|
|
| |
| synthesizer = Synthesizer( |
| best_model_path, |
| config_path, |
| speakers_path, |
| languages_path, |
| None, |
| None, |
| speaker_encoder_model_path, |
| speaker_encoder_config_path, |
| False |
| ) |
|
|
| |
| wavs = synthesizer.tts(text, speaker_idx, "Português") |
|
|
| |
| with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp: |
| synthesizer.save_wav(wavs, fp) |
| audio_path = fp.name |
|
|
| return audio_path, audio_path |
|
|
|
|
| |
| with gr.Blocks(title=my_title, css=".gradio-container {max-width: 900px; margin: auto;}") as demo: |
| gr.Markdown(f"<h1 style='text-align: center;'>{my_title}</h1>") |
| gr.Markdown(my_description) |
| |
| with gr.Row(): |
| with gr.Column(scale=1): |
| text_input = gr.Textbox( |
| lines=10, |
| label="Texto em Português", |
| placeholder="Insira o texto aqui... (máximo de 500 tokens)" |
| ) |
| voice_selector = gr.Radio( |
| label="Voz", |
| choices=TTS_VOICES, |
| value="Ed" |
| ) |
| submit_button = gr.Button("Gerar Áudio", variant="primary") |
| with gr.Column(scale=1): |
| audio_output = gr.Audio(type="filepath", label="Áudio Gerado") |
| download_button = gr.File(label="Baixar Áudio") |
| |
| gr.Markdown("<hr>") |
| gr.Markdown(""" |
| <h3>Guia do Usuário:</h3> |
| <ul> |
| <li>Insira o texto em português no campo de entrada (até 500 tokens).</li> |
| <li>Selecione a voz desejada (masculina ou feminina).</li> |
| <li>Clique em "Gerar Áudio" para gerar o arquivo de áudio.</li> |
| <li>Reproduza ou faça o download do áudio gerado.</li> |
| </ul> |
| """) |
|
|
| |
| submit_button.click( |
| fn=tts, |
| inputs=[text_input, voice_selector], |
| outputs=[audio_output, download_button] |
| ) |
|
|
| |
| demo.launch() |
|
|