TTS
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Copyright 2022 Balacoon
|
3 |
+
TTS interactive demo
|
4 |
+
"""
|
5 |
+
|
6 |
+
import logging
|
7 |
+
from typing import cast
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
from balacoon_tts import TTS
|
11 |
+
from huggingface_hub import hf_hub_download, list_repo_files
|
12 |
+
|
13 |
+
# global tts module, initialized from a model selected
|
14 |
+
tts = None
|
15 |
+
|
16 |
+
|
17 |
+
def main():
|
18 |
+
logging.basicConfig(level=logging.INFO)
|
19 |
+
|
20 |
+
with gr.Blocks() as demo:
|
21 |
+
gr.Markdown(
|
22 |
+
"""
|
23 |
+
<h1 align="center">Balacoon🦝 Text-to-Speech</h1>
|
24 |
+
1. Write an utterance to generate,
|
25 |
+
2. Select the model to synthesize with
|
26 |
+
3. Select speaker
|
27 |
+
4. Hit "Generate" and listen to the result!
|
28 |
+
When you select model for the first time,
|
29 |
+
it will take a little time to download it.
|
30 |
+
You can learn more about models available
|
31 |
+
[here](https://huggingface.co/balacoon/tts),
|
32 |
+
visit [Balacoon website](https://balacoon.com/) for more info.
|
33 |
+
"""
|
34 |
+
)
|
35 |
+
with gr.Row(variant="panel"):
|
36 |
+
text = gr.Textbox(label="Text", placeholder="Type something here...")
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
with gr.Column(variant="panel"):
|
40 |
+
repo_files = list_repo_files(repo_id="balacoon/tts")
|
41 |
+
model_files = [x for x in repo_files if x.endswith("_cpu.addon")]
|
42 |
+
model_name = gr.Dropdown(
|
43 |
+
label="Model",
|
44 |
+
choices=model_files,
|
45 |
+
)
|
46 |
+
with gr.Column(variant="panel"):
|
47 |
+
speaker = gr.Dropdown(label="Speaker", choices=[])
|
48 |
+
|
49 |
+
def set_model(model_name_str: str):
|
50 |
+
"""
|
51 |
+
gets value from `model_name`, loads model,
|
52 |
+
re-initializes tts object, gets list of
|
53 |
+
speakers that model supports and set them to `speaker`
|
54 |
+
"""
|
55 |
+
model_path = hf_hub_download(
|
56 |
+
repo_id="balacoon/tts", filename=model_name_str
|
57 |
+
)
|
58 |
+
global tts
|
59 |
+
tts = TTS(model_path)
|
60 |
+
speakers = tts.get_speakers()
|
61 |
+
value = speakers[-1]
|
62 |
+
return gr.Dropdown.update(
|
63 |
+
choices=speakers, value=value, visible=True
|
64 |
+
)
|
65 |
+
|
66 |
+
model_name.change(set_model, inputs=model_name, outputs=speaker)
|
67 |
+
|
68 |
+
with gr.Row(variant="panel"):
|
69 |
+
generate = gr.Button("Generate")
|
70 |
+
with gr.Row(variant="panel"):
|
71 |
+
audio = gr.Audio()
|
72 |
+
|
73 |
+
def synthesize_audio(text_str: str, speaker_str: str = ""):
|
74 |
+
"""
|
75 |
+
gets utterance to synthesize from `text` Textbox
|
76 |
+
and speaker name from `speaker` dropdown list.
|
77 |
+
speaker name might be empty for single-speaker models.
|
78 |
+
Synthesizes the waveform and updates `audio` with it.
|
79 |
+
"""
|
80 |
+
if not text_str:
|
81 |
+
logging.info("text or speaker are not provided")
|
82 |
+
return None
|
83 |
+
global tts
|
84 |
+
if len(text_str) > 1024:
|
85 |
+
text_str = text_str[:1024]
|
86 |
+
samples = cast(TTS, tts).synthesize(text_str, speaker_str)
|
87 |
+
return gr.Audio.update(value=(24000, samples))
|
88 |
+
|
89 |
+
generate.click(synthesize_audio, inputs=[text, speaker], outputs=audio)
|
90 |
+
|
91 |
+
demo.launch()
|
92 |
+
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
main()
|