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7 |
+
from parler_tts import ParlerTTSForConditionalGeneration
|
| 8 |
+
from transformers import AutoTokenizer, AutoFeatureExtractor, set_seed
|
| 9 |
+
from helpers.text_normalizer import BrazilianPortugueseTextNormalizer
|
| 10 |
+
|
| 11 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
|
| 13 |
+
repo_id = "GustavoNeneve/parler-ttsbr" # Seu repositório no Hugging Face
|
| 14 |
+
|
| 15 |
+
model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 17 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(repo_id)
|
| 18 |
+
|
| 19 |
+
SAMPLE_RATE = feature_extractor.sampling_rate
|
| 20 |
+
SEED = 42
|
| 21 |
+
|
| 22 |
+
number_normalizer = BrazilianPortugueseTextNormalizer()
|
| 23 |
+
|
| 24 |
+
default_text = "Olá! Esta é a primeira versão do modelo em português brasileiro."
|
| 25 |
+
default_description = "Francisca fala de forma clara e profissional com sotaque brasileiro neutro."
|
| 26 |
+
examples = [
|
| 27 |
+
[
|
| 28 |
+
"Olá, como posso ajudar você hoje?",
|
| 29 |
+
"Francisca fala de forma acolhedora e profissional com sotaque brasileiro neutro."
|
| 30 |
+
],
|
| 31 |
+
[
|
| 32 |
+
"Esta é uma mensagem importante para todos os usuários.",
|
| 33 |
+
"Francisca fala com ênfase e clareza em ritmo moderado.",
|
| 34 |
+
],
|
| 35 |
+
[
|
| 36 |
+
"Seja bem-vindo ao nosso sistema automatizado.",
|
| 37 |
+
"Francisca fala de forma amigável em um ritmo natural.",
|
| 38 |
+
]
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def preprocess(text):
|
| 43 |
+
text = number_normalizer.normalize(text)
|
| 44 |
+
if text[-1] not in punctuation:
|
| 45 |
+
text = f"{text}."
|
| 46 |
+
return text
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@spaces.GPU
|
| 50 |
+
def gen_tts(text, description):
|
| 51 |
+
inputs = tokenizer(description, return_tensors="pt").to(device)
|
| 52 |
+
prompt = tokenizer(preprocess(text), return_tensors="pt").to(device)
|
| 53 |
+
|
| 54 |
+
set_seed(SEED)
|
| 55 |
+
generation = model.generate(input_ids=inputs.input_ids, prompt_input_ids=prompt.input_ids)
|
| 56 |
+
audio_arr = generation.cpu().numpy().squeeze()
|
| 57 |
+
|
| 58 |
+
return SAMPLE_RATE, audio_arr
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
css = """
|
| 62 |
+
#share-btn-container {
|
| 63 |
+
display: flex;
|
| 64 |
+
padding-left: 0.5rem !important;
|
| 65 |
+
padding-right: 0.5rem !important;
|
| 66 |
+
background-color: #000000;
|
| 67 |
+
justify-content: center;
|
| 68 |
+
align-items: center;
|
| 69 |
+
border-radius: 9999px !important;
|
| 70 |
+
width: 13rem;
|
| 71 |
+
margin-top: 10px;
|
| 72 |
+
margin-left: auto;
|
| 73 |
+
flex: unset !important;
|
| 74 |
+
}
|
| 75 |
+
#share-btn {
|
| 76 |
+
all: initial;
|
| 77 |
+
color: #ffffff;
|
| 78 |
+
font-weight: 600;
|
| 79 |
+
cursor: pointer;
|
| 80 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
| 81 |
+
margin-left: 0.5rem !important;
|
| 82 |
+
padding-top: 0.25rem !important;
|
| 83 |
+
padding-bottom: 0.25rem !important;
|
| 84 |
+
right:0;
|
| 85 |
+
}
|
| 86 |
+
#share-btn * {
|
| 87 |
+
all: unset !important;
|
| 88 |
+
}
|
| 89 |
+
#share-btn-container div:nth-child(-n+2){
|
| 90 |
+
width: auto !important;
|
| 91 |
+
min-height: 0px !important;
|
| 92 |
+
}
|
| 93 |
+
#share-btn-container .wrap {
|
| 94 |
+
display: none !important;
|
| 95 |
+
}
|
| 96 |
+
"""
|
| 97 |
+
with gr.Blocks(css=css) as block:
|
| 98 |
+
gr.HTML(
|
| 99 |
+
"""
|
| 100 |
+
<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
| 101 |
+
<div style="display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem;">
|
| 102 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; line-height: normal;">
|
| 103 |
+
Parler-TTS: Português Brasileiro 🇧🇷
|
| 104 |
+
</h1>
|
| 105 |
+
</div>
|
| 106 |
+
</div>
|
| 107 |
+
"""
|
| 108 |
+
)
|
| 109 |
+
gr.HTML(
|
| 110 |
+
"""
|
| 111 |
+
<p>Este é um modelo de text-to-speech (TTS) adaptado para português brasileiro.
|
| 112 |
+
Gere áudio de alta qualidade especificando a voz e o estilo desejado através de uma descrição em texto.</p>
|
| 113 |
+
|
| 114 |
+
<p>Dicas para boas gerações:
|
| 115 |
+
<ul>
|
| 116 |
+
<li>Use a voz "Francisca" para resultados consistentes</li>
|
| 117 |
+
<li>Você pode especificar diferentes emoções como: "profissional", "acolhedora", "enfática"</li>
|
| 118 |
+
<li>Use pontuação para controlar as pausas na fala</li>
|
| 119 |
+
</ul>
|
| 120 |
+
</p>
|
| 121 |
+
"""
|
| 122 |
+
)
|
| 123 |
+
with gr.Row():
|
| 124 |
+
with gr.Column():
|
| 125 |
+
input_text = gr.Textbox(label="Texto de Entrada", lines=2, value=default_text, elem_id="input_text")
|
| 126 |
+
description = gr.Textbox(label="Descrição", lines=2, value=default_description, elem_id="input_description")
|
| 127 |
+
run_button = gr.Button("Gerar Áudio", variant="primary")
|
| 128 |
+
with gr.Column():
|
| 129 |
+
audio_out = gr.Audio(label="Geração Parler-TTS", type="numpy", elem_id="audio_out")
|
| 130 |
+
|
| 131 |
+
inputs = [input_text, description]
|
| 132 |
+
outputs = [audio_out]
|
| 133 |
+
gr.Examples(examples=examples, fn=gen_tts, inputs=inputs, outputs=outputs, cache_examples=True)
|
| 134 |
+
run_button.click(fn=gen_tts, inputs=inputs, outputs=outputs, queue=True)
|
| 135 |
+
gr.HTML(
|
| 136 |
+
"""
|
| 137 |
+
<p>Para melhorar a prosódia e a naturalidade da fala, estamos ampliando a quantidade de dados de treinamento.
|
| 138 |
+
A versão v1 do modelo será treinada com esses dados, além de otimizações de inferência que melhorarão a latência.
|
| 139 |
+
Se você quiser saber mais sobre como este modelo foi treinado e até mesmo ajustá-lo, confira o
|
| 140 |
+
<a href="https://github.com/huggingface/parler-tts"> repositório Parler-TTS</a> no GitHub. O código e os checkpoints associados ao Parler-TTS estão licenciados sob <a href='https://github.com/huggingface/parler-tts?tab=Apache-2.0-1-ov-file#readme'> Apache 2.0</a>.</p>
|
| 141 |
+
"""
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
block.queue()
|
| 145 |
+
block.launch(share=True)
|