Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,274 +1,230 @@
|
|
1 |
-
#!/usr/bin/env python3
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
from
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
.
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
model_dropdown,
|
232 |
-
input_text,
|
233 |
-
input_sid,
|
234 |
-
input_speed,
|
235 |
-
],
|
236 |
-
outputs=[
|
237 |
-
output_audio,
|
238 |
-
output_info,
|
239 |
-
],
|
240 |
-
)
|
241 |
-
|
242 |
-
input_button.click(
|
243 |
-
process,
|
244 |
-
inputs=[
|
245 |
-
language_radio,
|
246 |
-
model_dropdown,
|
247 |
-
input_text,
|
248 |
-
input_sid,
|
249 |
-
input_speed,
|
250 |
-
],
|
251 |
-
outputs=[
|
252 |
-
output_audio,
|
253 |
-
output_info,
|
254 |
-
],
|
255 |
-
)
|
256 |
-
|
257 |
-
gr.Markdown(description)
|
258 |
-
|
259 |
-
|
260 |
-
def download_espeak_ng_data():
|
261 |
-
os.system(
|
262 |
-
"""
|
263 |
-
cd /tmp
|
264 |
-
wget -qq https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/espeak-ng-data.tar.bz2
|
265 |
-
tar xf espeak-ng-data.tar.bz2
|
266 |
-
"""
|
267 |
-
)
|
268 |
-
|
269 |
-
|
270 |
-
if __name__ == "__main__":
|
271 |
-
download_espeak_ng_data()
|
272 |
-
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
|
273 |
-
|
274 |
-
demo.launch()
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
import uuid
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import soundfile as sf
|
10 |
+
|
11 |
+
from model import get_pretrained_model, language_to_models
|
12 |
+
|
13 |
+
|
14 |
+
def MyPrint(s):
|
15 |
+
now = datetime.now()
|
16 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")
|
17 |
+
print(f"{date_time}: {s}")
|
18 |
+
|
19 |
+
|
20 |
+
title = "# Next-gen Kaldi: Text-to-speech (TTS)"
|
21 |
+
|
22 |
+
description = """
|
23 |
+
This space shows how to convert text to speech with Next-gen Kaldi (Sherpa-ONNX).
|
24 |
+
|
25 |
+
Running on CPU in a Docker container from Hugging Face.
|
26 |
+
|
27 |
+
Useful links:
|
28 |
+
|
29 |
+
- <https://github.com/k2-fsa/sherpa-onnx>
|
30 |
+
- <https://k2-fsa.github.io/sherpa/>
|
31 |
+
- <https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models>
|
32 |
+
"""
|
33 |
+
|
34 |
+
css = """
|
35 |
+
.result {display:flex;flex-direction:column}
|
36 |
+
.result_item {padding:15px;margin-bottom:8px;border-radius:15px;width:100%}
|
37 |
+
.result_item_success {background-color:mediumaquamarine;color:white;align-self:start}
|
38 |
+
.result_item_error {background-color:#ff7070;color:white;align-self:start}
|
39 |
+
"""
|
40 |
+
|
41 |
+
examples = [
|
42 |
+
[
|
43 |
+
"English",
|
44 |
+
"csukuangfj/vits-piper-en_US-libritts-rhea|1 speaker",
|
45 |
+
"Hello! This is a test of the text-to-speech demo powered by Sherpa-ONNX.",
|
46 |
+
0,
|
47 |
+
1.0,
|
48 |
+
],
|
49 |
+
[
|
50 |
+
"English",
|
51 |
+
"csukuangfj/vits-piper-en_GB-alba|1 speaker",
|
52 |
+
"Welcome to the world of AI voice generation. Enjoy the experience.",
|
53 |
+
0,
|
54 |
+
1.0,
|
55 |
+
],
|
56 |
+
[
|
57 |
+
"English",
|
58 |
+
"csukuangfj/vits-piper-en_US-lessac-medium|1 speaker",
|
59 |
+
"Real-time voice synthesis helps developers build smarter applications.",
|
60 |
+
0,
|
61 |
+
1.0,
|
62 |
+
],
|
63 |
+
]
|
64 |
+
|
65 |
+
|
66 |
+
def update_model_dropdown(language: str):
|
67 |
+
if language in language_to_models:
|
68 |
+
choices = language_to_models[language]
|
69 |
+
return gr.Dropdown(
|
70 |
+
choices=choices,
|
71 |
+
value=choices[0],
|
72 |
+
interactive=True,
|
73 |
+
)
|
74 |
+
|
75 |
+
raise ValueError(f"Unsupported language: {language}")
|
76 |
+
|
77 |
+
|
78 |
+
def build_html_output(s: str, style: str = "result_item_success"):
|
79 |
+
return f"""
|
80 |
+
<div class='result'>
|
81 |
+
<div class='result_item {style}'>
|
82 |
+
{s}
|
83 |
+
</div>
|
84 |
+
</div>
|
85 |
+
"""
|
86 |
+
|
87 |
+
|
88 |
+
def process(language: str, repo_id: str, text: str, sid: str, speed: float):
|
89 |
+
MyPrint(f"Input text: {text}. sid: {sid}, speed: {speed}")
|
90 |
+
sid = int(sid)
|
91 |
+
tts = get_pretrained_model(repo_id, speed)
|
92 |
+
|
93 |
+
start = time.time()
|
94 |
+
audio = tts.generate(text, sid=sid)
|
95 |
+
end = time.time()
|
96 |
+
|
97 |
+
if len(audio.samples) == 0:
|
98 |
+
raise ValueError(
|
99 |
+
"Error in generating audios. Please read previous error messages."
|
100 |
+
)
|
101 |
+
|
102 |
+
duration = len(audio.samples) / audio.sample_rate
|
103 |
+
|
104 |
+
elapsed_seconds = end - start
|
105 |
+
rtf = elapsed_seconds / duration
|
106 |
+
|
107 |
+
info = f"""
|
108 |
+
Wave duration : {duration:.3f} s <br/>
|
109 |
+
Processing time: {elapsed_seconds:.3f} s <br/>
|
110 |
+
RTF: {elapsed_seconds:.3f}/{duration:.3f} = {rtf:.3f} <br/>
|
111 |
+
"""
|
112 |
+
|
113 |
+
MyPrint(info)
|
114 |
+
MyPrint(f"\nrepo_id: {repo_id}\ntext: {text}\nsid: {sid}\nspeed: {speed}")
|
115 |
+
|
116 |
+
filename = str(uuid.uuid4())
|
117 |
+
filename = f"{filename}.wav"
|
118 |
+
sf.write(
|
119 |
+
filename,
|
120 |
+
audio.samples,
|
121 |
+
samplerate=audio.sample_rate,
|
122 |
+
subtype="PCM_16",
|
123 |
+
)
|
124 |
+
|
125 |
+
return filename, build_html_output(info)
|
126 |
+
|
127 |
+
|
128 |
+
demo = gr.Blocks(css=css)
|
129 |
+
|
130 |
+
with demo:
|
131 |
+
gr.Markdown(title)
|
132 |
+
language_choices = list(language_to_models.keys())
|
133 |
+
|
134 |
+
language_radio = gr.Radio(
|
135 |
+
label="Language",
|
136 |
+
choices=language_choices,
|
137 |
+
value="English", # Default language
|
138 |
+
)
|
139 |
+
|
140 |
+
model_dropdown = gr.Dropdown(
|
141 |
+
choices=language_to_models["English"], # Default model list
|
142 |
+
label="Select a model",
|
143 |
+
value=language_to_models["English"][0], # Default model
|
144 |
+
)
|
145 |
+
|
146 |
+
language_radio.change(
|
147 |
+
update_model_dropdown,
|
148 |
+
inputs=language_radio,
|
149 |
+
outputs=model_dropdown,
|
150 |
+
)
|
151 |
+
|
152 |
+
with gr.Tabs():
|
153 |
+
with gr.TabItem("Please input your text"):
|
154 |
+
input_text = gr.Textbox(
|
155 |
+
label="Input text",
|
156 |
+
info="Your text",
|
157 |
+
lines=3,
|
158 |
+
placeholder="Please input your text here",
|
159 |
+
)
|
160 |
+
|
161 |
+
input_sid = gr.Textbox(
|
162 |
+
label="Speaker ID",
|
163 |
+
info="Speaker ID",
|
164 |
+
lines=1,
|
165 |
+
max_lines=1,
|
166 |
+
value="0",
|
167 |
+
placeholder="Speaker ID. Valid only for multi-speaker model",
|
168 |
+
)
|
169 |
+
|
170 |
+
input_speed = gr.Slider(
|
171 |
+
minimum=0.1,
|
172 |
+
maximum=10,
|
173 |
+
value=1,
|
174 |
+
step=0.1,
|
175 |
+
label="Speed (larger->faster; smaller->slower)",
|
176 |
+
)
|
177 |
+
|
178 |
+
input_button = gr.Button("Submit")
|
179 |
+
|
180 |
+
output_audio = gr.Audio(label="Output")
|
181 |
+
|
182 |
+
output_info = gr.HTML(label="Info")
|
183 |
+
|
184 |
+
gr.Examples(
|
185 |
+
examples=examples,
|
186 |
+
fn=process,
|
187 |
+
inputs=[
|
188 |
+
language_radio,
|
189 |
+
model_dropdown,
|
190 |
+
input_text,
|
191 |
+
input_sid,
|
192 |
+
input_speed,
|
193 |
+
],
|
194 |
+
outputs=[
|
195 |
+
output_audio,
|
196 |
+
output_info,
|
197 |
+
],
|
198 |
+
)
|
199 |
+
|
200 |
+
input_button.click(
|
201 |
+
process,
|
202 |
+
inputs=[
|
203 |
+
language_radio,
|
204 |
+
model_dropdown,
|
205 |
+
input_text,
|
206 |
+
input_sid,
|
207 |
+
input_speed,
|
208 |
+
],
|
209 |
+
outputs=[
|
210 |
+
output_audio,
|
211 |
+
output_info,
|
212 |
+
],
|
213 |
+
)
|
214 |
+
|
215 |
+
gr.Markdown(description)
|
216 |
+
|
217 |
+
|
218 |
+
def download_espeak_ng_data():
|
219 |
+
os.system(
|
220 |
+
"""
|
221 |
+
cd /tmp
|
222 |
+
wget -qq https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/espeak-ng-data.tar.bz2
|
223 |
+
tar xf espeak-ng-data.tar.bz2
|
224 |
+
"""
|
225 |
+
)
|
226 |
+
|
227 |
+
|
228 |
+
if __name__ == "__main__":
|
229 |
+
download_espeak_ng_data()
|
230 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|