forked from netease-youdao/EmotiVoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
234 lines (202 loc) · 8.17 KB
/
predict.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
# Prediction interface for Cog ⚙️DEVICE
# https://github.com/replicate/cog/blob/main/docs/python.md
from cog import BasePredictor, Input, Path
from typing import List
import numpy as np
from yacs import config as CONFIG
import torch
import re
import os, glob
import time
import subprocess
import requests
import soundfile as sf
from frontend_cn import g2p_cn
from frontend_en import preprocess_english
from config.joint.config import Config
from models.prompt_tts_modified.jets import JETSGenerator
from models.prompt_tts_modified.simbert import StyleEncoder
from transformers import AutoTokenizer
MAX_WAV_VALUE = 32768.0
# url for the weights mirror
REPLICATE_WEIGHTS_URL = "https://weights.replicate.delivery/default"
# files to download from the weights mirrors
DEFAULT_WEIGHTS = [
{
"dest": "outputs/prompt_tts_open_source_joint/ckpt",
"src": "EmotiVoice",
"files": [
"do_00140000",
"g_00140000",
],
},
{
"dest": "outputs/style_encoder/ckpt",
"src": "EmotiVoice",
"files": [
"checkpoint_163431",
],
},
{
"dest": "WangZeJun/simbert-base-chinese",
"src": "simbert-base-chinese/b5c82a8ab1e4bcac799620fc4d870aae087b0c71",
"files": [
"pytorch_model.bin",
"config.json",
"vocab.txt",
],
}
]
def scan_checkpoint(cp_dir, prefix, c=8):
pattern = os.path.join(cp_dir, prefix + '?'*c)
cp_list = glob.glob(pattern)
if len(cp_list) == 0:
return None
return sorted(cp_list)[-1]
def g2p_en(text):
return preprocess_english(text)
def contains_chinese(text):
pattern = re.compile(r'[\u4e00-\u9fa5]')
match = re.search(pattern, text)
return match is not None
def download_json(url: str, dest: Path):
res = requests.get(url, allow_redirects=True)
if res.status_code == 200 and res.content:
with dest.open("wb") as f:
f.write(res.content)
else:
print(f"Failed to download {url}. Status code: {res.status_code}")
def download_weights(baseurl: str, basedest: str, files: List[str]):
"""Download model weights from Replicate and save to file.
Weights and download locations are specified in DEFAULT_WEIGHTS
"""
basedest = Path(basedest)
start = time.time()
print("downloading to: ", basedest)
basedest.mkdir(parents=True, exist_ok=True)
for f in files:
dest = basedest / f
url = os.path.join(REPLICATE_WEIGHTS_URL, baseurl, f)
if not dest.exists():
print("downloading url: ", url)
if dest.suffix == ".json":
download_json(url, dest)
else:
subprocess.check_call(["pget", url, str(dest)], close_fds=False)
print("downloading took: ", time.time() - start)
class Predictor(BasePredictor):
def setup_models(self):
config = self.config
am_checkpoint_path = scan_checkpoint(f'{config.output_directory}/prompt_tts_open_source_joint/ckpt', 'g_')
style_encoder_checkpoint_path = scan_checkpoint(f'{config.output_directory}/style_encoder/ckpt', 'checkpoint_', 6)
with open(config.model_config_path, 'r') as fin:
conf = CONFIG.load_cfg(fin)
conf.n_vocab = config.n_symbols
conf.n_speaker = config.speaker_n_labels
style_encoder = StyleEncoder(config)
model_CKPT = torch.load(style_encoder_checkpoint_path, map_location="cpu")
model_ckpt = {}
for key, value in model_CKPT['model'].items():
new_key = key[7:]
model_ckpt[new_key] = value
style_encoder.load_state_dict(model_ckpt, strict=False)
generator = JETSGenerator(conf).to(self.device)
model_CKPT = torch.load(am_checkpoint_path, map_location=self.device)
generator.load_state_dict(model_CKPT['generator'])
generator.eval()
self.tokenizer = AutoTokenizer.from_pretrained(config.bert_path)
with open(config.token_list_path, 'r') as f:
self.token2id = {t.strip():idx for idx, t, in enumerate(f.readlines())}
with open(config.speaker2id_path, encoding='utf-8') as f:
self.speaker2id = {t.strip():idx for idx, t in enumerate(f.readlines())}
self.style_encoder = style_encoder
self.generator = generator
print(self.tokenizer)
def setup(self) -> None:
"""Load the model into memory to make running multiple predictions efficient"""
# self.model = torch.load("./weights.pth")
for weight in DEFAULT_WEIGHTS:
download_weights(weight["src"], weight["dest"], weight["files"])
self.config = Config()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.setup_models()
# def get_style_embedding(prompt, tokenizer, style_encoder):
def get_style_embedding(self, text):
tokenizer = self.tokenizer
style_encoder = self.style_encoder
text = tokenizer([text], return_tensors="pt")
input_ids = text["input_ids"]
token_type_ids = text["token_type_ids"]
attention_mask = text["attention_mask"]
with torch.no_grad():
output = style_encoder(
input_ids=input_ids,
token_type_ids=token_type_ids,
attention_mask=attention_mask,
)
style_embedding = output["pooled_output"].cpu().squeeze().numpy()
return style_embedding
def tts(self, text, prompt, content, speaker):
style_embedding = self.get_style_embedding(prompt)
content_embedding = self.get_style_embedding(content)
device = self.device
speaker = self.speaker2id[speaker]
text_int = [self.token2id[ph] for ph in text.split()]
sequence = torch.from_numpy(np.array(text_int)).to(device).long().unsqueeze(0)
sequence_len = torch.from_numpy(np.array([len(text_int)])).to(device)
style_embedding = torch.from_numpy(style_embedding).to(device).unsqueeze(0)
content_embedding = torch.from_numpy(content_embedding).to(device).unsqueeze(0)
speaker = torch.from_numpy(np.array([speaker])).to(device)
with torch.no_grad():
infer_output = self.generator(
inputs_ling=sequence,
inputs_style_embedding=style_embedding,
input_lengths=sequence_len,
inputs_content_embedding=content_embedding,
inputs_speaker=speaker,
alpha=1.0
)
audio = infer_output["wav_predictions"].squeeze()* MAX_WAV_VALUE
audio = audio.cpu().numpy().astype('int16')
path = os.path.join(self.config.output_directory,"output.mp3")
sf.write(file=path, data=audio, samplerate=self.config.sampling_rate)
return path
def predict(
self,
prompt: str = Input(
description="Input prompt",
default="Happy",
),
content: str = Input(
description="Input text",
default="Emoti-Voice - a Multi-Voice and Prompt-Controlled T-T-S Engine",
),
language: str = Input(
description="Language",
choices=["English", "Chinese"],
default="English",
),
speaker: str = Input(
description="speakers",
choices=Config().speakers,
default=Config().speakers[0],
),
) -> Path:
"""Run a single prediction on the model"""
# processed_input = preprocess(image)
# output = self.model(processed_image, scale)
# return postprocess(output)
if language=="English":
if contains_chinese(content):
raise ValueError("文本含有中文/input text contains Chinese, but language is English")
else:
text = g2p_en(content)
path = self.tts(text, prompt, content, speaker)
return Path(path)
else:
if not contains_chinese(content):
raise ValueError("文本含有英文/input text contains English, but language is Chinese")
else:
text = g2p_cn(content)
path = self.tts(text, prompt, content, speaker)
return Path(path)