-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathgpt-sovits-v2.py
632 lines (553 loc) · 20.4 KB
/
gpt-sovits-v2.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import time
import sys
# logger
from logging import getLogger # noqa: E402
import numpy as np
import soundfile
import librosa
from tqdm import tqdm
# import original modules
sys.path.append("../../util")
from arg_utils import get_base_parser, update_parser # noqa: E402
from model_utils import check_and_download_models # noqa: E402
import ailia
from text import cleaned_text_to_sequence
from text.cleaner import clean_text
logger = getLogger(__name__)
# ======================
# PARAMETERS
# ======================
REF_WAV_PATH = "reference_audio_captured_by_ax.wav"
REF_TEXT = "水をマレーシアから買わなくてはならない。"
SAVE_WAV_PATH = "output.wav"
REMOTE_PATH = "https://storage.googleapis.com/ailia-models/gpt-sovits-v2/"
WEIGHT_PATH_SSL = "cnhubert.onnx"
WEIGHT_PATH_T2S_ENCODER = "t2s_encoder.onnx"
WEIGHT_PATH_T2S_FIRST_DECODER = "t2s_fsdec.onnx"
WEIGHT_PATH_T2S_STAGE_DECODER = "t2s_sdec.onnx"
WEIGHT_PATH_VITS = "vits.onnx"
MODEL_PATH_SSL = WEIGHT_PATH_SSL + ".prototxt"
MODEL_PATH_T2S_ENCODER = WEIGHT_PATH_T2S_ENCODER + ".prototxt"
MODEL_PATH_T2S_FIRST_DECODER = WEIGHT_PATH_T2S_FIRST_DECODER + ".prototxt"
MODEL_PATH_T2S_STAGE_DECODER = WEIGHT_PATH_T2S_STAGE_DECODER + ".prototxt"
MODEL_PATH_VITS = WEIGHT_PATH_VITS + ".prototxt"
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser("GPT-SoVits", None, SAVE_WAV_PATH)
# overwrite
parser.add_argument(
"--input",
"-i",
metavar="TEXT",
default="ax株式会社ではAIの実用化のための技術を開発しています。",
help="input text",
)
parser.add_argument(
"--text_language", "-tl", default="ja", choices=("ja", "en"), help="[ja, en]"
)
parser.add_argument(
"--ref_audio",
"-ra",
metavar="TEXT",
default=REF_WAV_PATH,
help="ref audio",
)
parser.add_argument(
"--ref_text",
"-rt",
metavar="TEXT",
default=REF_TEXT,
help="ref text",
)
parser.add_argument(
"--ref_language", "-rl", default="ja", choices=("ja", "en"), help="[ja, en]"
)
parser.add_argument("--top_k", type=int, default=15, help="top_k")
parser.add_argument("--top_p", type=float, default=1.0, help="top_p")
parser.add_argument("--temperature", type=float, default=1.0, help="temperature")
parser.add_argument("--speed", type=float, default=1.0, help="Speech rate")
parser.add_argument("--onnx", action="store_true", help="use onnx runtime")
parser.add_argument("--profile", action="store_true", help="use profile model")
args = update_parser(parser, check_input_type=False)
splits = {
# fmt: off
",", "。", "?", "!", ",", ".", "?", "!", "~", ":", ":", "—", "…",
# fmt: on
}
# ======================
# Secondary Functions
# ======================
def split(todo_text):
todo_text = todo_text.replace("……", "。").replace("——", ",")
if todo_text[-1] not in splits:
todo_text += "。"
i_split_head = i_split_tail = 0
len_text = len(todo_text)
todo_texts = []
while 1:
if i_split_head >= len_text:
break # 结尾一定有标点,所以直接跳出即可,最后一段在上次已加入
if todo_text[i_split_head] in splits:
i_split_head += 1
todo_texts.append(todo_text[i_split_tail:i_split_head])
i_split_tail = i_split_head
else:
i_split_head += 1
return todo_texts
def cut(inp):
punctuation = set(["!", "?", "…", ",", ".", "-", " "])
inp = inp.strip("\n")
inps = split(inp)
split_idx = list(range(0, len(inps), 4))
split_idx[-1] = None
if len(split_idx) > 1:
opts = []
for idx in range(len(split_idx) - 1):
opts.append("".join(inps[split_idx[idx] : split_idx[idx + 1]]))
else:
opts = [inp]
opts = [item for item in opts if not set(item).issubset(punctuation)]
return "\n".join(opts)
def process_text(texts):
_text = []
if all(text in [None, " ", "\n", ""] for text in texts):
raise ValueError("Please enter valid text.")
for text in texts:
if text in [None, " ", ""]:
pass
else:
_text.append(text)
return _text
def merge_short_text_in_array(texts, threshold):
if (len(texts)) < 2:
return texts
result = []
text = ""
for ele in texts:
text += ele
if len(text) >= threshold:
result.append(text)
text = ""
if len(text) > 0:
if len(result) == 0:
result.append(text)
else:
result[len(result) - 1] += text
return result
# ======================
# Main Logic
# ======================
class T2SModel:
def __init__(self, sess_encoder, sess_fsdec, sess_sdec):
self.hz = 50
self.max_sec = 54
self.top_k = 5
self.early_stop_num = np.array([self.hz * self.max_sec])
self.sess_encoder = sess_encoder
self.sess_fsdec = sess_fsdec
self.sess_sdec = sess_sdec
def forward(
self,
ref_seq,
text_seq,
ref_bert,
text_bert,
ssl_content,
top_k=20,
top_p=0.6,
temperature=0.6,
repetition_penalty=1.35,
):
early_stop_num = self.early_stop_num
top_k = np.array([top_k], dtype=np.int64)
top_p = np.array([top_p], dtype=np.float32)
temperature = np.array([temperature], dtype=np.float32)
repetition_penalty = np.array([repetition_penalty], dtype=np.float32)
EOS = 1024
if args.benchmark:
start = int(round(time.time() * 1000))
if args.onnx:
x, prompts = self.sess_encoder.run(
None,
{
"ref_seq": ref_seq,
"text_seq": text_seq,
"ref_bert": ref_bert,
"text_bert": text_bert,
"ssl_content": ssl_content,
},
)
else:
x, prompts = self.sess_encoder.run(
{
"ref_seq": ref_seq,
"text_seq": text_seq,
"ref_bert": ref_bert,
"text_bert": text_bert,
"ssl_content": ssl_content,
}
)
if args.benchmark:
end = int(round(time.time() * 1000))
logger.info("\tsencoder processing time {} ms".format(end - start))
prefix_len = prompts.shape[1]
if args.benchmark:
start = int(round(time.time() * 1000))
if args.onnx:
y, k, v, y_emb, x_example = self.sess_fsdec.run(
None,
{
"x": x,
"prompts": prompts,
"top_k": top_k,
"top_p": top_p,
"temperature": temperature,
"repetition_penalty": repetition_penalty,
},
)
else:
y, k, v, y_emb, x_example = self.sess_fsdec.run(
{
"x": x,
"prompts": prompts,
"top_k": top_k,
"top_p": top_p,
"temperature": temperature,
"repetition_penalty": repetition_penalty,
}
)
if args.benchmark:
end = int(round(time.time() * 1000))
logger.info("\tfsdec processing time {} ms".format(end - start))
stop = False
for idx in tqdm(range(1, 1500)):
if args.benchmark:
start = int(round(time.time() * 1000))
if args.onnx:
y, k, v, y_emb, logits, samples = self.sess_sdec.run(
None,
{
"iy": y,
"ik": k,
"iv": v,
"iy_emb": y_emb,
"ix_example": x_example,
"top_k": top_k,
"top_p": top_p,
"temperature": temperature,
"repetition_penalty": repetition_penalty,
},
)
else:
COPY_INPUT_BLOB_DATA = False
if idx == 1:
y, k, v, y_emb, logits, samples = self.sess_sdec.run(
{
"iy": y,
"ik": k,
"iv": v,
"iy_emb": y_emb,
"ix_example": x_example,
"top_k": top_k,
"top_p": top_p,
"temperature": temperature,
"repetition_penalty": repetition_penalty,
}
)
kv_base_shape = k.shape
else:
input_blob_idx = self.sess_sdec.get_input_blob_list()
output_blob_idx = self.sess_sdec.get_output_blob_list()
self.sess_sdec.set_input_blob_data(y, 0)
if COPY_INPUT_BLOB_DATA:
kv_shape = (
kv_base_shape[0],
kv_base_shape[1] + idx - 2,
kv_base_shape[2],
kv_base_shape[3],
)
self.sess_sdec.set_input_blob_shape(kv_shape, 1)
self.sess_sdec.set_input_blob_shape(kv_shape, 2)
self.sess_sdec.copy_blob_data(
input_blob_idx[1], output_blob_idx[1], self.sess_sdec
)
self.sess_sdec.copy_blob_data(
input_blob_idx[2], output_blob_idx[2], self.sess_sdec
)
else:
self.sess_sdec.set_input_blob_data(k, 1)
self.sess_sdec.set_input_blob_data(v, 2)
self.sess_sdec.set_input_blob_data(y_emb, 3)
self.sess_sdec.set_input_blob_data(x_example, 4)
self.sess_sdec.set_input_blob_data(top_k, 5)
self.sess_sdec.set_input_blob_data(top_p, 6)
self.sess_sdec.set_input_blob_data(temperature, 7)
self.sess_sdec.set_input_blob_data(repetition_penalty, 8)
self.sess_sdec.update()
y = self.sess_sdec.get_blob_data(output_blob_idx[0])
if not COPY_INPUT_BLOB_DATA:
k = self.sess_sdec.get_blob_data(output_blob_idx[1])
v = self.sess_sdec.get_blob_data(output_blob_idx[2])
y_emb = self.sess_sdec.get_blob_data(output_blob_idx[3])
logits = self.sess_sdec.get_blob_data(output_blob_idx[4])
samples = self.sess_sdec.get_blob_data(output_blob_idx[5])
if args.benchmark:
end = int(round(time.time() * 1000))
logger.info("\tsdec processing time {} ms".format(end - start))
if early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num:
stop = True
if np.argmax(logits, axis=-1)[0] == EOS or samples[0, 0] == EOS:
stop = True
if stop:
break
y[0, -1] = 0
return y[np.newaxis, :, -idx:-1]
class GptSoVits:
def __init__(self, t2s: T2SModel, sess):
self.t2s = t2s
self.sess = sess
def forward(
self,
ref_seq,
text_seq,
ref_bert,
text_bert,
ref_audio,
ssl_content,
top_k=20,
top_p=0.6,
temperature=0.6,
repetition_penalty=1.35,
speed=1.0,
):
pred_semantic = self.t2s.forward(
ref_seq,
text_seq,
ref_bert,
text_bert,
ssl_content,
top_k=top_k,
top_p=top_p,
temperature=temperature,
repetition_penalty=repetition_penalty,
)
speed = np.array(speed, dtype=np.float32)
if args.benchmark:
start = int(round(time.time() * 1000))
if args.onnx:
audio1 = self.sess.run(
None,
{
"text_seq": text_seq,
"pred_semantic": pred_semantic,
"ref_audio": ref_audio,
"speed": speed,
},
)
else:
audio1 = self.sess.run(
{
"text_seq": text_seq,
"pred_semantic": pred_semantic,
"ref_audio": ref_audio,
"speed": speed,
}
)
if args.benchmark:
end = int(round(time.time() * 1000))
logger.info("\tvits processing time {} ms".format(end - start))
return audio1[0]
class SSLModel:
def __init__(self, sess):
self.sess = sess
def forward(self, ref_audio_16k):
if args.benchmark:
start = int(round(time.time() * 1000))
if args.onnx:
last_hidden_state = self.sess.run(None, {"ref_audio_16k": ref_audio_16k})
else:
last_hidden_state = self.sess.run({"ref_audio_16k": ref_audio_16k})
if args.benchmark:
end = int(round(time.time() * 1000))
logger.info("\tssl processing time {} ms".format(end - start))
return last_hidden_state[0]
def get_phones_and_bert(text, language, final=False):
if language == "en":
try:
import LangSegment
LangSegment.setfilters(["en"])
formattext = " ".join(tmp["text"] for tmp in LangSegment.getTexts(text))
except ImportError:
formattext = text
else:
formattext = text
while " " in formattext:
formattext = formattext.replace(" ", " ")
phones, word2ph, norm_text = clean_text(formattext, language)
phones = cleaned_text_to_sequence(phones)
bert = np.zeros((1024, len(phones)), dtype=np.float32)
if not final and len(phones) < 6:
return get_phones_and_bert("." + text, language, final=True)
return phones, bert, norm_text
def generate_voice(ssl, t2s_encoder, t2s_first_decoder, t2s_stage_decoder, vits):
gpt = T2SModel(
t2s_encoder,
t2s_first_decoder,
t2s_stage_decoder,
)
gpt_sovits = GptSoVits(gpt, vits)
ssl = SSLModel(ssl)
input_audio = args.ref_audio
ref_text = args.ref_text
ref_language = args.ref_language
text = args.input
text_language = args.text_language
top_k = args.top_k
top_p = args.top_p
temperature = args.temperature
speed = args.speed
ref_text = ref_text.strip("\n")
if ref_text[-1] not in splits:
ref_text += "。" if ref_language != "en" else "."
logger.info("Actual Input Reference Text: %s" % ref_text)
text = text.strip("\n")
logger.info("Actual Input Target Text: %s" % text)
vits_hps_data_sampling_rate = 32000
zero_wav = np.zeros(int(vits_hps_data_sampling_rate * 0.3), dtype=np.float16)
ref_audio, sr = librosa.load(input_audio, sr=vits_hps_data_sampling_rate)
ref_audio_16k = librosa.resample(ref_audio, orig_sr=sr, target_sr=16000)
if ref_audio_16k.shape[0] > 160000 or ref_audio_16k.shape[0] < 48000:
logger.warning(
"Reference audio is outside the 3-10 second range, please choose another one!"
)
# hubertの入力のみpaddingする
ref_audio_16k = np.concatenate([ref_audio_16k, zero_wav], axis=0)
ref_audio_16k = ref_audio_16k[np.newaxis, :]
ssl_content = ssl.forward(ref_audio_16k)
text = cut(text) # Slice once every 4 sentences
while "\n\n" in text:
text = text.replace("\n\n", "\n")
logger.info("Actual Input Target Text (after sentence segmentation): %s" % text)
texts = text.split("\n")
texts = process_text(texts)
texts = merge_short_text_in_array(texts, 5)
ref_seq, ref_bert, _ = get_phones_and_bert(ref_text, ref_language)
ref_seq = np.array(ref_seq)[np.newaxis, :]
ref_audio = ref_audio[np.newaxis, :]
audio_opt = []
for i_text, text in enumerate(texts):
# 解决输入目标文本的空行导致报错的问题
if len(text.strip()) == 0:
continue
if text[-1] not in splits:
text += "。" if text_language != "en" else "."
logger.info("Actual Input Target Text (per sentence): %s" % text)
text_seq, text_bert, norm_text = get_phones_and_bert(text, text_language)
text_seq = np.array(text_seq)[np.newaxis, :]
logger.info("Processed text from the frontend (per sentence): %s" % norm_text)
audio = gpt_sovits.forward(
ref_seq,
text_seq,
ref_bert.T,
text_bert.T,
ref_audio,
ssl_content,
top_k=top_k,
top_p=top_p,
temperature=temperature,
speed=speed,
)
max_audio = np.abs(audio).max()
if max_audio > 1:
audio /= max_audio
audio_opt.append(audio)
audio_opt.append(zero_wav)
audio = (np.concatenate(audio_opt, 0) * 32768).astype(np.int16)
savepath = args.savepath
logger.info(f"saved at : {savepath}")
soundfile.write(savepath, audio, vits_hps_data_sampling_rate)
logger.info("Script finished successfully.")
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH_SSL, MODEL_PATH_SSL, REMOTE_PATH)
check_and_download_models(
WEIGHT_PATH_T2S_ENCODER, MODEL_PATH_T2S_ENCODER, REMOTE_PATH
)
check_and_download_models(
WEIGHT_PATH_T2S_FIRST_DECODER, MODEL_PATH_T2S_FIRST_DECODER, REMOTE_PATH
)
check_and_download_models(
WEIGHT_PATH_T2S_STAGE_DECODER, MODEL_PATH_T2S_STAGE_DECODER, REMOTE_PATH
)
check_and_download_models(WEIGHT_PATH_VITS, MODEL_PATH_VITS, REMOTE_PATH)
env_id = args.env_id
if args.onnx:
import onnxruntime
ssl = onnxruntime.InferenceSession(WEIGHT_PATH_SSL)
t2s_encoder = onnxruntime.InferenceSession(WEIGHT_PATH_T2S_ENCODER)
t2s_first_decoder = onnxruntime.InferenceSession(WEIGHT_PATH_T2S_FIRST_DECODER)
t2s_stage_decoder = onnxruntime.InferenceSession(WEIGHT_PATH_T2S_STAGE_DECODER)
vits = onnxruntime.InferenceSession(WEIGHT_PATH_VITS)
else:
memory_mode = ailia.get_memory_mode(
reduce_constant=True,
ignore_input_with_initializer=True,
reduce_interstage=False,
reuse_interstage=True,
)
ssl = ailia.Net(
weight=WEIGHT_PATH_SSL,
stream=MODEL_PATH_SSL,
memory_mode=memory_mode,
env_id=env_id,
)
t2s_encoder = ailia.Net(
weight=WEIGHT_PATH_T2S_ENCODER,
stream=MODEL_PATH_T2S_ENCODER,
memory_mode=memory_mode,
env_id=env_id,
)
t2s_first_decoder = ailia.Net(
weight=WEIGHT_PATH_T2S_FIRST_DECODER,
stream=MODEL_PATH_T2S_FIRST_DECODER,
memory_mode=memory_mode,
env_id=env_id,
)
t2s_stage_decoder = ailia.Net(
weight=WEIGHT_PATH_T2S_STAGE_DECODER,
stream=MODEL_PATH_T2S_STAGE_DECODER,
memory_mode=memory_mode,
env_id=env_id,
)
vits = ailia.Net(
weight=WEIGHT_PATH_VITS,
stream=MODEL_PATH_VITS,
memory_mode=memory_mode,
env_id=env_id,
)
if args.profile:
ssl.set_profile_mode(True)
t2s_encoder.set_profile_mode(True)
t2s_first_decoder.set_profile_mode(True)
t2s_stage_decoder.set_profile_mode(True)
vits.set_profile_mode(True)
if args.benchmark:
start = int(round(time.time() * 1000))
generate_voice(ssl, t2s_encoder, t2s_first_decoder, t2s_stage_decoder, vits)
if args.benchmark:
end = int(round(time.time() * 1000))
logger.info("\ttotal processing time {} ms".format(end - start))
if args.profile:
print("ssl : ")
print(ssl.get_summary())
print("t2s_encoder : ")
print(t2s_encoder.get_summary())
print("t2s_first_decoder : ")
print(t2s_first_decoder.get_summary())
print("t2s_stage_decoder : ")
print(t2s_stage_decoder.get_summary())
print("vits : ")
print(vits.get_summary())
if __name__ == "__main__":
main()