Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,3 @@
|
|
1 |
-
#!/usr/bin/env python3
|
2 |
-
#
|
3 |
-
# Copyright 2022-2023 Xiaomi Corp. (authors: Fangjun Kuang)
|
4 |
-
#
|
5 |
-
# See LICENSE for clarification regarding multiple authors
|
6 |
-
#
|
7 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
8 |
-
# you may not use this file except in compliance with the License.
|
9 |
-
# You may obtain a copy of the License at
|
10 |
-
#
|
11 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
12 |
-
#
|
13 |
-
# Unless required by applicable law or agreed to in writing, software
|
14 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
15 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16 |
-
# See the License for the specific language governing permissions and
|
17 |
-
# limitations under the License.
|
18 |
-
|
19 |
-
# References:
|
20 |
-
# https://gradio.app/docs/#dropdown
|
21 |
-
|
22 |
import logging
|
23 |
import os
|
24 |
import time
|
@@ -51,11 +30,8 @@ If you want to use Android text-to-speech engine APKs, please see
|
|
51 |
|
52 |
If you want to download an all-in-one exe for Windows, please see
|
53 |
<https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models>
|
54 |
-
|
55 |
"""
|
56 |
|
57 |
-
# css style is copied from
|
58 |
-
# https://huggingface.co/spaces/alphacep/asr/blob/main/app.py#L113
|
59 |
css = """
|
60 |
.result {display:flex;flex-direction:column}
|
61 |
.result_item {padding:15px;margin-bottom:8px;border-radius:15px;width:100%}
|
@@ -63,98 +39,34 @@ css = """
|
|
63 |
.result_item_error {background-color:#ff7070;color:white;align-self:start}
|
64 |
"""
|
65 |
|
|
|
66 |
examples = [
|
67 |
-
["
|
68 |
-
["Thai", "csukuangfj/vits-mms-tha", "ฉันรักคุณ", 0, 1.0],
|
69 |
]
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
if language in language_to_models:
|
74 |
-
choices = language_to_models[language]
|
75 |
-
return gr.Dropdown(
|
76 |
-
choices=choices,
|
77 |
-
value=choices[0],
|
78 |
-
interactive=True,
|
79 |
-
)
|
80 |
-
|
81 |
-
raise ValueError(f"Unsupported language: {language}")
|
82 |
-
|
83 |
-
|
84 |
-
def build_html_output(s: str, style: str = "result_item_success"):
|
85 |
-
return f"""
|
86 |
-
<div class='result'>
|
87 |
-
<div class='result_item {style}'>
|
88 |
-
{s}
|
89 |
-
</div>
|
90 |
-
</div>
|
91 |
-
"""
|
92 |
-
|
93 |
-
|
94 |
-
def process(language: str, repo_id: str, text: str, sid: str, speed: float):
|
95 |
-
logging.info(f"Input text: {text}. sid: {sid}, speed: {speed}")
|
96 |
-
sid = int(sid)
|
97 |
-
tts = get_pretrained_model(repo_id, speed)
|
98 |
-
|
99 |
-
start = time.time()
|
100 |
-
audio = tts.generate(text, sid=sid)
|
101 |
-
end = time.time()
|
102 |
-
|
103 |
-
if len(audio.samples) == 0:
|
104 |
-
raise ValueError(
|
105 |
-
"Error in generating audios. Please read previous error messages."
|
106 |
-
)
|
107 |
-
|
108 |
-
duration = len(audio.samples) / audio.sample_rate
|
109 |
-
|
110 |
-
elapsed_seconds = end - start
|
111 |
-
rtf = elapsed_seconds / duration
|
112 |
-
|
113 |
-
info = f"""
|
114 |
-
Wave duration : {duration:.3f} s <br/>
|
115 |
-
Processing time: {elapsed_seconds:.3f} s <br/>
|
116 |
-
RTF: {elapsed_seconds:.3f}/{duration:.3f} = {rtf:.3f} <br/>
|
117 |
-
"""
|
118 |
-
|
119 |
-
logging.info(info)
|
120 |
-
logging.info(f"\nrepo_id: {repo_id}\ntext: {text}\nsid: {sid}\nspeed: {speed}")
|
121 |
-
|
122 |
-
filename = str(uuid.uuid4())
|
123 |
-
filename = f"{filename}.wav"
|
124 |
-
sf.write(
|
125 |
-
filename,
|
126 |
-
audio.samples,
|
127 |
-
samplerate=audio.sample_rate,
|
128 |
-
subtype="PCM_16",
|
129 |
-
)
|
130 |
-
|
131 |
-
return filename, build_html_output(info)
|
132 |
-
|
133 |
|
134 |
demo = gr.Blocks(css=css)
|
135 |
|
136 |
-
|
137 |
with demo:
|
138 |
gr.Markdown(title)
|
139 |
-
|
140 |
-
|
141 |
language_radio = gr.Radio(
|
142 |
label="Language",
|
143 |
choices=language_choices,
|
144 |
value=language_choices[0],
|
145 |
)
|
146 |
|
|
|
147 |
model_dropdown = gr.Dropdown(
|
148 |
-
choices=language_to_models[
|
149 |
label="Select a model",
|
150 |
-
value=language_to_models[
|
151 |
)
|
152 |
|
153 |
-
|
154 |
-
update_model_dropdown,
|
155 |
-
inputs=language_radio,
|
156 |
-
outputs=model_dropdown,
|
157 |
-
)
|
158 |
|
159 |
with gr.Tabs():
|
160 |
with gr.TabItem("Please input your text"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import logging
|
2 |
import os
|
3 |
import time
|
|
|
30 |
|
31 |
If you want to download an all-in-one exe for Windows, please see
|
32 |
<https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models>
|
|
|
33 |
"""
|
34 |
|
|
|
|
|
35 |
css = """
|
36 |
.result {display:flex;flex-direction:column}
|
37 |
.result_item {padding:15px;margin-bottom:8px;border-radius:15px;width:100%}
|
|
|
39 |
.result_item_error {background-color:#ff7070;color:white;align-self:start}
|
40 |
"""
|
41 |
|
42 |
+
# Simplified examples for Portuguese only
|
43 |
examples = [
|
44 |
+
["Portuguese", "csukuangfj/vits-mms-por", "Eu desejo uma versão simplificada para português.", 0, 1.0],
|
|
|
45 |
]
|
46 |
|
47 |
+
# Use only Portuguese as a language choice
|
48 |
+
language_choices = ["Portuguese"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
demo = gr.Blocks(css=css)
|
51 |
|
|
|
52 |
with demo:
|
53 |
gr.Markdown(title)
|
54 |
+
|
55 |
+
# Use Radio instead of Dropdown for language choice
|
56 |
language_radio = gr.Radio(
|
57 |
label="Language",
|
58 |
choices=language_choices,
|
59 |
value=language_choices[0],
|
60 |
)
|
61 |
|
62 |
+
# Initialize model_dropdown with Portuguese models
|
63 |
model_dropdown = gr.Dropdown(
|
64 |
+
choices=language_to_models["Portuguese"],
|
65 |
label="Select a model",
|
66 |
+
value=language_to_models["Portuguese"][0],
|
67 |
)
|
68 |
|
69 |
+
# No need to update model_dropdown for a single language
|
|
|
|
|
|
|
|
|
70 |
|
71 |
with gr.Tabs():
|
72 |
with gr.TabItem("Please input your text"):
|