-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathChatWaifu.py
280 lines (237 loc) · 7.88 KB
/
ChatWaifu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from scipy.io.wavfile import write
from mel_processing import spectrogram_torch
from text import text_to_sequence, _clean_text
from models import SynthesizerTrn
import utils
import commons
import sys
import re
from torch import no_grad, LongTensor
import logging
from winsound import PlaySound
from openai import OpenAI
chinese_model_path = ".\model\CN\model.pth"
chinese_config_path = ".\model\CN\config.json"
japanese_model_path = ".\model\H_excluded.pth"
japanese_config_path = ".\model\config.json"
####################################
#CHATGPT INITIALIZE
from pyChatGPT import ChatGPT
import json
modelmessage = """ID Output Language
0 Chinese
1 Japanese
"""
idmessage_cn = """ID Speaker
0 綾地寧々
1 在原七海
2 小茸
3 唐乐吟
"""
idmessage_jp = """ID Speaker
0 綾地寧々
1 因幡めぐる
2 朝武芳乃
3 常陸茉子
4 ムラサメ
5 鞍馬小春
6 在原七海
"""
def get_input():
# prompt for input
print("You:")
user_input = input()
return user_input
def get_input_jp():
# prompt for input
print("You:")
usr_in = input()
if usr_in == 'quit()':
return usr_in
else:
user_input = usr_in +" 使用日本语"
return user_input
def get_token():
token = input("Your API Key: \n")
return token
################################################
logging.getLogger('numba').setLevel(logging.WARNING)
def ex_print(text, escape=False):
if escape:
print(text.encode('unicode_escape').decode())
else:
print(text)
def get_text(text, hps, cleaned=False):
if cleaned:
text_norm = text_to_sequence(text, hps.symbols, [])
else:
text_norm = text_to_sequence(text, hps.symbols, hps.data.text_cleaners)
if hps.data.add_blank:
text_norm = commons.intersperse(text_norm, 0)
text_norm = LongTensor(text_norm)
return text_norm
def ask_if_continue():
while True:
answer = input('Continue? (y/n): ')
if answer == 'y':
break
elif answer == 'n':
sys.exit(0)
def print_speakers(speakers, escape=False):
if len(speakers) > 100:
return
print('ID\tSpeaker')
for id, name in enumerate(speakers):
ex_print(str(id) + '\t' + name, escape)
def get_speaker_id(message):
speaker_id = input(message)
if speaker_id == '':
print(str(speaker_id) + ' is not a valid ID!')
sys.exit(1)
else:
try:
speaker_id = int(speaker_id)
except:
print(str(speaker_id) + ' is not a valid ID!')
sys.exit(1)
return speaker_id
def get_model_id(message):
model_id = input(message)
if model_id == '':
print(str(model_id) + ' is not a valid ID!')
sys.exit(1)
else:
try:
model_id = int(model_id)
except:
print(str(model_id) + ' is not a valid ID!')
sys.exit(1)
return model_id
def get_label_value(text, label, default, warning_name='value'):
value = re.search(rf'\[{label}=(.+?)\]', text)
if value:
try:
text = re.sub(rf'\[{label}=(.+?)\]', '', text, 1)
value = float(value.group(1))
except:
print(f'Invalid {warning_name}!')
sys.exit(1)
else:
value = default
return value, text
def get_label(text, label):
if f'[{label}]' in text:
return True, text.replace(f'[{label}]', '')
else:
return False, text
def get_reponse(input):
msg = [
{"role": "user", "content": input}
]
# Call the OpenAI API with the prompt
response = client.chat.completions.create(
model="gpt-3.5-turbo", # Adjust based on available engine versions
messages=msg,
temperature=0
)
# Extract and return the text from the API response
return response.choices[0].message.content
def generateSound(inputString, id, model_id):
if '--escape' in sys.argv:
escape = True
else:
escape = False
#model = input('0: Chinese')
#config = input('Path of a config file: ')
if model_id == 0:
model = chinese_model_path
config = chinese_config_path
elif model_id == 1:
model = japanese_model_path
config = japanese_config_path
hps_ms = utils.get_hparams_from_file(config)
n_speakers = hps_ms.data.n_speakers if 'n_speakers' in hps_ms.data.keys() else 0
n_symbols = len(hps_ms.symbols) if 'symbols' in hps_ms.keys() else 0
emotion_embedding = hps_ms.data.emotion_embedding if 'emotion_embedding' in hps_ms.data.keys() else False
net_g_ms = SynthesizerTrn(
n_symbols,
hps_ms.data.filter_length // 2 + 1,
hps_ms.train.segment_size // hps_ms.data.hop_length,
n_speakers=n_speakers,
emotion_embedding=emotion_embedding,
**hps_ms.model)
_ = net_g_ms.eval()
utils.load_checkpoint(model, net_g_ms)
if n_symbols != 0:
if not emotion_embedding:
#while True:
if(1 == 1):
choice = 't'
if choice == 't':
text = inputString
if text == '[ADVANCED]':
text = "我不会说"
length_scale, text = get_label_value(
text, 'LENGTH', 1, 'length scale')
noise_scale, text = get_label_value(
text, 'NOISE', 0.667, 'noise scale')
noise_scale_w, text = get_label_value(
text, 'NOISEW', 0.8, 'deviation of noise')
cleaned, text = get_label(text, 'CLEANED')
stn_tst = get_text(text, hps_ms, cleaned=cleaned)
speaker_id = id
out_path = "output.wav"
with no_grad():
x_tst = stn_tst.unsqueeze(0)
x_tst_lengths = LongTensor([stn_tst.size(0)])
sid = LongTensor([speaker_id])
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=noise_scale,
noise_scale_w=noise_scale_w, length_scale=length_scale)[0][0, 0].data.cpu().float().numpy()
write(out_path, hps_ms.data.sampling_rate, audio)
if __name__ == "__main__":
# Set OpenAI API key
api_key = get_token()
print()
client = OpenAI(api_key=api_key, timeout=600)
model_id = -1
while True:
print(modelmessage)
model_id = int(get_model_id('选择回复语言: '))
if model_id == 0 or model_id == 1:
break
else:
print(str(model_id) + ' is not a valid ID!\n')
print()
speaker_id = -1
while True:
if model_id == 0:
print("\n" + idmessage_cn)
elif model_id == 1:
print("\n" + idmessage_jp)
speaker_id = get_speaker_id('选择角色: ')
if (model_id == 0 and speaker_id in list(range(4))) or (model_id == 1 and speaker_id in list(range(7))):
break
else:
print(str(speaker_id) + ' is not a valid ID!\n')
print()
while True:
if model_id == 0:
usr_in = get_input()
if(usr_in == "quit()"):
break
resp = get_reponse(usr_in)
print("ChatGPT:")
answer = resp.replace('\n','')
generateSound("[ZH]"+answer+"[ZH]", speaker_id, model_id)
print(answer)
PlaySound(r'./output.wav', flags=1)
elif model_id == 1:
usr_in = get_input_jp()
if(usr_in == "quit()"):
break
resp = get_reponse(usr_in)
print("ChatGPT:")
answer = resp.replace('\n','')
generateSound(answer, speaker_id, model_id)
print(answer)
PlaySound(r'./output.wav', flags=1)