Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,245 +1,85 @@
|
|
1 |
-
#!/usr/bin/env python3
|
2 |
-
#
|
3 |
-
# Copyright 2022-2023 Xiaomi Corp. (authors: Fangjun Kuang)
|
4 |
-
#
|
5 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
-
# you may not use this file except in compliance with the License.
|
7 |
-
# You may obtain a copy of the License at
|
8 |
-
#
|
9 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
-
#
|
11 |
-
# Unless required by applicable law or agreed to in writing, software
|
12 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
-
# See the License for the specific language governing permissions and
|
15 |
-
# limitations under the License.
|
16 |
-
|
17 |
import logging
|
18 |
import os
|
19 |
-
import time
|
20 |
import uuid
|
21 |
-
|
22 |
import gradio as gr
|
23 |
import soundfile as sf
|
24 |
|
25 |
from model import get_pretrained_model, language_to_models
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
description = """
|
30 |
-
This space shows how to convert text to speech with Next-gen Kaldi.
|
31 |
-
It is running on CPU within a docker container provided by Hugging Face.
|
32 |
-
See more information by visiting the following links:
|
33 |
-
- <https://github.com/k2-fsa/sherpa-onnx>
|
34 |
-
If you want to deploy it locally, please see
|
35 |
-
<https://k2-fsa.github.io/sherpa/>
|
36 |
-
If you want to use Android APKs, please see
|
37 |
-
<https://k2-fsa.github.io/sherpa/onnx/tts/apk.html>
|
38 |
-
If you want to use Android text-to-speech engine APKs, please see
|
39 |
-
<https://k2-fsa.github.io/sherpa/onnx/tts/apk-engine.html>
|
40 |
-
If you want to download an all-in-one exe for Windows, please see
|
41 |
-
<https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models>
|
42 |
-
"""
|
43 |
-
|
44 |
-
css = """
|
45 |
-
.result {display:flex;flex-direction:column}
|
46 |
-
.result_item {padding:15px;margin-bottom:8px;border-radius:15px;width:100%}
|
47 |
-
.result_item_success {background-color:mediumaquamarine;color:white;align-self:start}
|
48 |
-
.result_item_error {background-color:#ff7070;color:white;align-self:start}
|
49 |
-
"""
|
50 |
-
|
51 |
-
examples = [
|
52 |
-
[
|
53 |
-
"Chinese (Mandarin, 普通话)",
|
54 |
-
"csukuangfj/vits-zh-hf-fanchen-wnj|1",
|
55 |
-
"在一个阳光明媚的夏天,小马、小羊和小狗它们一块儿在广阔的草地上,嬉戏玩耍,这时小猴来了,还带着它心爱的足球活蹦乱跳地跑前、跑后教小马、小羊、小狗踢足球。",
|
56 |
-
0,
|
57 |
-
1.0,
|
58 |
-
],
|
59 |
-
[
|
60 |
-
"Chinese (Mandarin, 普通话)",
|
61 |
-
"csukuangfj/vits-zh-hf-fanchen-C|187",
|
62 |
-
'小米的使命是,始终坚持做"感动人心、价格厚道"的好产品,让全球每个人都能享受科技带来的美好生活。',
|
63 |
-
0,
|
64 |
-
1.0,
|
65 |
-
],
|
66 |
-
["Min-nan (闽南话)", "csukuangfj/vits-mms-nan", "ài piaǸ chiah ē iaN̂", 0, 1.0],
|
67 |
-
["Thai", "csukuangfj/vits-mms-tha", "ฉันรักคุณ", 0, 1.0],
|
68 |
-
[
|
69 |
-
"Chinese (Mandarin, 普通话)",
|
70 |
-
"csukuangfj/sherpa-onnx-vits-zh-ll|5",
|
71 |
-
"当夜幕降临,星光点点,伴随着微风拂面,我在静谧中感受着时光的流转,思念如涟漪荡漾,梦境如画卷展开,我与自然融为一体,沉静在这片宁静的美丽之中,感受着生命的奇迹与温柔。",
|
72 |
-
2,
|
73 |
-
1.0,
|
74 |
-
],
|
75 |
-
]
|
76 |
-
|
77 |
-
|
78 |
-
def update_model_dropdown(language: str):
|
79 |
if language in language_to_models:
|
80 |
-
choices
|
81 |
-
|
82 |
-
|
83 |
-
value=choices[0],
|
84 |
-
interactive=True,
|
85 |
-
)
|
86 |
-
|
87 |
-
raise ValueError(f"Unsupported language: {language}")
|
88 |
|
89 |
-
|
90 |
-
def
|
91 |
-
|
92 |
-
<div class='result'>
|
93 |
-
<div class='result_item {style}'>
|
94 |
-
{s}
|
95 |
-
</div>
|
96 |
-
</div>
|
97 |
-
"""
|
98 |
-
|
99 |
-
|
100 |
-
def process(language: str, repo_id: str, text: str, sid: str, speed: float):
|
101 |
-
logging.info(f"Input text: {text}. sid: {sid}, speed: {speed}")
|
102 |
sid = int(sid)
|
103 |
tts = get_pretrained_model(repo_id, speed)
|
104 |
|
105 |
start = time.time()
|
106 |
audio = tts.generate(text, sid=sid)
|
107 |
-
end = time.time()
|
108 |
-
|
109 |
-
if len(audio.samples) == 0:
|
110 |
-
raise ValueError(
|
111 |
-
"Error in generating audios. Please read previous error messages."
|
112 |
-
)
|
113 |
-
|
114 |
duration = len(audio.samples) / audio.sample_rate
|
115 |
-
|
116 |
-
elapsed_seconds = end - start
|
117 |
rtf = elapsed_seconds / duration
|
118 |
|
119 |
info = f"""
|
120 |
-
Wave duration
|
121 |
-
Processing time: {elapsed_seconds:.3f} s
|
122 |
-
RTF: {
|
123 |
"""
|
124 |
|
125 |
logging.info(info)
|
126 |
-
logging.info(f"\nrepo_id: {repo_id}\ntext: {text}\nsid: {sid}\nspeed: {speed}")
|
127 |
-
|
128 |
-
filename = str(uuid.uuid4())
|
129 |
-
filename = f"{filename}.wav"
|
130 |
-
sf.write(
|
131 |
-
filename,
|
132 |
-
audio.samples,
|
133 |
-
samplerate=audio.sample_rate,
|
134 |
-
subtype="PCM_16",
|
135 |
-
)
|
136 |
-
|
137 |
-
return filename, build_html_output(info)
|
138 |
|
|
|
|
|
|
|
139 |
|
140 |
-
|
|
|
141 |
|
142 |
with demo:
|
143 |
-
gr.Markdown(
|
144 |
-
|
145 |
-
|
146 |
-
language_radio = gr.Radio(
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
)
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
)
|
163 |
|
164 |
-
|
165 |
-
with gr.TabItem("Please input your text"):
|
166 |
-
input_text = gr.Textbox(
|
167 |
-
label="Input text",
|
168 |
-
info="Your text",
|
169 |
-
lines=3,
|
170 |
-
placeholder="Please input your text here",
|
171 |
-
)
|
172 |
-
|
173 |
-
input_sid = gr.Textbox(
|
174 |
-
label="Speaker ID",
|
175 |
-
info="Speaker ID",
|
176 |
-
lines=1,
|
177 |
-
max_lines=1,
|
178 |
-
value="0",
|
179 |
-
placeholder="Speaker ID. Valid only for mult-speaker model",
|
180 |
-
)
|
181 |
-
|
182 |
-
input_speed = gr.Slider(
|
183 |
-
minimum=0.1,
|
184 |
-
maximum=10,
|
185 |
-
value=1,
|
186 |
-
step=0.1,
|
187 |
-
label="Speed (larger->faster; smaller->slower)",
|
188 |
-
)
|
189 |
-
|
190 |
-
input_button = gr.Button("Submit")
|
191 |
-
|
192 |
-
output_audio = gr.Audio(label="Output")
|
193 |
-
|
194 |
-
output_info = gr.HTML(label="Info")
|
195 |
-
|
196 |
-
gr.Examples(
|
197 |
-
examples=examples,
|
198 |
-
fn=process,
|
199 |
-
inputs=[
|
200 |
-
language_radio,
|
201 |
-
model_dropdown,
|
202 |
-
input_text,
|
203 |
-
input_sid,
|
204 |
-
input_speed,
|
205 |
-
],
|
206 |
-
outputs=[
|
207 |
-
output_audio,
|
208 |
-
output_info,
|
209 |
-
],
|
210 |
-
)
|
211 |
-
|
212 |
-
input_button.click(
|
213 |
-
process,
|
214 |
-
inputs=[
|
215 |
-
language_radio,
|
216 |
-
model_dropdown,
|
217 |
-
input_text,
|
218 |
-
input_sid,
|
219 |
-
input_speed,
|
220 |
-
],
|
221 |
-
outputs=[
|
222 |
-
output_audio,
|
223 |
-
output_info,
|
224 |
-
],
|
225 |
-
)
|
226 |
-
|
227 |
-
gr.Markdown(description)
|
228 |
-
|
229 |
-
|
230 |
def download_espeak_ng_data():
|
231 |
os.system(
|
232 |
"""
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
)
|
238 |
|
239 |
-
|
240 |
if __name__ == "__main__":
|
241 |
download_espeak_ng_data()
|
242 |
-
|
243 |
-
|
244 |
-
logging.basicConfig(format=formatter, level=logging.INFO)
|
245 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import logging
|
2 |
import os
|
|
|
3 |
import uuid
|
4 |
+
import time
|
5 |
import gradio as gr
|
6 |
import soundfile as sf
|
7 |
|
8 |
from model import get_pretrained_model, language_to_models
|
9 |
|
10 |
+
# Function to update model dropdown based on language selection
|
11 |
+
def update_model_dropdown(language):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if language in language_to_models:
|
13 |
+
return gr.Dropdown.update(choices=language_to_models[language])
|
14 |
+
else:
|
15 |
+
raise ValueError(f"Unsupported language: {language}")
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Function to process text to speech conversion
|
18 |
+
def process(language, repo_id, text, sid, speed):
|
19 |
+
logging.info(f"Input text: {text}, SID: {sid}, Speed: {speed}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
sid = int(sid)
|
21 |
tts = get_pretrained_model(repo_id, speed)
|
22 |
|
23 |
start = time.time()
|
24 |
audio = tts.generate(text, sid=sid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
duration = len(audio.samples) / audio.sample_rate
|
26 |
+
elapsed_seconds = time.time() - start
|
|
|
27 |
rtf = elapsed_seconds / duration
|
28 |
|
29 |
info = f"""
|
30 |
+
Wave duration: {duration:.3f} s<br/>
|
31 |
+
Processing time: {elapsed_seconds:.3f} s<br/>
|
32 |
+
RTF: {rtf:.3f}<br/>
|
33 |
"""
|
34 |
|
35 |
logging.info(info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
filename = f"{uuid.uuid4()}.wav"
|
38 |
+
sf.write(filename, audio.samples, samplerate=audio.sample_rate, subtype="PCM_16")
|
39 |
+
return filename, info
|
40 |
|
41 |
+
# Interface layout
|
42 |
+
demo = gr.Blocks()
|
43 |
|
44 |
with demo:
|
45 |
+
gr.Markdown("# Text to Voice")
|
46 |
+
gr.Markdown("High Fidelity TTS. Visit <a href='https://ruslanmv.com/' target='_blank'>ruslanmv.com</a> for more information.")
|
47 |
+
|
48 |
+
language_radio = gr.Radio(label="Language", choices=list(language_to_models.keys()))
|
49 |
+
model_dropdown = gr.Dropdown(label="Select a model", choices=language_to_models[list(language_to_models.keys())[0]])
|
50 |
+
|
51 |
+
language_radio.change(update_model_dropdown, inputs=language_radio, outputs=model_dropdown)
|
52 |
+
|
53 |
+
input_text = gr.Textbox(lines=10, label="Enter text to convert to speech")
|
54 |
+
input_sid = gr.Textbox(label="Speaker ID", value="0", placeholder="Valid only for multi-speaker model")
|
55 |
+
input_speed = gr.Slider(minimum=0.1, maximum=10, value=1, step=0.1, label="Speed (larger->faster; smaller->slower)")
|
56 |
+
|
57 |
+
output_audio = gr.Audio(label="Generated audio")
|
58 |
+
output_info = gr.HTML(label="Info")
|
59 |
+
|
60 |
+
input_button = gr.Button("Submit")
|
61 |
+
input_button.click(process, inputs=[language_radio, model_dropdown, input_text, input_sid, input_speed], outputs=[output_audio, output_info])
|
62 |
+
|
63 |
+
gr.Examples(
|
64 |
+
examples=[
|
65 |
+
["Chinese (Mandarin, 普通话)", "csukuangfj/vits-zh-hf-fanchen-wnj|1", "在一个阳光明媚的夏天...", 0, 1.0],
|
66 |
+
["Thai", "csukuangfj/vits-mms-tha", "ฉันรักคุณ", 0, 1.0],
|
67 |
+
],
|
68 |
+
inputs=[language_radio, model_dropdown, input_text, input_sid, input_speed],
|
69 |
+
outputs=[output_audio, output_info],
|
70 |
)
|
71 |
|
72 |
+
# Download necessary data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
def download_espeak_ng_data():
|
74 |
os.system(
|
75 |
"""
|
76 |
+
cd /tmp
|
77 |
+
wget -qq https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/espeak-ng-data.tar.bz2
|
78 |
+
tar xf espeak-ng-data.tar.bz2
|
79 |
+
"""
|
80 |
)
|
81 |
|
|
|
82 |
if __name__ == "__main__":
|
83 |
download_espeak_ng_data()
|
84 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
85 |
demo.launch()
|