Update app.py
Browse files
app.py
CHANGED
|
@@ -3,62 +3,54 @@ import tempfile
|
|
| 3 |
from TTS.utils.synthesizer import Synthesizer
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
|
|
|
| 6 |
REPO_ID = "mbarnig/lb-de-fr-en-pt-coqui-vits-tts"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
de_text = "Einst stritten sich Nordwind und Sonne, wer von ihnen beiden wohl der Stärkere wäre, als ein Wanderer, der in einen warmen Mantel gehüllt war, des Weges daherkam."
|
| 12 |
-
fr_text = "La bise et le soleil se disputaient, chacun assurant qu'il était le plus fort, quand ils ont vu un voyageur qui s'avançait, enveloppé dans son manteau."
|
| 13 |
-
en_text = "The North Wind and the Sun were disputing which was the stronger, when a traveler came along wrapped in a warm cloak."
|
| 14 |
pt_text = "O vento norte e o Sol discutiam quem era o mais forte, quando surgiu um viajante envolvido numa capa."
|
| 15 |
|
|
|
|
| 16 |
TTS_VOICES = [
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"Ed",
|
| 20 |
-
"Guy",
|
| 21 |
-
"Judith",
|
| 22 |
-
"Kerstin",
|
| 23 |
-
"Linda",
|
| 24 |
-
"Thorsten"
|
| 25 |
-
]
|
| 26 |
-
|
| 27 |
-
TTS_LANGUAGES = [
|
| 28 |
-
"Deutsch",
|
| 29 |
-
"English",
|
| 30 |
-
"Français",
|
| 31 |
-
"Lëtzebuergesch",
|
| 32 |
-
"Português"
|
| 33 |
]
|
| 34 |
|
|
|
|
| 35 |
my_examples = [
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
[fr_text, "Bernard", "Français"],
|
| 39 |
-
[en_text, "Linda", "English"],
|
| 40 |
-
[pt_text, "Ed", "Português"]
|
| 41 |
]
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
| 45 |
my_inputs = [
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
gr.Radio(label="Language", choices = TTS_LANGUAGES, value = "Lëtzebuergesch"),
|
| 49 |
]
|
| 50 |
|
| 51 |
-
my_outputs = gr.Audio(type="filepath", label="
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
|
| 56 |
speakers_path = hf_hub_download(repo_id=REPO_ID, filename="speakers.pth")
|
| 57 |
languages_path = hf_hub_download(repo_id=REPO_ID, filename="language_ids.json")
|
| 58 |
speaker_encoder_model_path = hf_hub_download(repo_id=REPO_ID, filename="model_se.pth")
|
| 59 |
speaker_encoder_config_path = hf_hub_download(repo_id=REPO_ID, filename="config_se.json")
|
| 60 |
-
|
| 61 |
-
#
|
| 62 |
synthesizer = Synthesizer(
|
| 63 |
best_model_path,
|
| 64 |
config_path,
|
|
@@ -71,20 +63,25 @@ def tts(text: str, speaker_idx: str, language_idx: str):
|
|
| 71 |
False
|
| 72 |
)
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
wavs = synthesizer.tts(text, speaker_idx,
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
iface = gr.Interface(
|
| 81 |
-
fn=tts,
|
| 82 |
-
inputs=my_inputs,
|
| 83 |
-
outputs=my_outputs,
|
| 84 |
-
title=my_title,
|
| 85 |
-
description
|
| 86 |
-
article
|
| 87 |
-
examples
|
| 88 |
allow_flagging=False
|
| 89 |
)
|
| 90 |
-
|
|
|
|
|
|
|
|
|
| 3 |
from TTS.utils.synthesizer import Synthesizer
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
+
# Repositório do modelo no Hugging Face Hub
|
| 7 |
REPO_ID = "mbarnig/lb-de-fr-en-pt-coqui-vits-tts"
|
| 8 |
|
| 9 |
+
# Configurações da interface
|
| 10 |
+
my_title = "🇵🇹 Sintetizador de Fala em Português com Coqui TTS"
|
| 11 |
+
my_description = "Um sintetizador de fala em português baseado no modelo YourTTS da Coqui.ai. Insira o texto e gere o áudio!"
|
|
|
|
|
|
|
|
|
|
| 12 |
pt_text = "O vento norte e o Sol discutiam quem era o mais forte, quando surgiu um viajante envolvido numa capa."
|
| 13 |
|
| 14 |
+
# Vozes disponíveis para português
|
| 15 |
TTS_VOICES = [
|
| 16 |
+
"Ed", # Voz masculina
|
| 17 |
+
"Linda" # Voz feminina
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
]
|
| 19 |
|
| 20 |
+
# Exemplo de uso
|
| 21 |
my_examples = [
|
| 22 |
+
[pt_text, "Ed"], # Texto em português com voz masculina
|
| 23 |
+
[pt_text, "Linda"] # Texto em português com voz feminina
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
|
| 26 |
+
# Artigo com informações adicionais
|
| 27 |
+
my_article = """
|
| 28 |
+
<h3>Guia do Usuário</h3>
|
| 29 |
+
<p>1. Insira o texto em português no campo de entrada.</p>
|
| 30 |
+
<p>2. Selecione a voz desejada (masculina ou feminina).</p>
|
| 31 |
+
<p>3. Clique em "Submit" para gerar o áudio.</p>
|
| 32 |
+
<p>4. Reproduza o áudio gerado ou faça o download.</p>
|
| 33 |
+
"""
|
| 34 |
|
| 35 |
+
# Componentes de entrada e saída
|
| 36 |
my_inputs = [
|
| 37 |
+
gr.Textbox(lines=5, label="Texto em Português", placeholder="Insira o texto aqui..."),
|
| 38 |
+
gr.Radio(label="Voz", choices=TTS_VOICES, value="Ed") # Voz padrão: masculina
|
|
|
|
| 39 |
]
|
| 40 |
|
| 41 |
+
my_outputs = gr.Audio(type="filepath", label="Áudio Gerado")
|
| 42 |
|
| 43 |
+
# Função para sintetizar a fala
|
| 44 |
+
def tts(text: str, speaker_idx: str):
|
| 45 |
+
# Baixar os arquivos do modelo
|
| 46 |
+
best_model_path = hf_hub_download(repo_id=REPO_ID, filename="best_model.pth")
|
| 47 |
config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
|
| 48 |
speakers_path = hf_hub_download(repo_id=REPO_ID, filename="speakers.pth")
|
| 49 |
languages_path = hf_hub_download(repo_id=REPO_ID, filename="language_ids.json")
|
| 50 |
speaker_encoder_model_path = hf_hub_download(repo_id=REPO_ID, filename="model_se.pth")
|
| 51 |
speaker_encoder_config_path = hf_hub_download(repo_id=REPO_ID, filename="config_se.json")
|
| 52 |
+
|
| 53 |
+
# Inicializar o sintetizador
|
| 54 |
synthesizer = Synthesizer(
|
| 55 |
best_model_path,
|
| 56 |
config_path,
|
|
|
|
| 63 |
False
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Gerar o áudio
|
| 67 |
+
wavs = synthesizer.tts(text, speaker_idx, "Português") # Idioma fixo: Português
|
| 68 |
+
|
| 69 |
+
# Salvar o áudio em um arquivo temporário
|
| 70 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
| 71 |
+
synthesizer.save_wav(wavs, fp)
|
| 72 |
+
return fp.name
|
| 73 |
+
|
| 74 |
+
# Criar a interface Gradio
|
| 75 |
iface = gr.Interface(
|
| 76 |
+
fn=tts,
|
| 77 |
+
inputs=my_inputs,
|
| 78 |
+
outputs=my_outputs,
|
| 79 |
+
title=my_title,
|
| 80 |
+
description=my_description,
|
| 81 |
+
article=my_article,
|
| 82 |
+
examples=my_examples,
|
| 83 |
allow_flagging=False
|
| 84 |
)
|
| 85 |
+
|
| 86 |
+
# Iniciar a interface
|
| 87 |
+
iface.launch()
|