MZhaovo commited on
Commit
425b9a7
1 Parent(s): ac25389

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. __pycache__/presets.cpython-39.pyc +0 -0
  2. app.py +1 -132
  3. javascript/main.js +15 -0
  4. presets.py +135 -0
__pycache__/presets.cpython-39.pyc ADDED
Binary file (3.85 kB). View file
 
app.py CHANGED
@@ -1,132 +1,4 @@
1
- import nltk
2
- import ssl
3
-
4
- try:
5
- _create_unverified_https_context = ssl._create_unverified_context
6
- except AttributeError:
7
- pass
8
- else:
9
- ssl._create_default_https_context = _create_unverified_https_context
10
- nltk.download("cmudict")
11
-
12
- import os
13
- import json
14
- import random
15
- import gradio as gr
16
- import numpy as np
17
- import torch
18
- import re_matching
19
- import utils
20
- from infer import infer, latest_version, get_net_g
21
- import gradio as gr
22
- from config import config
23
- from tools.webui import reload_javascript, get_character_html
24
-
25
- device = config.webui_config.device
26
- if device == "mps":
27
- os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
28
-
29
- def speak_fn(
30
- text: str,
31
- exceed_flag,
32
- speaker="TalkFlower_CNzh",
33
- sdp_ratio=0.2, # SDP/DP混合比
34
- noise_scale=0.6, # 感情
35
- noise_scale_w=0.6, # 音素长度
36
- length_scale=0.9, # 语速
37
- language="ZH",
38
- interval_between_para=0.2, # 段间间隔
39
- interval_between_sent=1, # 句间间隔
40
- ):
41
- while text.find("\n\n") != -1:
42
- text = text.replace("\n\n", "\n")
43
- if len(text) > 100:
44
- print(f"Too Long Text: {text}")
45
- if exceed_flag:
46
- text = "不要超过100字!"
47
- audio_value = "./assets/audios/nomorethan100.wav"
48
- else:
49
- text = "这句太长了,憋坏我啦!"
50
- audio_value = "./assets/audios/overlength.wav"
51
- exceed_flag = not exceed_flag
52
- else:
53
- audio_list = []
54
- if len(text) > 42:
55
- print(f"Long Text: {text}")
56
- para_list = re_matching.cut_para(text)
57
- for p in para_list:
58
- audio_list_sent = []
59
- sent_list = re_matching.cut_sent(p)
60
- for s in sent_list:
61
- audio = infer(
62
- s,
63
- sdp_ratio=sdp_ratio,
64
- noise_scale=noise_scale,
65
- noise_scale_w=noise_scale_w,
66
- length_scale=length_scale,
67
- sid=speaker,
68
- language=language,
69
- hps=hps,
70
- net_g=net_g,
71
- device=device,
72
- )
73
- audio_list_sent.append(audio)
74
- silence = np.zeros((int)(44100 * interval_between_sent))
75
- audio_list_sent.append(silence)
76
- if (interval_between_para - interval_between_sent) > 0:
77
- silence = np.zeros((int)(44100 * (interval_between_para - interval_between_sent)))
78
- audio_list_sent.append(silence)
79
- audio16bit = gr.processing_utils.convert_to_16_bit_wav(np.concatenate(audio_list_sent)) # 对完整句子做音量归一
80
- audio_list.append(audio16bit)
81
- else:
82
- print(f"Short Text: {text}")
83
- silence = np.zeros(hps.data.sampling_rate // 2, dtype=np.int16)
84
- with torch.no_grad():
85
- for piece in text.split("|"):
86
- audio = infer(
87
- piece,
88
- sdp_ratio=sdp_ratio,
89
- noise_scale=noise_scale,
90
- noise_scale_w=noise_scale_w,
91
- length_scale=length_scale,
92
- sid=speaker,
93
- language=language,
94
- hps=hps,
95
- net_g=net_g,
96
- device=device,
97
- )
98
- audio16bit = gr.processing_utils.convert_to_16_bit_wav(audio)
99
- audio_list.append(audio16bit)
100
- audio_list.append(silence) # 将静音添加到列表中
101
-
102
- audio_concat = np.concatenate(audio_list)
103
- audio_value = (hps.data.sampling_rate, audio_concat)
104
-
105
- return gr.update(value=audio_value, autoplay=True), get_character_html(text), exceed_flag, gr.update(interactive=True)
106
-
107
-
108
- def submit_lock_fn():
109
- return gr.update(interactive=False)
110
-
111
-
112
- def init_fn():
113
- gr.Info("2023-11-24: 优化长句生成效果;增加示例;更新了一些小彩蛋;画了一些大饼)")
114
- gr.Info("Only support Chinese now. Trying to train a mutilingual model. 欢迎在 Community 中提建议~")
115
-
116
- index = random.randint(1,7)
117
- welcome_text = get_sentence("Welcome", index)
118
-
119
- return gr.update(value=f"./assets/audios/Welcome{index}.wav", autoplay=False), get_character_html(welcome_text)
120
-
121
- def get_sentence(category, index=-1):
122
- if index == -1:
123
- index = random.randint(1, len(full_lines[category]))
124
- return full_lines[category][f"{index}"]
125
-
126
- with open("./css/style.css", "r", encoding="utf-8") as f:
127
- customCSS = f.read()
128
- with open("./assets/lines.json", "r", encoding="utf-8") as f:
129
- full_lines = json.load(f)
130
 
131
  with gr.Blocks(css=customCSS) as demo:
132
  exceed_flag = gr.State(value=False)
@@ -162,9 +34,6 @@ with gr.Blocks(css=customCSS) as demo:
162
 
163
 
164
  if __name__ == "__main__":
165
- hps = utils.get_hparams_from_file(config.webui_config.config_path)
166
- version = hps.version if hasattr(hps, "version") else latest_version
167
- net_g = get_net_g(model_path=config.webui_config.model, version=version, device=device, hps=hps)
168
  reload_javascript()
169
  demo.launch(
170
  allowed_paths=["./assets", "./javascript", "./css"],
 
1
+ from presets import *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  with gr.Blocks(css=customCSS) as demo:
4
  exceed_flag = gr.State(value=False)
 
34
 
35
 
36
  if __name__ == "__main__":
 
 
 
37
  reload_javascript()
38
  demo.launch(
39
  allowed_paths=["./assets", "./javascript", "./css"],
javascript/main.js CHANGED
@@ -65,14 +65,29 @@ function set_speak_examples() {
65
  buttons[0].addEventListener("click", function() {
66
  const randomString = praiseArray[Math.floor(Math.random() * praiseArray.length)];
67
  speak_input.value = randomString;
 
 
 
 
 
68
  });
69
  buttons[1].addEventListener("click", function() {
70
  const randomString = scriptsArray[Math.floor(Math.random() * scriptsArray.length)];
71
  speak_input.value = randomString;
 
 
 
 
 
72
  });
73
  buttons[2].addEventListener("click", function() {
74
  const randomString = memeArray[Math.floor(Math.random() * memeArray.length)];
75
  speak_input.value = randomString;
 
 
 
 
 
76
  });
77
 
78
  }
 
65
  buttons[0].addEventListener("click", function() {
66
  const randomString = praiseArray[Math.floor(Math.random() * praiseArray.length)];
67
  speak_input.value = randomString;
68
+ var event = new Event('input', {
69
+ bubbles: true,
70
+ cancelable: true,
71
+ });
72
+ speak_input.dispatchEvent(event);
73
  });
74
  buttons[1].addEventListener("click", function() {
75
  const randomString = scriptsArray[Math.floor(Math.random() * scriptsArray.length)];
76
  speak_input.value = randomString;
77
+ var event = new Event('input', {
78
+ bubbles: true,
79
+ cancelable: true,
80
+ });
81
+ speak_input.dispatchEvent(event);
82
  });
83
  buttons[2].addEventListener("click", function() {
84
  const randomString = memeArray[Math.floor(Math.random() * memeArray.length)];
85
  speak_input.value = randomString;
86
+ var event = new Event('input', {
87
+ bubbles: true,
88
+ cancelable: true,
89
+ });
90
+ speak_input.dispatchEvent(event);
91
  });
92
 
93
  }
presets.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nltk, ssl
2
+ try:
3
+ _create_unverified_https_context = ssl._create_unverified_context
4
+ except AttributeError:
5
+ pass
6
+ else:
7
+ ssl._create_default_https_context = _create_unverified_https_context
8
+ nltk.download("cmudict")
9
+
10
+ import os, logging, datetime, json, random
11
+ import gradio as gr
12
+ import numpy as np
13
+ import torch
14
+ import re_matching
15
+ import utils
16
+ from infer import infer, latest_version, get_net_g
17
+ import gradio as gr
18
+ from config import config
19
+ from tools.webui import reload_javascript, get_character_html
20
+
21
+ logging.basicConfig(
22
+ level=logging.INFO,
23
+ format='[%(levelname)s|%(asctime)s]%(message)s',
24
+ datefmt='%Y-%m-%d %H:%M:%S'
25
+ )
26
+
27
+ device = config.webui_config.device
28
+ if device == "mps":
29
+ os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
30
+
31
+ hps = utils.get_hparams_from_file(config.webui_config.config_path)
32
+ version = hps.version if hasattr(hps, "version") else latest_version
33
+ net_g = get_net_g(model_path=config.webui_config.model, version=version, device=device, hps=hps)
34
+
35
+ with open("./css/style.css", "r", encoding="utf-8") as f:
36
+ customCSS = f.read()
37
+ with open("./assets/lines.json", "r", encoding="utf-8") as f:
38
+ full_lines = json.load(f)
39
+
40
+ def speak_fn(
41
+ text: str,
42
+ exceed_flag,
43
+ speaker="TalkFlower_CNzh",
44
+ sdp_ratio=0.2, # SDP/DP混合比
45
+ noise_scale=0.6, # 感情
46
+ noise_scale_w=0.6, # 音素长度
47
+ length_scale=0.9, # 语速
48
+ language="ZH",
49
+ interval_between_para=0.2, # 段间间隔
50
+ interval_between_sent=1, # 句间间隔
51
+ ):
52
+ while text.find("\n\n") != -1:
53
+ text = text.replace("\n\n", "\n")
54
+ if len(text) > 100:
55
+ logging.info(f"Too Long Text: {text}")
56
+ if exceed_flag:
57
+ text = "不要超过100字!"
58
+ audio_value = "./assets/audios/nomorethan100.wav"
59
+ else:
60
+ text = "这句太长了,憋坏我啦!"
61
+ audio_value = "./assets/audios/overlength.wav"
62
+ exceed_flag = not exceed_flag
63
+ else:
64
+ audio_list = []
65
+ if len(text) > 42:
66
+ logging.info(f"Long Text: {text}")
67
+ para_list = re_matching.cut_para(text)
68
+ for p in para_list:
69
+ audio_list_sent = []
70
+ sent_list = re_matching.cut_sent(p)
71
+ for s in sent_list:
72
+ audio = infer(
73
+ s,
74
+ sdp_ratio=sdp_ratio,
75
+ noise_scale=noise_scale,
76
+ noise_scale_w=noise_scale_w,
77
+ length_scale=length_scale,
78
+ sid=speaker,
79
+ language=language,
80
+ hps=hps,
81
+ net_g=net_g,
82
+ device=device,
83
+ )
84
+ audio_list_sent.append(audio)
85
+ silence = np.zeros((int)(44100 * interval_between_sent))
86
+ audio_list_sent.append(silence)
87
+ if (interval_between_para - interval_between_sent) > 0:
88
+ silence = np.zeros((int)(44100 * (interval_between_para - interval_between_sent)))
89
+ audio_list_sent.append(silence)
90
+ audio16bit = gr.processing_utils.convert_to_16_bit_wav(np.concatenate(audio_list_sent)) # 对完整句子做音量归一
91
+ audio_list.append(audio16bit)
92
+ else:
93
+ logging.info(f"Short Text: {text}")
94
+ silence = np.zeros(hps.data.sampling_rate // 2, dtype=np.int16)
95
+ with torch.no_grad():
96
+ for piece in text.split("|"):
97
+ audio = infer(
98
+ piece,
99
+ sdp_ratio=sdp_ratio,
100
+ noise_scale=noise_scale,
101
+ noise_scale_w=noise_scale_w,
102
+ length_scale=length_scale,
103
+ sid=speaker,
104
+ language=language,
105
+ hps=hps,
106
+ net_g=net_g,
107
+ device=device,
108
+ )
109
+ audio16bit = gr.processing_utils.convert_to_16_bit_wav(audio)
110
+ audio_list.append(audio16bit)
111
+ audio_list.append(silence) # 将静音添加到列表中
112
+
113
+ audio_concat = np.concatenate(audio_list)
114
+ audio_value = (hps.data.sampling_rate, audio_concat)
115
+
116
+ return gr.update(value=audio_value, autoplay=True), get_character_html(text), exceed_flag, gr.update(interactive=True)
117
+
118
+
119
+ def submit_lock_fn():
120
+ return gr.update(interactive=False)
121
+
122
+
123
+ def init_fn():
124
+ gr.Info("2023-11-24: 优化长句生成效果;增加示例;更新了一些小彩蛋;画了一些大饼)")
125
+ gr.Info("Only support Chinese now. Trying to train a mutilingual model. 欢迎在 Community 中提建议~")
126
+
127
+ index = random.randint(1,7)
128
+ welcome_text = get_sentence("Welcome", index)
129
+
130
+ return gr.update(value=f"./assets/audios/Welcome{index}.wav", autoplay=False), get_character_html(welcome_text)
131
+
132
+ def get_sentence(category, index=-1):
133
+ if index == -1:
134
+ index = random.randint(1, len(full_lines[category]))
135
+ return full_lines[category][f"{index}"]