-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtts_frontend_main.py
182 lines (156 loc) · 6.75 KB
/
tts_frontend_main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2019-10-11 14:05
# @Author : liufeng
import argparse
import logging
import os
import pickle
from collections import namedtuple
import yaml
from src.get_model_path import get_model_path
from src.model.char2phone import TranscriptToPinyin
from src.text_normalizer.text import Text
from src.utils import read_lines, write_lines
from src.utils import split_sentence, clean_sentence
ROOT_DIR = "/data1/liufeng/synthesis/TACOTRON-2-refined"
os.environ["CUDA_VISIBLE_DEVICES"] = "{}".format(-1)
def __load_hparams(yaml_path):
with open(yaml_path, encoding="utf-8") as yaml_file:
hp_yaml = yaml.load(yaml_file.read())
Hparams = namedtuple('Hparams', hp_yaml.keys())
hparams = Hparams(*hp_yaml.values())
return hparams
def __compute_psd_result(hparams, sentences, load_memory=False):
from src.model.prosody import BertProsody
psd_predict = BertProsody()
bert_result_path = os.path.join(hparams.psd_model_dir, "bert_result.pkl")
if os.path.exists(bert_result_path) and load_memory:
with open(bert_result_path, "rb") as fr:
bert_psd_result = pickle.load(fr)
else:
bert_input = []
for sent_id, sentence in enumerate(sentences):
sentence = clean_sentence(sentence)
sub_sentences = split_sentence(sentence)
bert_input.extend(sub_sentences)
model_path, init_epoch = get_model_path(
os.path.join(hparams.psd_model_dir, "hdf5"))
psd_predict.initial_model(bert_model_path=hparams.bert_model_path,
psd_model_path=model_path)
bert_psd_result = psd_predict.predict(bert_input)
with open(bert_result_path, "wb") as fw:
pickle.dump(bert_psd_result, fw)
print("completed bert inference")
return psd_predict, bert_psd_result
def __compute_nnet_phone_result(hparams, sentences, load_memory=False):
from src.model.phone import BertPolyPhone
phone_predictor = BertPolyPhone()
bert_result_path = os.path.join(hparams.poly_model_dir, "bert_result.pkl")
if os.path.exists(bert_result_path) and load_memory:
with open(bert_result_path, "rb") as fr:
bert_phone_result = pickle.load(fr)
else:
bert_input = []
for sent_id, sentence in enumerate(sentences):
sentence = clean_sentence(sentence)
sub_sentences = split_sentence(sentence)
bert_input.extend(sub_sentences)
print("total sub sentences:{}".format(len(bert_input)))
model_path, init_epoch = get_model_path(
os.path.join(hparams.poly_model_dir, "hdf5"))
phone_predictor.inialize_model(bert_model_path=hparams.bert_model_path,
poly_model_path=model_path)
bert_phone_result = phone_predictor.predict(bert_input)
with open(bert_result_path, "wb") as fw:
pickle.dump(bert_phone_result, fw)
print("completed bert inference")
return phone_predictor, bert_phone_result
def main():
parser = argparse.ArgumentParser()
parser.add_argument('yaml_path', help='config path for frontend')
parser.add_argument('input_path', help='input path(txt)')
parser.add_argument('output_path', help='output path(txt)')
args = parser.parse_args()
# todo: add sil label
hparams = __load_hparams(args.yaml_path)
text_path = args.input_path
frontend_path = args.output_path
flag_psd = hparams.flag_psd
if hparams.norm_text:
raw_file_lines = read_lines(text_path)
sentences = []
print("text normalize:")
for line in raw_file_lines:
new_line = Text().normalize(line)
sentences.append(new_line.replace(" ", ""))
if not new_line == line:
print("{}->{}".format(line, new_line))
else:
sentences = read_lines(text_path)
write_lines("norm.txt", sentences)
# exit()
trans = TranscriptToPinyin(dic_path=hparams.dict_path,
eng_dic_path=hparams.eng_dict_path, )
if hparams.nnet_psd and hparams.flag_psd:
psd_predict, bert_psd_result = __compute_psd_result(
hparams, sentences, hparams.load_memory_psd)
else:
psd_predict, bert_psd_result = None, None
if hparams.nnet_phone:
phone_predictor, bert_phone_result = __compute_nnet_phone_result(
hparams, sentences, hparams.load_memory_phone)
else:
phone_predictor, bert_phone_result = None, None
sub_count, count = 0, 0
with open(frontend_path, "w", encoding="utf-8") as frontend_file:
for sent_id, sentence in enumerate(sentences[0:]):
# sentence = num2hanzi(clean_sentence(sentence))
frontend_file.write("\nid:{}\n{}\n".format(sent_id, sentence))
print("\nid:{}\n{}".format(sent_id, sentence))
sub_sentences = split_sentence(sentence)
for split_id, sub_sentence in enumerate(sub_sentences):
sub_count += 1
phone_pairs = trans.get_phone_pairs(
sub_sentence, change_eng_symbol=hparams.eng_symbol)
new_ = []
if hparams.nnet_phone:
bert_phone = bert_phone_result[count]
if len(sub_sentence) == len(bert_phone):
phone = phone_predictor.modify_result(bert_phone, phone_pairs)
for i, (c, ph, p) in enumerate(phone_pairs):
new_.append((c, phone[i], p))
phone_pairs = new_
else:
print("Error for bert result")
if flag_psd and not hparams.nnet_psd:
phone = " ".join([ph + " #" + psd for _, ph, psd in phone_pairs])
phone = phone.replace("#0", "").replace("#5", "")
sub_sentence = "".join([c + "#" + psd for c, _, psd in phone_pairs])
sub_sentence = sub_sentence.replace("#0", "").replace("#5", "")
elif flag_psd and hparams.nnet_psd:
new_pairs = []
for new_psd, (char, ph, _) in zip(bert_psd_result[count],
phone_pairs):
new_pairs.append((char, ph, new_psd))
new_pairs = psd_predict.change_by_rules(new_pairs)
phone = " ".join([ph + " #" + psd for _, ph, psd in new_pairs])
phone = phone.replace("#0", "").replace("#5", "")
sub_sentence = "".join([c + "#" + psd for c, _, psd in new_pairs])
sub_sentence = sub_sentence.replace("#0", "").replace("#5", "")
else:
phone = " ".join([ph for _, ph, _ in phone_pairs])
sub_sentence = "".join([c for c, _, _ in phone_pairs])
count += 1
frontend_file.write(
"split-id:{} | {}\n{}\n".format(split_id, sub_sentence, phone))
print("split-id:{} | {} | {}".format(split_id, sub_sentence, phone))
frontend_file.write("split-end\n")
# todo: 改善停顿。
# todo: 重构,废弃kashagri,使用keras-bert
print("\nsub count:{}".format(sub_count))
print("write output data to {}".format(frontend_path))
if __name__ == '__main__':
logging.basicConfig(format="%(asctime)s %(name)s %(levelname)s %(message)s",
level=logging.INFO)
main()