Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- README.md +3 -3
- __pycache__/infer.cpython-39.pyc +0 -0
- app.py +73 -55
- assets/audios/nomorethan100.wav +0 -0
- assets/audios/overlength.wav +0 -0
- tools/__pycache__/webui.cpython-39.pyc +0 -0
- tools/webui.py +64 -0
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🌼🌼🌼
|
|
4 |
app_file: app.py
|
5 |
sdk: gradio
|
6 |
sdk_version: 4.5.0
|
7 |
-
colorFrom:
|
8 |
-
colorTo:
|
9 |
pinned: true
|
10 |
-
---
|
|
|
4 |
app_file: app.py
|
5 |
sdk: gradio
|
6 |
sdk_version: 4.5.0
|
7 |
+
colorFrom: red
|
8 |
+
colorTo: blue
|
9 |
pinned: true
|
10 |
+
---
|
__pycache__/infer.cpython-39.pyc
CHANGED
Binary files a/__pycache__/infer.cpython-39.pyc and b/__pycache__/infer.cpython-39.pyc differ
|
|
app.py
CHANGED
@@ -10,82 +10,101 @@ import gradio as gr
|
|
10 |
import webbrowser
|
11 |
from config import config
|
12 |
from tools.translate import translate
|
13 |
-
|
14 |
-
from webui import reload_javascript
|
15 |
|
16 |
device = config.webui_config.device
|
17 |
if device == "mps":
|
18 |
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
19 |
|
20 |
-
def generate_audio(
|
21 |
-
slices,
|
22 |
-
sdp_ratio,
|
23 |
-
noise_scale,
|
24 |
-
noise_scale_w,
|
25 |
-
length_scale,
|
26 |
-
speaker,
|
27 |
-
language,
|
28 |
-
):
|
29 |
-
audio_list = []
|
30 |
-
silence = np.zeros(hps.data.sampling_rate // 2, dtype=np.int16)
|
31 |
-
with torch.no_grad():
|
32 |
-
for piece in slices:
|
33 |
-
audio = infer(
|
34 |
-
piece,
|
35 |
-
sdp_ratio=sdp_ratio,
|
36 |
-
noise_scale=noise_scale,
|
37 |
-
noise_scale_w=noise_scale_w,
|
38 |
-
length_scale=length_scale,
|
39 |
-
sid=speaker,
|
40 |
-
language=language,
|
41 |
-
hps=hps,
|
42 |
-
net_g=net_g,
|
43 |
-
device=device,
|
44 |
-
)
|
45 |
-
audio16bit = gr.processing_utils.convert_to_16_bit_wav(audio)
|
46 |
-
audio_list.append(audio16bit)
|
47 |
-
audio_list.append(silence) # 将静音添加到列表中
|
48 |
-
return audio_list
|
49 |
|
50 |
def speak_fn(
|
51 |
text: str,
|
|
|
52 |
speaker="TalkFlower_CNzh",
|
53 |
sdp_ratio=0.2, # SDP/DP混合比
|
54 |
noise_scale=0.6, # 感情
|
55 |
noise_scale_w=0.6, # 音素长度
|
56 |
length_scale=0.9, # 语速
|
57 |
-
language="ZH"
|
|
|
|
|
58 |
):
|
59 |
-
|
|
|
60 |
if len(text) > 100:
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
63 |
audio_list = []
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
audio_concat = np.concatenate(audio_list)
|
77 |
-
return (hps.data.sampling_rate, audio_concat)
|
78 |
|
79 |
|
80 |
def init_fn():
|
81 |
-
gr.Info("2023-11-
|
82 |
gr.Info("2023-11-23: Only support Chinese now. Trying to train a mutilingual model.")
|
83 |
|
|
|
84 |
with open("./css/style.css", "r", encoding="utf-8") as f:
|
85 |
customCSS = f.read()
|
86 |
|
87 |
with gr.Blocks(css=customCSS) as demo:
|
88 |
-
|
89 |
talkingFlowerPic = gr.HTML("""<img src="file=assets/flower-2x.webp" alt="TalkingFlowerPic">""", elem_id="talking_flower_pic")
|
90 |
input_text = gr.Textbox(lines=1, label="Talking Flower will say:", elem_classes="wonder-card", elem_id="input_text")
|
91 |
speak_button = gr.Button("Speak!", elem_id="comfirm_button", elem_classes="button wonder-card")
|
@@ -98,13 +117,13 @@ with gr.Blocks(css=customCSS) as demo:
|
|
98 |
)
|
99 |
input_text.submit(
|
100 |
speak_fn,
|
101 |
-
inputs=[input_text],
|
102 |
-
outputs=[audio_output],
|
103 |
)
|
104 |
speak_button.click(
|
105 |
speak_fn,
|
106 |
-
inputs=[input_text],
|
107 |
-
outputs=[audio_output],
|
108 |
)
|
109 |
|
110 |
|
@@ -118,6 +137,5 @@ if __name__ == "__main__":
|
|
118 |
show_api=False,
|
119 |
# server_name=server_name,
|
120 |
# server_port=server_port,
|
121 |
-
|
122 |
-
inbrowser=True, # 禁止在docker下开启inbrowser
|
123 |
)
|
|
|
10 |
import webbrowser
|
11 |
from config import config
|
12 |
from tools.translate import translate
|
13 |
+
from tools.webui import reload_javascript
|
|
|
14 |
|
15 |
device = config.webui_config.device
|
16 |
if device == "mps":
|
17 |
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def speak_fn(
|
21 |
text: str,
|
22 |
+
exceed_flag,
|
23 |
speaker="TalkFlower_CNzh",
|
24 |
sdp_ratio=0.2, # SDP/DP混合比
|
25 |
noise_scale=0.6, # 感情
|
26 |
noise_scale_w=0.6, # 音素长度
|
27 |
length_scale=0.9, # 语速
|
28 |
+
language="ZH",
|
29 |
+
interval_between_para=0.2, # 段间间隔
|
30 |
+
interval_between_sent=1, # 句间间隔
|
31 |
):
|
32 |
+
while text.find("\n\n") != -1:
|
33 |
+
text = text.replace("\n\n", "\n")
|
34 |
if len(text) > 100:
|
35 |
+
print(f"Too Long Text: {text}")
|
36 |
+
gr.Warning("Too long! No more than 100 characters. 一口气不要超过 100 个字,憋坏我了。")
|
37 |
+
if exceed_flag:
|
38 |
+
return gr.update(value="./assets/audios/nomorethan100.wav", autoplay=True), False
|
39 |
+
else:
|
40 |
+
return gr.update(value="./assets/audios/overlength.wav", autoplay=True), True
|
41 |
audio_list = []
|
42 |
+
if len(text) > 42:
|
43 |
+
print(f"Long Text: {text}")
|
44 |
+
para_list = re_matching.cut_para(text)
|
45 |
+
for p in para_list:
|
46 |
+
audio_list_sent = []
|
47 |
+
sent_list = re_matching.cut_sent(p)
|
48 |
+
for s in sent_list:
|
49 |
+
audio = infer(
|
50 |
+
s,
|
51 |
+
sdp_ratio=sdp_ratio,
|
52 |
+
noise_scale=noise_scale,
|
53 |
+
noise_scale_w=noise_scale_w,
|
54 |
+
length_scale=length_scale,
|
55 |
+
sid=speaker,
|
56 |
+
language=language,
|
57 |
+
hps=hps,
|
58 |
+
net_g=net_g,
|
59 |
+
device=device,
|
60 |
+
)
|
61 |
+
audio_list_sent.append(audio)
|
62 |
+
silence = np.zeros((int)(44100 * interval_between_sent))
|
63 |
+
audio_list_sent.append(silence)
|
64 |
+
if (interval_between_para - interval_between_sent) > 0:
|
65 |
+
silence = np.zeros(
|
66 |
+
(int)(44100 * (interval_between_para - interval_between_sent))
|
67 |
+
)
|
68 |
+
audio_list_sent.append(silence)
|
69 |
+
audio16bit = gr.processing_utils.convert_to_16_bit_wav(
|
70 |
+
np.concatenate(audio_list_sent)
|
71 |
+
) # 对完整句子做音量归一
|
72 |
+
audio_list.append(audio16bit)
|
73 |
+
else:
|
74 |
+
print(f"Short Text: {text}")
|
75 |
+
silence = np.zeros(hps.data.sampling_rate // 2, dtype=np.int16)
|
76 |
+
with torch.no_grad():
|
77 |
+
for piece in text.split("|"):
|
78 |
+
audio = infer(
|
79 |
+
piece,
|
80 |
+
sdp_ratio=sdp_ratio,
|
81 |
+
noise_scale=noise_scale,
|
82 |
+
noise_scale_w=noise_scale_w,
|
83 |
+
length_scale=length_scale,
|
84 |
+
sid=speaker,
|
85 |
+
language=language,
|
86 |
+
hps=hps,
|
87 |
+
net_g=net_g,
|
88 |
+
device=device,
|
89 |
+
)
|
90 |
+
audio16bit = gr.processing_utils.convert_to_16_bit_wav(audio)
|
91 |
+
audio_list.append(audio16bit)
|
92 |
+
audio_list.append(silence) # 将静音添加到列表中
|
93 |
|
94 |
audio_concat = np.concatenate(audio_list)
|
95 |
+
return (hps.data.sampling_rate, audio_concat), exceed_flag
|
96 |
|
97 |
|
98 |
def init_fn():
|
99 |
+
gr.Info("2023-11-24: 优化长句生成效果;更新了一些小��蛋。")
|
100 |
gr.Info("2023-11-23: Only support Chinese now. Trying to train a mutilingual model.")
|
101 |
|
102 |
+
|
103 |
with open("./css/style.css", "r", encoding="utf-8") as f:
|
104 |
customCSS = f.read()
|
105 |
|
106 |
with gr.Blocks(css=customCSS) as demo:
|
107 |
+
exceed_flag = gr.State(value=False)
|
108 |
talkingFlowerPic = gr.HTML("""<img src="file=assets/flower-2x.webp" alt="TalkingFlowerPic">""", elem_id="talking_flower_pic")
|
109 |
input_text = gr.Textbox(lines=1, label="Talking Flower will say:", elem_classes="wonder-card", elem_id="input_text")
|
110 |
speak_button = gr.Button("Speak!", elem_id="comfirm_button", elem_classes="button wonder-card")
|
|
|
117 |
)
|
118 |
input_text.submit(
|
119 |
speak_fn,
|
120 |
+
inputs=[input_text, exceed_flag],
|
121 |
+
outputs=[audio_output, exceed_flag],
|
122 |
)
|
123 |
speak_button.click(
|
124 |
speak_fn,
|
125 |
+
inputs=[input_text, exceed_flag],
|
126 |
+
outputs=[audio_output, exceed_flag],
|
127 |
)
|
128 |
|
129 |
|
|
|
137 |
show_api=False,
|
138 |
# server_name=server_name,
|
139 |
# server_port=server_port,
|
140 |
+
inbrowser=True,
|
|
|
141 |
)
|
assets/audios/nomorethan100.wav
ADDED
Binary file (166 kB). View file
|
|
assets/audios/overlength.wav
ADDED
Binary file (235 kB). View file
|
|
tools/__pycache__/webui.cpython-39.pyc
ADDED
Binary file (5.06 kB). View file
|
|
tools/webui.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
|
4 |
+
GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse
|
5 |
+
root_path = os.path.dirname(os.path.realpath(__file__))
|
6 |
+
|
7 |
+
def webpath(fn):
|
8 |
+
if fn.startswith(root_path):
|
9 |
+
web_path = os.path.relpath(fn, root_path).replace('\\', '/')
|
10 |
+
else:
|
11 |
+
web_path = os.path.abspath(fn)
|
12 |
+
return f'file={web_path}?{os.path.getmtime(fn)}'
|
13 |
+
|
14 |
+
def list_scripts(scriptdirname, extension):
|
15 |
+
scripts_list = []
|
16 |
+
scripts_dir = os.path.join(root_path, scriptdirname)
|
17 |
+
if os.path.exists(scripts_dir):
|
18 |
+
for filename in sorted(os.listdir(scripts_dir)):
|
19 |
+
scripts_list.append(ScriptFile(shared.assets_path, filename, os.path.join(scripts_dir, filename)))
|
20 |
+
scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
|
21 |
+
return scripts_list
|
22 |
+
|
23 |
+
def javascript_html():
|
24 |
+
head = ""
|
25 |
+
for script in list_scripts("javascript", ".js"):
|
26 |
+
head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
|
27 |
+
for script in list_scripts("javascript", ".mjs"):
|
28 |
+
head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
|
29 |
+
return head
|
30 |
+
|
31 |
+
def reload_javascript():
|
32 |
+
js = javascript_html()
|
33 |
+
js += """
|
34 |
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
35 |
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
36 |
+
<link href="https://fonts.googleapis.com/css2?family=Tilt+Warp&display=swap" rel="stylesheet">
|
37 |
+
<script type="importmap">
|
38 |
+
{
|
39 |
+
"imports": {
|
40 |
+
"three": "https://unpkg.com/[email protected]/build/three.module.js",
|
41 |
+
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
|
42 |
+
}
|
43 |
+
}
|
44 |
+
</script>
|
45 |
+
"""
|
46 |
+
|
47 |
+
corner = """
|
48 |
+
<a href="https://www.bilibili.com/video/BV14M411Z7FR" class="bilibili-corner" aria-label="View author on Bilibili">
|
49 |
+
<svg aria-hidden="true" style="fill:#F87399;/* color:#fff; */position: absolute;top: 0;border: 0;right: 0;" viewBox="0 0 80 80" height="80" width="80">
|
50 |
+
<path d="M0 0L80 80V0H0Z" fill="#F87399"></path>
|
51 |
+
<path d="M63.7507 10.5251H64.8893C66.9027 10.5985 68.5813 11.3099 69.92 12.6621C71.2587 14.013 71.952 15.7155 72 17.767V27.7595C71.952 29.8096 71.2587 31.5189 69.92 32.882C68.5813 34.2451 66.904 34.9511 64.8893 35H47.1107C45.0973 34.9511 43.4187 34.2451 42.08 32.882C40.7413 31.5189 40.048 29.811 40 27.7595V17.767C40.048 15.7155 40.7413 14.013 42.08 12.6621C43.4187 11.3099 45.096 10.5985 47.1107 10.5251H48.1427L46.5773 9.00454C46.4164 8.84495 46.2892 8.65348 46.2036 8.44193C46.1181 8.23038 46.076 8.00323 46.08 7.77448C46.08 7.29115 46.2453 6.88113 46.5773 6.54307L46.6133 6.50641C46.9693 6.16835 47.3773 6 47.84 6C48.3027 6 48.7107 6.16835 49.0667 6.50641L52.8707 10.236C52.9653 10.3323 53.0493 10.4287 53.12 10.5251H58.8093C58.8648 10.4178 58.9368 10.3201 59.0227 10.236L62.8267 6.50641C63.1827 6.16835 63.5907 6 64.0533 6C64.516 6 64.936 6.20501 65.292 6.54307C65.648 6.88113 65.8133 7.29115 65.8133 7.77448C65.8133 8.25646 65.648 8.66648 65.316 9.00454L63.7507 10.5251ZM47.1107 14.0375C46.116 14.0619 45.28 14.4122 44.604 15.0869C43.9293 15.7631 43.5787 16.6211 43.556 17.6584V27.8681C43.5787 28.9054 43.9293 29.7621 44.604 30.4382C45.28 31.1143 46.116 31.4646 47.1107 31.4877H64.8893C65.884 31.4646 66.72 31.1143 67.396 30.4382C68.0707 29.7621 68.4213 28.9054 68.444 27.8681V17.6584C68.4213 16.6198 68.0707 15.7631 67.396 15.0869C66.72 14.4122 65.884 14.0619 64.8893 14.0375H47.1107ZM50.6667 19.2876C51.164 19.2876 51.5787 19.4559 51.9107 19.794C52.244 20.1321 52.4213 20.5665 52.444 21.0974V22.6899C52.4213 23.2208 52.244 23.6552 51.9107 23.9933C51.5787 24.3327 51.164 24.5011 50.6667 24.5011C50.1693 24.5011 49.7547 24.3314 49.4227 23.9933C49.0893 23.6552 48.912 23.2208 48.8893 22.6899V21.0974C48.8893 20.591 49.0613 20.1619 49.404 19.8117C49.748 19.4627 50.1693 19.2876 50.6667 19.2876ZM61.3333 19.2876C61.8307 19.2876 62.2453 19.4559 62.5773 19.794C62.9107 20.1321 63.088 20.5665 63.1107 21.0974V22.6899C63.088 23.2208 62.9107 23.6552 62.5773 23.9933C62.2453 24.3327 61.8307 24.5011 61.3333 24.5011C60.836 24.5011 60.4213 24.3314 60.0893 23.9933C59.756 23.6552 59.5787 23.2208 59.556 22.6899V21.0974C59.5787 20.5665 59.756 20.1321 60.0893 19.794C60.4213 19.4559 60.836 19.2876 61.3333 19.2876Z" fill="white"></path>
|
52 |
+
</svg>
|
53 |
+
</a>
|
54 |
+
"""
|
55 |
+
|
56 |
+
def template_response(*args, **kwargs):
|
57 |
+
res = GradioTemplateResponseOriginal(*args, **kwargs)
|
58 |
+
# res.body = res.body.replace(b'</head>', f'{meta}{js}</head>'.encode("utf8"))
|
59 |
+
res.body = res.body.replace(b'</head>', f'{js}</head>'.encode("utf8"))
|
60 |
+
res.body = res.body.replace(b'</body>', f'{corner}</body>'.encode("utf8"))
|
61 |
+
res.init_headers()
|
62 |
+
return res
|
63 |
+
|
64 |
+
gr.routes.templates.TemplateResponse = template_response
|