Spaces:
Runtime error
Runtime error
ulysses115
commited on
Commit
•
d50849f
1
Parent(s):
d81c6c6
Update app.py
Browse files
app.py
CHANGED
@@ -1,123 +1,123 @@
|
|
1 |
-
# coding=utf-8
|
2 |
-
import time
|
3 |
-
import gradio as gr
|
4 |
-
import utils
|
5 |
-
import commons
|
6 |
-
from models import SynthesizerTrn
|
7 |
-
from text import text_to_sequence
|
8 |
-
from torch import no_grad, LongTensor
|
9 |
-
|
10 |
-
hps_ms = utils.get_hparams_from_file(r'./model/config.json')
|
11 |
-
net_g_ms = SynthesizerTrn(
|
12 |
-
len(hps_ms.symbols),
|
13 |
-
hps_ms.data.filter_length // 2 + 1,
|
14 |
-
hps_ms.train.segment_size // hps_ms.data.hop_length,
|
15 |
-
n_speakers=hps_ms.data.n_speakers,
|
16 |
-
**hps_ms.model)
|
17 |
-
_ = net_g_ms.eval()
|
18 |
-
speakers = hps_ms.speakers
|
19 |
-
model, optimizer, learning_rate, epochs = utils.load_checkpoint(r'./model/G_953000.pth', net_g_ms, None)
|
20 |
-
|
21 |
-
def get_text(text, hps):
|
22 |
-
text_norm, clean_text = text_to_sequence(text, hps.symbols, hps.data.text_cleaners)
|
23 |
-
if hps.data.add_blank:
|
24 |
-
text_norm = commons.intersperse(text_norm, 0)
|
25 |
-
text_norm = LongTensor(text_norm)
|
26 |
-
return text_norm, clean_text
|
27 |
-
|
28 |
-
def vits(text, language, speaker_id, noise_scale, noise_scale_w, length_scale):
|
29 |
-
start = time.perf_counter()
|
30 |
-
if not len(text):
|
31 |
-
return "输入文本不能为空!", None, None
|
32 |
-
text = text.replace('\n', ' ').replace('\r', '').replace(" ", "")
|
33 |
-
if len(text) > 100:
|
34 |
-
|
35 |
-
if language == 0:
|
36 |
-
text = f"[ZH]{text}[ZH]"
|
37 |
-
elif language == 1:
|
38 |
-
text = f"[JA]{text}[JA]"
|
39 |
-
else:
|
40 |
-
text = f"{text}"
|
41 |
-
stn_tst, clean_text = get_text(text, hps_ms)
|
42 |
-
with no_grad():
|
43 |
-
x_tst = stn_tst.unsqueeze(0)
|
44 |
-
x_tst_lengths = LongTensor([stn_tst.size(0)])
|
45 |
-
speaker_id = LongTensor([speaker_id])
|
46 |
-
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=speaker_id, noise_scale=noise_scale, noise_scale_w=noise_scale_w,
|
47 |
-
length_scale=length_scale)[0][0, 0].data.float().numpy()
|
48 |
-
|
49 |
-
return "生成成功!", (22050, audio), f"生成耗时 {round(time.perf_counter()-start, 2)} s"
|
50 |
-
|
51 |
-
def search_speaker(search_value):
|
52 |
-
for s in speakers:
|
53 |
-
if search_value == s:
|
54 |
-
return s
|
55 |
-
for s in speakers:
|
56 |
-
if search_value in s:
|
57 |
-
return s
|
58 |
-
|
59 |
-
def change_lang(language):
|
60 |
-
if language == 0:
|
61 |
-
return 0.6, 0.668, 1.2
|
62 |
-
else:
|
63 |
-
return 0.6, 0.668, 1.1
|
64 |
-
|
65 |
-
download_audio_js = """
|
66 |
-
() =>{{
|
67 |
-
let root = document.querySelector("body > gradio-app");
|
68 |
-
if (root.shadowRoot != null)
|
69 |
-
root = root.shadowRoot;
|
70 |
-
let audio = root.querySelector("#tts-audio").querySelector("audio");
|
71 |
-
let text = root.querySelector("#input-text").querySelector("textarea");
|
72 |
-
if (audio == undefined)
|
73 |
-
return;
|
74 |
-
text = text.value;
|
75 |
-
if (text == undefined)
|
76 |
-
text = Math.floor(Math.random()*100000000);
|
77 |
-
audio = audio.src;
|
78 |
-
let oA = document.createElement("a");
|
79 |
-
oA.download = text.substr(0, 20)+'.wav';
|
80 |
-
oA.href = audio;
|
81 |
-
document.body.appendChild(oA);
|
82 |
-
oA.click();
|
83 |
-
oA.remove();
|
84 |
-
}}
|
85 |
-
"""
|
86 |
-
|
87 |
-
if __name__ == '__main__':
|
88 |
-
with gr.Blocks() as app:
|
89 |
-
gr.Markdown(
|
90 |
-
"# <center> VITS语音在线合成demo\n"
|
91 |
-
"<div align='center'>主要有赛马娘,原神中文,原神日语,崩坏3的音色</div>"
|
92 |
-
'<div align="center"><a><font color="#dd0000">结果有随机性,语调可能很奇怪,可多次生成取最佳效果</font></a></div>'
|
93 |
-
'<div align="center"><a><font color="#dd0000">标点符号会影响生成的结果</font></a></div>'
|
94 |
-
)
|
95 |
-
|
96 |
-
with gr.Tabs():
|
97 |
-
with gr.TabItem("vits"):
|
98 |
-
with gr.Row():
|
99 |
-
with gr.Column():
|
100 |
-
input_text = gr.Textbox(label="Text (100 words limitation)", lines=5, value="今天晚上吃啥好呢。", elem_id=f"input-text")
|
101 |
-
lang = gr.Dropdown(label="Language", choices=["中文", "日语", "中日混合(中文用[ZH][ZH]包裹起来,日文用[JA][JA]包裹起来)"],
|
102 |
-
type="index", value="中文")
|
103 |
-
btn = gr.Button(value="Submit")
|
104 |
-
with gr.Row():
|
105 |
-
search = gr.Textbox(label="Search Speaker", lines=1)
|
106 |
-
btn2 = gr.Button(value="Search")
|
107 |
-
sid = gr.Dropdown(label="Speaker", choices=speakers, type="index", value=speakers[228])
|
108 |
-
with gr.Row():
|
109 |
-
ns = gr.Slider(label="noise_scale(控制感情变化程度)", minimum=0.1, maximum=1.0, step=0.1, value=0.6, interactive=True)
|
110 |
-
nsw = gr.Slider(label="noise_scale_w(控制音素发音长度)", minimum=0.1, maximum=1.0, step=0.1, value=0.668, interactive=True)
|
111 |
-
ls = gr.Slider(label="length_scale(控制整体语速)", minimum=0.1, maximum=2.0, step=0.1, value=1.2, interactive=True)
|
112 |
-
with gr.Column():
|
113 |
-
o1 = gr.Textbox(label="Output Message")
|
114 |
-
o2 = gr.Audio(label="Output Audio", elem_id=f"tts-audio")
|
115 |
-
o3 = gr.Textbox(label="Extra Info")
|
116 |
-
download = gr.Button("Download Audio")
|
117 |
-
btn.click(vits, inputs=[input_text, lang, sid, ns, nsw, ls], outputs=[o1, o2, o3])
|
118 |
-
download.click(None, [], [], _js=download_audio_js.format())
|
119 |
-
btn2.click(search_speaker, inputs=[search], outputs=[sid])
|
120 |
-
lang.change(change_lang, inputs=[lang], outputs=[ns, nsw, ls])
|
121 |
-
with gr.TabItem("可用人物一览"):
|
122 |
-
gr.Radio(label="Speaker", choices=speakers, interactive=False, type="index")
|
123 |
-
app.queue(concurrency_count=1).launch()
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
import time
|
3 |
+
import gradio as gr
|
4 |
+
import utils
|
5 |
+
import commons
|
6 |
+
from models import SynthesizerTrn
|
7 |
+
from text import text_to_sequence
|
8 |
+
from torch import no_grad, LongTensor
|
9 |
+
|
10 |
+
hps_ms = utils.get_hparams_from_file(r'./model/config.json')
|
11 |
+
net_g_ms = SynthesizerTrn(
|
12 |
+
len(hps_ms.symbols),
|
13 |
+
hps_ms.data.filter_length // 2 + 1,
|
14 |
+
hps_ms.train.segment_size // hps_ms.data.hop_length,
|
15 |
+
n_speakers=hps_ms.data.n_speakers,
|
16 |
+
**hps_ms.model)
|
17 |
+
_ = net_g_ms.eval()
|
18 |
+
speakers = hps_ms.speakers
|
19 |
+
model, optimizer, learning_rate, epochs = utils.load_checkpoint(r'./model/G_953000.pth', net_g_ms, None)
|
20 |
+
|
21 |
+
def get_text(text, hps):
|
22 |
+
text_norm, clean_text = text_to_sequence(text, hps.symbols, hps.data.text_cleaners)
|
23 |
+
if hps.data.add_blank:
|
24 |
+
text_norm = commons.intersperse(text_norm, 0)
|
25 |
+
text_norm = LongTensor(text_norm)
|
26 |
+
return text_norm, clean_text
|
27 |
+
|
28 |
+
def vits(text, language, speaker_id, noise_scale, noise_scale_w, length_scale):
|
29 |
+
start = time.perf_counter()
|
30 |
+
if not len(text):
|
31 |
+
return "输入文本不能为空!", None, None
|
32 |
+
text = text.replace('\n', ' ').replace('\r', '').replace(" ", "")
|
33 |
+
# if len(text) > 100:
|
34 |
+
# return f"输入文字过长!{len(text)}>100", None, None
|
35 |
+
if language == 0:
|
36 |
+
text = f"[ZH]{text}[ZH]"
|
37 |
+
elif language == 1:
|
38 |
+
text = f"[JA]{text}[JA]"
|
39 |
+
else:
|
40 |
+
text = f"{text}"
|
41 |
+
stn_tst, clean_text = get_text(text, hps_ms)
|
42 |
+
with no_grad():
|
43 |
+
x_tst = stn_tst.unsqueeze(0)
|
44 |
+
x_tst_lengths = LongTensor([stn_tst.size(0)])
|
45 |
+
speaker_id = LongTensor([speaker_id])
|
46 |
+
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=speaker_id, noise_scale=noise_scale, noise_scale_w=noise_scale_w,
|
47 |
+
length_scale=length_scale)[0][0, 0].data.float().numpy()
|
48 |
+
|
49 |
+
return "生成成功!", (22050, audio), f"生成耗时 {round(time.perf_counter()-start, 2)} s"
|
50 |
+
|
51 |
+
def search_speaker(search_value):
|
52 |
+
for s in speakers:
|
53 |
+
if search_value == s:
|
54 |
+
return s
|
55 |
+
for s in speakers:
|
56 |
+
if search_value in s:
|
57 |
+
return s
|
58 |
+
|
59 |
+
def change_lang(language):
|
60 |
+
if language == 0:
|
61 |
+
return 0.6, 0.668, 1.2
|
62 |
+
else:
|
63 |
+
return 0.6, 0.668, 1.1
|
64 |
+
|
65 |
+
download_audio_js = """
|
66 |
+
() =>{{
|
67 |
+
let root = document.querySelector("body > gradio-app");
|
68 |
+
if (root.shadowRoot != null)
|
69 |
+
root = root.shadowRoot;
|
70 |
+
let audio = root.querySelector("#tts-audio").querySelector("audio");
|
71 |
+
let text = root.querySelector("#input-text").querySelector("textarea");
|
72 |
+
if (audio == undefined)
|
73 |
+
return;
|
74 |
+
text = text.value;
|
75 |
+
if (text == undefined)
|
76 |
+
text = Math.floor(Math.random()*100000000);
|
77 |
+
audio = audio.src;
|
78 |
+
let oA = document.createElement("a");
|
79 |
+
oA.download = text.substr(0, 20)+'.wav';
|
80 |
+
oA.href = audio;
|
81 |
+
document.body.appendChild(oA);
|
82 |
+
oA.click();
|
83 |
+
oA.remove();
|
84 |
+
}}
|
85 |
+
"""
|
86 |
+
|
87 |
+
if __name__ == '__main__':
|
88 |
+
with gr.Blocks() as app:
|
89 |
+
gr.Markdown(
|
90 |
+
"# <center> VITS语音在线合成demo\n"
|
91 |
+
"<div align='center'>主要有赛马娘,原神中文,原神日语,崩坏3的音色</div>"
|
92 |
+
'<div align="center"><a><font color="#dd0000">结果有随机性,语调可能很奇怪,可多次生成取最佳效果</font></a></div>'
|
93 |
+
'<div align="center"><a><font color="#dd0000">标点符号会影响生成的结果</font></a></div>'
|
94 |
+
)
|
95 |
+
|
96 |
+
with gr.Tabs():
|
97 |
+
with gr.TabItem("vits"):
|
98 |
+
with gr.Row():
|
99 |
+
with gr.Column():
|
100 |
+
input_text = gr.Textbox(label="Text (100 words limitation)", lines=5, value="今天晚上吃啥好呢。", elem_id=f"input-text")
|
101 |
+
lang = gr.Dropdown(label="Language", choices=["中文", "日语", "中日混合(中文用[ZH][ZH]包裹起来,日文用[JA][JA]包裹起来)"],
|
102 |
+
type="index", value="中文")
|
103 |
+
btn = gr.Button(value="Submit")
|
104 |
+
with gr.Row():
|
105 |
+
search = gr.Textbox(label="Search Speaker", lines=1)
|
106 |
+
btn2 = gr.Button(value="Search")
|
107 |
+
sid = gr.Dropdown(label="Speaker", choices=speakers, type="index", value=speakers[228])
|
108 |
+
with gr.Row():
|
109 |
+
ns = gr.Slider(label="noise_scale(控制感情变化程度)", minimum=0.1, maximum=1.0, step=0.1, value=0.6, interactive=True)
|
110 |
+
nsw = gr.Slider(label="noise_scale_w(控制音素发音长度)", minimum=0.1, maximum=1.0, step=0.1, value=0.668, interactive=True)
|
111 |
+
ls = gr.Slider(label="length_scale(控制整体语速)", minimum=0.1, maximum=2.0, step=0.1, value=1.2, interactive=True)
|
112 |
+
with gr.Column():
|
113 |
+
o1 = gr.Textbox(label="Output Message")
|
114 |
+
o2 = gr.Audio(label="Output Audio", elem_id=f"tts-audio")
|
115 |
+
o3 = gr.Textbox(label="Extra Info")
|
116 |
+
download = gr.Button("Download Audio")
|
117 |
+
btn.click(vits, inputs=[input_text, lang, sid, ns, nsw, ls], outputs=[o1, o2, o3])
|
118 |
+
download.click(None, [], [], _js=download_audio_js.format())
|
119 |
+
btn2.click(search_speaker, inputs=[search], outputs=[sid])
|
120 |
+
lang.change(change_lang, inputs=[lang], outputs=[ns, nsw, ls])
|
121 |
+
with gr.TabItem("可用人物一览"):
|
122 |
+
gr.Radio(label="Speaker", choices=speakers, interactive=False, type="index")
|
123 |
+
app.queue(concurrency_count=1).launch()
|