Mahiruoshi
commited on
Commit
•
ba568fb
1
Parent(s):
ba2bf37
Rename colab_inference.py to inference_ork.py .py
Browse files- colab_inference.py +0 -178
- inference_ork.py .py +121 -0
colab_inference.py
DELETED
@@ -1,178 +0,0 @@
|
|
1 |
-
#colab克隆并且安装完环境后启动该文件
|
2 |
-
import romajitable
|
3 |
-
import re
|
4 |
-
import numpy as np
|
5 |
-
import logging
|
6 |
-
logging.getLogger('numba').setLevel(logging.WARNING)
|
7 |
-
import IPython.display as ipd
|
8 |
-
import torch
|
9 |
-
import commons
|
10 |
-
import utils
|
11 |
-
from models import SynthesizerTrn
|
12 |
-
from text.symbols import symbols
|
13 |
-
from text import text_to_sequence
|
14 |
-
import gradio as gr
|
15 |
-
import time
|
16 |
-
def get_text(text, hps):
|
17 |
-
text_norm = text_to_sequence(text, symbols, hps.data.text_cleaners)
|
18 |
-
if hps.data.add_blank:
|
19 |
-
text_norm = commons.intersperse(text_norm, 0)
|
20 |
-
text_norm = torch.LongTensor(text_norm)
|
21 |
-
return text_norm
|
22 |
-
dev = torch.device("cuda:0")
|
23 |
-
def selection(speaker):
|
24 |
-
if speaker == "高咲侑":
|
25 |
-
spk = 0
|
26 |
-
return spk
|
27 |
-
|
28 |
-
elif speaker == "歩夢":
|
29 |
-
spk = 1
|
30 |
-
return spk
|
31 |
-
|
32 |
-
elif speaker == "かすみ":
|
33 |
-
spk = 2
|
34 |
-
return spk
|
35 |
-
|
36 |
-
elif speaker == "しずく":
|
37 |
-
spk = 3
|
38 |
-
return spk
|
39 |
-
|
40 |
-
elif speaker == "果林":
|
41 |
-
spk = 4
|
42 |
-
return spk
|
43 |
-
|
44 |
-
elif speaker == "愛":
|
45 |
-
spk = 5
|
46 |
-
return spk
|
47 |
-
|
48 |
-
elif speaker == "彼方":
|
49 |
-
spk = 6
|
50 |
-
return spk
|
51 |
-
|
52 |
-
elif speaker == "せつ菜":
|
53 |
-
spk = 7
|
54 |
-
return spk
|
55 |
-
elif speaker == "エマ":
|
56 |
-
spk = 8
|
57 |
-
return spk
|
58 |
-
elif speaker == "璃奈":
|
59 |
-
spk = 9
|
60 |
-
return spk
|
61 |
-
elif speaker == "栞子":
|
62 |
-
spk = 10
|
63 |
-
return spk
|
64 |
-
elif speaker == "ランジュ":
|
65 |
-
spk = 11
|
66 |
-
return spk
|
67 |
-
elif speaker == "ミア":
|
68 |
-
spk = 12
|
69 |
-
return spk
|
70 |
-
elif speaker == "三色绘恋1":
|
71 |
-
spk = 13
|
72 |
-
return spk
|
73 |
-
elif speaker == "三色绘恋2":
|
74 |
-
spk = 15
|
75 |
-
elif speaker == "派蒙":
|
76 |
-
spk = 16
|
77 |
-
return spk
|
78 |
-
def is_japanese(string):
|
79 |
-
for ch in string:
|
80 |
-
if ord(ch) > 0x3040 and ord(ch) < 0x30FF:
|
81 |
-
return True
|
82 |
-
return False
|
83 |
-
def is_english(string):
|
84 |
-
import re
|
85 |
-
pattern = re.compile('^[A-Za-z0-9.,:;!?()_*"\' ]+$')
|
86 |
-
if pattern.fullmatch(string):
|
87 |
-
return True
|
88 |
-
else:
|
89 |
-
return False
|
90 |
-
def sle(language,tts_input0):
|
91 |
-
if language == "中文":
|
92 |
-
tts_input1 = "[ZH]" + tts_input0.replace('\n','。').replace(' ',',') + "[ZH]"
|
93 |
-
return tts_input1
|
94 |
-
if language == "自动":
|
95 |
-
tts_input1 = f"[JA]{tts_input0}[JA]" if is_japanese(tts_input0) else f"[ZH]{tts_input0}[ZH]"
|
96 |
-
return tts_input1
|
97 |
-
elif language == "日文":
|
98 |
-
tts_input1 = "[JA]" + tts_input0.replace('\n','。').replace(' ',',') + "[JA]"
|
99 |
-
return tts_input1
|
100 |
-
def extrac(text):
|
101 |
-
text = re.sub("<[^>]*>","",text)
|
102 |
-
result_list = re.split(r'\n', text)
|
103 |
-
final_list = []
|
104 |
-
for i in result_list:
|
105 |
-
if is_english(i):
|
106 |
-
i = romajitable.to_kana(i).katakana
|
107 |
-
i = i.replace('\n','').replace(' ','')
|
108 |
-
#Current length of single sentence: 20
|
109 |
-
if len(i)>1:
|
110 |
-
if len(i) > 20:
|
111 |
-
try:
|
112 |
-
cur_list = re.split(r'。|!', i)
|
113 |
-
for i in cur_list:
|
114 |
-
if len(i)>1:
|
115 |
-
final_list.append(i+'。')
|
116 |
-
except:
|
117 |
-
pass
|
118 |
-
else:
|
119 |
-
final_list.append(i)
|
120 |
-
final_list = [x for x in final_list if x != '']
|
121 |
-
print(final_list)
|
122 |
-
return final_list
|
123 |
-
def infer(language,text,speaker_id, n_scale= 0.667,n_scale_w = 0.8, l_scale = 1 ):
|
124 |
-
speaker_id = int(selection(speaker_id))
|
125 |
-
a = ['【','[','(','(']
|
126 |
-
b = ['】',']',')',')']
|
127 |
-
for i in a:
|
128 |
-
text = text.replace(i,'<')
|
129 |
-
for i in b:
|
130 |
-
text = text.replace(i,'>')
|
131 |
-
final_list = extrac(text.replace('“','').replace('”',''))
|
132 |
-
audio_fin = []
|
133 |
-
c = 0
|
134 |
-
for sentence in final_list:
|
135 |
-
c +=1
|
136 |
-
try:
|
137 |
-
stn_tst = get_text(sle(language,sentence), hps_ms)
|
138 |
-
with torch.no_grad():
|
139 |
-
x_tst = stn_tst.unsqueeze(0).to(dev)
|
140 |
-
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(dev)
|
141 |
-
sid = torch.LongTensor([speaker_id]).to(dev)
|
142 |
-
t1 = time.time()
|
143 |
-
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=n_scale, noise_scale_w=n_scale_w, length_scale=l_scale)[0][0,0].data.cpu().float().numpy()
|
144 |
-
t2 = time.time()
|
145 |
-
spending_time = "第"+str(c)+"句的推理时间为:"+str(t2-t1)+"s"
|
146 |
-
print(spending_time)
|
147 |
-
audio_fin.append(audio)
|
148 |
-
except:
|
149 |
-
print('存在非法字符')
|
150 |
-
return (hps_ms.data.sampling_rate, np.concatenate(audio_fin))
|
151 |
-
lan = ["中文","日文","自动"]
|
152 |
-
idols = ["高咲侑","歩夢","かすみ","しずく","果林","愛","せつ菜","璃奈","栞子","エマ","ランジュ","ミア","派蒙"]
|
153 |
-
hps_ms = utils.get_hparams_from_file("lovelive/config.json")
|
154 |
-
net_g_ms = SynthesizerTrn(
|
155 |
-
len(symbols),
|
156 |
-
hps_ms.data.filter_length // 2 + 1,
|
157 |
-
hps_ms.train.segment_size // hps_ms.data.hop_length,
|
158 |
-
n_speakers=hps_ms.data.n_speakers,
|
159 |
-
**hps_ms.model).to(dev)
|
160 |
-
_ = net_g_ms.eval()
|
161 |
-
_ = utils.load_checkpoint("lovelive/G_525000.pth", net_g_ms)
|
162 |
-
app = gr.Blocks()
|
163 |
-
with app:
|
164 |
-
with gr.Tabs():
|
165 |
-
|
166 |
-
with gr.TabItem("虹团vits模型,现可按句分割实现长文本合成,可自行用export_to_onnx.py导出"):
|
167 |
-
|
168 |
-
tts_input1 = gr.TextArea(label="如需实现快速合成,建议在colab上克隆后运行本仓库", value="为什么你会那么熟练啊?你和雪菜亲过多少次了?我想做只属于你一个人的学院偶像,所以,请只注视我一个人,好吗?【中文】\nなんでそんなに慣れてんだよっ?せつ菜と…何回キスしたんだよ?どこまであたしを置いてきぼりにすれば気が済むんだよ?[日文]\nI can't choose just one(English)")
|
169 |
-
language = gr.Dropdown(label="选择语言,目前勉强可以做到自动识别",choices=lan, value="自动", interactive=True)
|
170 |
-
para_input1 = gr.Slider(minimum= 0,maximum=1.0,label="更改噪声比例,以控制情感", value=0.667)
|
171 |
-
para_input2 = gr.Slider(minimum= 0,maximum=1.0,label="更改噪声偏差,以控制音素长短", value=0.7)
|
172 |
-
para_input3 = gr.Slider(minimum= 0.1,maximum=10,label="更改时间比例", value=1)
|
173 |
-
tts_submit = gr.Button("Generate", variant="primary")
|
174 |
-
speaker1 = gr.Dropdown(label="选择说话人",choices=idols, value="歩夢", interactive=True)
|
175 |
-
tts_output2 = gr.Audio(label="Output")
|
176 |
-
tts_submit.click(infer, [language,tts_input1,speaker1,para_input1,para_input2,para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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#basic enviornments & openai
|
2 |
+
import romajitable
|
3 |
+
import re
|
4 |
+
import os
|
5 |
+
import numpy as np
|
6 |
+
import logging
|
7 |
+
logging.getLogger('numba').setLevel(logging.WARNING)
|
8 |
+
import IPython.display as ipd
|
9 |
+
import torch
|
10 |
+
import commons
|
11 |
+
import utils
|
12 |
+
from models import SynthesizerTrn
|
13 |
+
from text.symbols import symbols
|
14 |
+
from text import text_to_sequence
|
15 |
+
import openai
|
16 |
+
import tkinter as tk
|
17 |
+
from tkinter import scrolledtext
|
18 |
+
import argparse
|
19 |
+
import time
|
20 |
+
from scipy.io.wavfile import write
|
21 |
+
def get_args():
|
22 |
+
parser = argparse.ArgumentParser(description='inference')
|
23 |
+
parser.add_argument('--model', default = 'lovelive/G_817000.pth')
|
24 |
+
parser.add_argument('--audio',
|
25 |
+
type=str,
|
26 |
+
help='the sound file of live2d to be replace,assuming they are temp1.wav,temp2.wav,temp3.wav......',
|
27 |
+
default = 'path/to/temp.wav')
|
28 |
+
parser.add_argument('--cfg', default="lovelive/config.json")
|
29 |
+
parser.add_argument('--key',default = "openai key",
|
30 |
+
help='platform.openai.com')
|
31 |
+
args = parser.parse_args()
|
32 |
+
return args
|
33 |
+
args = get_args()
|
34 |
+
dev = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
35 |
+
dev = torch.device("cuda:0")
|
36 |
+
hps_ms = utils.get_hparams_from_file(args.cfg)
|
37 |
+
#mult-speakers
|
38 |
+
net_g_ms = SynthesizerTrn(
|
39 |
+
len(symbols),
|
40 |
+
hps_ms.data.filter_length // 2 + 1,
|
41 |
+
hps_ms.train.segment_size // hps_ms.data.hop_length,
|
42 |
+
n_speakers=hps_ms.data.n_speakers,
|
43 |
+
**hps_ms.model).to(dev)
|
44 |
+
_ = net_g_ms.eval()
|
45 |
+
_ = utils.load_checkpoint(args.model, net_g_ms, None)
|
46 |
+
# detecting japanese
|
47 |
+
def is_japanese(string):
|
48 |
+
for ch in string:
|
49 |
+
if ord(ch) > 0x3040 and ord(ch) < 0x30FF:
|
50 |
+
return True
|
51 |
+
return False
|
52 |
+
def get_text(text, hps):
|
53 |
+
text_norm = text_to_sequence(text, hps.data.text_cleaners)
|
54 |
+
if hps.data.add_blank:
|
55 |
+
text_norm = commons.intersperse(text_norm, 0)
|
56 |
+
text_norm = torch.LongTensor(text_norm)
|
57 |
+
return text_norm
|
58 |
+
def ttv(text):
|
59 |
+
text = text.replace('\n','').replace(' ','')
|
60 |
+
text = f"[JA]{text}[JA]" if is_japanese(text) else f"[ZH]{text}[ZH]"
|
61 |
+
speaker_id = 7
|
62 |
+
stn_tst = get_text(text,hps_ms)
|
63 |
+
t1 = time.time()
|
64 |
+
with torch.no_grad():
|
65 |
+
x_tst = stn_tst.unsqueeze(0).to(dev)
|
66 |
+
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(dev)
|
67 |
+
sid = torch.LongTensor([speaker_id]).to(dev)
|
68 |
+
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=0.467, noise_scale_w=0.5, length_scale=1)[0][0,0].data.cpu().float().numpy()
|
69 |
+
write(args.audio + '.wav',22050,audio)
|
70 |
+
i = 0
|
71 |
+
while i < 19:
|
72 |
+
i +=1
|
73 |
+
cmd = 'ffmpeg -y -i ' + args.audio + '.wav' + ' -ar 44100 '+ args.audio.replace('temp','temp'+str(i))
|
74 |
+
os.system(cmd)
|
75 |
+
t2 = time.time()
|
76 |
+
print("推理耗时:",(t2 - t1),"s")
|
77 |
+
openai.api_key = args.key
|
78 |
+
result_list = []
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
messages = [
|
84 |
+
{"role": "system", "content": "你是超级ai,名字叫巴珠绪,是我的女朋友。接下来我们将进行一个克苏鲁跑团游戏,你负责扮演守密人,我负责扮演调查员。接下来你会加载一个名叫《幽暗之门》的模组,作为守密人,你需要基于“克苏鲁神话角色扮演游戏规则第七版(Call of Cthulhu 7th Edition)”,我会给你剧本的开头部分,然后基于你对它的理解自由发挥。投掷骰子的环节将由你来模拟,用两个1d10的骰子来生成0-100的随机数。比如说我的某一项属性点是80,当骰子的数目小于80时就判定为成功,0-5为大成功,95-100为大失败等等。整个游戏过程将类似于你来描述故事,我来投骰子并且做出决定来推动剧情的走向。"},
|
85 |
+
{'role': 'assistant', 'content': '我明白了,现在我将扮演守密人。'},
|
86 |
+
]
|
87 |
+
def send_message():
|
88 |
+
text = input_box.get("1.0", "end-1c")
|
89 |
+
messages.append({"role": "user", "content": text},)
|
90 |
+
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
|
91 |
+
reply = chat.choices[0].message.content
|
92 |
+
ttv(reply)
|
93 |
+
messages.append({"role": "assistant", "content": reply})
|
94 |
+
print(messages[-1])
|
95 |
+
if len(messages) == 12:
|
96 |
+
messages[6:10] = messages[8:]
|
97 |
+
del messages[-2:]
|
98 |
+
with open('log.txt', 'w', encoding='utf-8') as f:
|
99 |
+
for item in messages:
|
100 |
+
f.write(str(item) + "\n")
|
101 |
+
chat_box.configure(state='normal')
|
102 |
+
chat_box.insert(tk.END, "You: " + text + "\n")
|
103 |
+
chat_box.insert(tk.END, "Tamao: " + reply + "\n")
|
104 |
+
chat_box.configure(state='disabled')
|
105 |
+
input_box.delete("1.0", tk.END)
|
106 |
+
|
107 |
+
root = tk.Tk()
|
108 |
+
root.title("Tamao")
|
109 |
+
|
110 |
+
chat_box = scrolledtext.ScrolledText(root, width=50, height=10)
|
111 |
+
chat_box.configure(state='disabled')
|
112 |
+
chat_box.pack(side=tk.TOP, fill=tk.BOTH, padx=10, pady=10, expand=True)
|
113 |
+
|
114 |
+
input_frame = tk.Frame(root)
|
115 |
+
input_frame.pack(side=tk.BOTTOM, fill=tk.X, padx=10, pady=10)
|
116 |
+
input_box = tk.Text(input_frame, height=3, width=50)
|
117 |
+
input_box.pack(side=tk.LEFT, fill=tk.X, padx=10, expand=True)
|
118 |
+
send_button = tk.Button(input_frame, text="Send", command=send_message)
|
119 |
+
send_button.pack(side=tk.RIGHT, padx=10)
|
120 |
+
|
121 |
+
root.mainloop()
|