-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.py
226 lines (193 loc) · 7.47 KB
/
interact.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
# -*- coding: utf-8 -*-
import sys, re
from torch import no_grad, LongTensor
import logging
logging.getLogger('numba').setLevel(logging.WARNING)
import commons
import utils
from models import SynthesizerTrn
from text import text_to_sequence, _clean_text
from mel_processing import spectrogram_torch
from scipy.io.wavfile import write
config = "config/config.json"
api_hps_ms = utils.get_hparams_from_file(config)
api_net_g_ms = SynthesizerTrn(
len(api_hps_ms.symbols),
api_hps_ms.data.filter_length // 2 + 1,
api_hps_ms.train.segment_size // api_hps_ms.data.hop_length,
n_speakers=api_hps_ms.data.n_speakers,
**api_hps_ms.model)
_ = api_net_g_ms.eval()
utils.load_checkpoint("model/1374_epochs.pth", api_net_g_ms)
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():
"""原方法是是否继续,这里直接结束"""
# sys.exit(0)
# while True:
# answer = input('Continue? (y/n): ')
# if answer == 'y':
# break
# elif answer == 'n':
# sys.exit(0)
def print_speakers(speakers):
pass
# print('ID\tSpeaker')
# for id, name in enumerate(speakers):
# print(str(id) + '\t' + name)
def get_speaker_id(message):
"""
speaker_id :
0 綾地寧々
1 在原七海
2 小茸
3 唐乐吟
"""
speaker_id = input(message)
try:
speaker_id = int(speaker_id)
except:
print(str(speaker_id) + ' is not a valid ID!')
sys.exit(1)
return speaker_id
class Interact:
def __init__(self, text, out_path, hps_ms=None, speaker_id=0, choice='t', model='model/1374_epochs.pth',
config="config/config.json"):
speaker_id = speaker_id if speaker_id is not None else 0
choice = choice if choice is not None else 't'
model = model if model is not None else 'model/1374_epochs.pth'
config = config if config is not None else "config/config.json"
self.run(model, config, choice, text, out_path, speaker_id=speaker_id, audio_path=None, hps_ms=hps_ms)
def run(self, model, config, choice, text, out_path, speaker_id=0, audio_path=None, hps_ms=None):
"""
model:
模型路径
config:
配置文件路径
choice:
选择模式:
t :TTS
c :CV
text :
参数格式 : [ZH]中文[ZH] 或者 [JA]日本語[JA]
out_path :
输出路径 例:save/TTS/read.wav
speaker_id :
0 綾地寧々
1 在原七海
2 小茸
3 唐乐吟
audio_path :
cv 模式的 输入音频
"""
# model = input('Path of a VITS model: ')
# config = input('Path of a config file: ')
try:
if hps_ms is not None:
hps_ms = hps_ms if hps_ms is not None else api_hps_ms
net_g_ms = api_net_g_ms if hps_ms is None else SynthesizerTrn(
len(hps_ms.symbols),
hps_ms.data.filter_length // 2 + 1,
hps_ms.train.segment_size // hps_ms.data.hop_length,
n_speakers=hps_ms.data.n_speakers,
**hps_ms.model)
print('重新运算')
_ = net_g_ms.eval()
utils.load_checkpoint(model, net_g_ms)
else:
hps_ms = api_hps_ms
net_g_ms = api_net_g_ms
except:
print('Failed to load!')
sys.exit(1)
# while True:
# choice = input('TTS or VC? (t/v):')
if choice == 't':
# text = input('Text to read: ')
if text == '[ADVANCED]':
text = input('Raw text:')
print('Cleaned text is:')
print(_clean_text(text, hps_ms.data.text_cleaners))
# continue
length_scale = re.search(r'\[LENGTH=(.+?)\]', text)
if length_scale:
try:
text = re.sub(r'\[LENGTH=(.+?)\]', '', text)
length_scale = float(length_scale.group(1))
except:
print('Invalid length scale!')
sys.exit(1)
else:
length_scale = 1
if '[CLEANED]' in text:
try:
stn_tst = get_text(text.replace('[CLEANED]', ''), hps_ms, cleaned=True)
except:
print('Invalid text!')
sys.exit(1)
else:
try:
stn_tst = get_text(text, hps_ms)
except EOFError:
print(EOFError)
print('Invalid text!')
sys.exit(1)
print_speakers(hps_ms.speakers)
# speaker_id = get_speaker_id('Speaker ID: ')
# speaker_id = get_speaker_id('Speaker ID: ')
length_scale
# out_path = input('Path to save: ')
try:
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=.667, noise_scale_w=0.8,
length_scale=length_scale)[0][0, 0].data.cpu().float().numpy()
write(out_path, hps_ms.data.sampling_rate, audio)
except EOFError:
print(EOFError)
print('Failed to generate!')
sys.exit(1)
print('Successfully saved!')
ask_if_continue()
elif choice == 'v':
# audio_path = input('Path of an audio file to convert:\n')
print_speakers(hps_ms.speakers)
audio = utils.load_audio_to_torch(audio_path, hps_ms.data.sampling_rate)
originnal_id = get_speaker_id('Original speaker ID: ')
target_id = get_speaker_id('Target speaker ID: ')
# out_path = input('Path to save: ')
y = audio.unsqueeze(0)
spec = spectrogram_torch(y, hps_ms.data.filter_length,
hps_ms.data.sampling_rate, hps_ms.data.hop_length, hps_ms.data.win_length,
center=False)
spec_lengths = LongTensor([spec.size(-1)])
sid_src = LongTensor([originnal_id])
try:
with no_grad():
sid_tgt = LongTensor([target_id])
audio = net_g_ms.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt)[0][
0, 0].data.cpu().float().numpy()
write(out_path, hps_ms.data.sampling_rate, audio)
except:
print('Failed to generate!')
sys.exit(1)
print('Successfully saved!')
ask_if_continue()
if __name__ == '__main__':
model = "model/1374_epochs.pth"
config = "config/config.json"
choice = "t"
text = "[ZH]风萧萧兮易水寒[ZH]"
# text = "[JA]風蕭蕭として易水寒[JA]"
out_path = "save/TTS/read.wav"
speaker_id = 0
Interact(text, out_path, speaker_id)