mirror of
https://github.com/RVC-Boss/GPT-SoVITS.git
synced 2026-01-09 03:46:58 +08:00
Simplify i18n text and remove trailing spaces
This commit is contained in:
parent
4748f1a6b0
commit
9ce83c8eea
@ -79,7 +79,7 @@ def set_seed(seed:int):
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return seed
|
return seed
|
||||||
|
|
||||||
class TTS_Config:
|
class TTS_Config:
|
||||||
default_configs={
|
default_configs={
|
||||||
"default":{
|
"default":{
|
||||||
@ -118,18 +118,18 @@ class TTS_Config:
|
|||||||
# "auto_yue",#多语种启动切分识别语种
|
# "auto_yue",#多语种启动切分识别语种
|
||||||
|
|
||||||
def __init__(self, configs: Union[dict, str]=None):
|
def __init__(self, configs: Union[dict, str]=None):
|
||||||
|
|
||||||
# 设置默认配置文件路径
|
# 设置默认配置文件路径
|
||||||
configs_base_path:str = "GPT_SoVITS/configs/"
|
configs_base_path:str = "GPT_SoVITS/configs/"
|
||||||
os.makedirs(configs_base_path, exist_ok=True)
|
os.makedirs(configs_base_path, exist_ok=True)
|
||||||
self.configs_path:str = os.path.join(configs_base_path, "tts_infer.yaml")
|
self.configs_path:str = os.path.join(configs_base_path, "tts_infer.yaml")
|
||||||
|
|
||||||
if configs in ["", None]:
|
if configs in ["", None]:
|
||||||
if not os.path.exists(self.configs_path):
|
if not os.path.exists(self.configs_path):
|
||||||
self.save_configs()
|
self.save_configs()
|
||||||
print(f"Create default config file at {self.configs_path}")
|
print(f"Create default config file at {self.configs_path}")
|
||||||
configs:dict = deepcopy(self.default_configs)
|
configs:dict = deepcopy(self.default_configs)
|
||||||
|
|
||||||
if isinstance(configs, str):
|
if isinstance(configs, str):
|
||||||
self.configs_path = configs
|
self.configs_path = configs
|
||||||
configs:dict = self._load_configs(self.configs_path)
|
configs:dict = self._load_configs(self.configs_path)
|
||||||
@ -142,8 +142,8 @@ class TTS_Config:
|
|||||||
|
|
||||||
default_config_key = "default"if version=="v1" else "default_v2"
|
default_config_key = "default"if version=="v1" else "default_v2"
|
||||||
self.configs:dict = configs.get("custom", deepcopy(self.default_configs[default_config_key]))
|
self.configs:dict = configs.get("custom", deepcopy(self.default_configs[default_config_key]))
|
||||||
|
|
||||||
|
|
||||||
self.device = self.configs.get("device", torch.device("cpu"))
|
self.device = self.configs.get("device", torch.device("cpu"))
|
||||||
self.is_half = self.configs.get("is_half", False)
|
self.is_half = self.configs.get("is_half", False)
|
||||||
self.version = version
|
self.version = version
|
||||||
@ -153,7 +153,7 @@ class TTS_Config:
|
|||||||
self.cnhuhbert_base_path = self.configs.get("cnhuhbert_base_path", None)
|
self.cnhuhbert_base_path = self.configs.get("cnhuhbert_base_path", None)
|
||||||
self.languages = self.v2_languages if self.version=="v2" else self.v1_languages
|
self.languages = self.v2_languages if self.version=="v2" else self.v1_languages
|
||||||
|
|
||||||
|
|
||||||
if (self.t2s_weights_path in [None, ""]) or (not os.path.exists(self.t2s_weights_path)):
|
if (self.t2s_weights_path in [None, ""]) or (not os.path.exists(self.t2s_weights_path)):
|
||||||
self.t2s_weights_path = self.default_configs[default_config_key]['t2s_weights_path']
|
self.t2s_weights_path = self.default_configs[default_config_key]['t2s_weights_path']
|
||||||
print(f"fall back to default t2s_weights_path: {self.t2s_weights_path}")
|
print(f"fall back to default t2s_weights_path: {self.t2s_weights_path}")
|
||||||
@ -167,8 +167,8 @@ class TTS_Config:
|
|||||||
self.cnhuhbert_base_path = self.default_configs[default_config_key]['cnhuhbert_base_path']
|
self.cnhuhbert_base_path = self.default_configs[default_config_key]['cnhuhbert_base_path']
|
||||||
print(f"fall back to default cnhuhbert_base_path: {self.cnhuhbert_base_path}")
|
print(f"fall back to default cnhuhbert_base_path: {self.cnhuhbert_base_path}")
|
||||||
self.update_configs()
|
self.update_configs()
|
||||||
|
|
||||||
|
|
||||||
self.max_sec = None
|
self.max_sec = None
|
||||||
self.hz:int = 50
|
self.hz:int = 50
|
||||||
self.semantic_frame_rate:str = "25hz"
|
self.semantic_frame_rate:str = "25hz"
|
||||||
@ -180,7 +180,7 @@ class TTS_Config:
|
|||||||
self.n_speakers:int = 300
|
self.n_speakers:int = 300
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _load_configs(self, configs_path: str)->dict:
|
def _load_configs(self, configs_path: str)->dict:
|
||||||
if os.path.exists(configs_path):
|
if os.path.exists(configs_path):
|
||||||
...
|
...
|
||||||
@ -189,14 +189,14 @@ class TTS_Config:
|
|||||||
self.save_configs(configs_path)
|
self.save_configs(configs_path)
|
||||||
with open(configs_path, 'r') as f:
|
with open(configs_path, 'r') as f:
|
||||||
configs = yaml.load(f, Loader=yaml.FullLoader)
|
configs = yaml.load(f, Loader=yaml.FullLoader)
|
||||||
|
|
||||||
return configs
|
return configs
|
||||||
|
|
||||||
def save_configs(self, configs_path:str=None)->None:
|
def save_configs(self, configs_path:str=None)->None:
|
||||||
configs=deepcopy(self.default_configs)
|
configs=deepcopy(self.default_configs)
|
||||||
if self.configs is not None:
|
if self.configs is not None:
|
||||||
configs["custom"] = self.update_configs()
|
configs["custom"] = self.update_configs()
|
||||||
|
|
||||||
if configs_path is None:
|
if configs_path is None:
|
||||||
configs_path = self.configs_path
|
configs_path = self.configs_path
|
||||||
with open(configs_path, 'w') as f:
|
with open(configs_path, 'w') as f:
|
||||||
@ -217,7 +217,7 @@ class TTS_Config:
|
|||||||
def update_version(self, version:str)->None:
|
def update_version(self, version:str)->None:
|
||||||
self.version = version
|
self.version = version
|
||||||
self.languages = self.v2_languages if self.version=="v2" else self.v1_languages
|
self.languages = self.v2_languages if self.version=="v2" else self.v1_languages
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
self.configs = self.update_configs()
|
self.configs = self.update_configs()
|
||||||
string = "TTS Config".center(100, '-') + '\n'
|
string = "TTS Config".center(100, '-') + '\n'
|
||||||
@ -225,7 +225,7 @@ class TTS_Config:
|
|||||||
string += f"{str(k).ljust(20)}: {str(v)}\n"
|
string += f"{str(k).ljust(20)}: {str(v)}\n"
|
||||||
string += "-" * 100 + '\n'
|
string += "-" * 100 + '\n'
|
||||||
return string
|
return string
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
@ -242,21 +242,21 @@ class TTS:
|
|||||||
self.configs = configs
|
self.configs = configs
|
||||||
else:
|
else:
|
||||||
self.configs:TTS_Config = TTS_Config(configs)
|
self.configs:TTS_Config = TTS_Config(configs)
|
||||||
|
|
||||||
self.t2s_model:Text2SemanticLightningModule = None
|
self.t2s_model:Text2SemanticLightningModule = None
|
||||||
self.vits_model:SynthesizerTrn = None
|
self.vits_model:SynthesizerTrn = None
|
||||||
self.bert_tokenizer:AutoTokenizer = None
|
self.bert_tokenizer:AutoTokenizer = None
|
||||||
self.bert_model:AutoModelForMaskedLM = None
|
self.bert_model:AutoModelForMaskedLM = None
|
||||||
self.cnhuhbert_model:CNHubert = None
|
self.cnhuhbert_model:CNHubert = None
|
||||||
|
|
||||||
self._init_models()
|
self._init_models()
|
||||||
|
|
||||||
self.text_preprocessor:TextPreprocessor = \
|
self.text_preprocessor:TextPreprocessor = \
|
||||||
TextPreprocessor(self.bert_model,
|
TextPreprocessor(self.bert_model,
|
||||||
self.bert_tokenizer,
|
self.bert_tokenizer,
|
||||||
self.configs.device)
|
self.configs.device)
|
||||||
|
|
||||||
|
|
||||||
self.prompt_cache:dict = {
|
self.prompt_cache:dict = {
|
||||||
"ref_audio_path" : None,
|
"ref_audio_path" : None,
|
||||||
"prompt_semantic": None,
|
"prompt_semantic": None,
|
||||||
@ -268,8 +268,8 @@ class TTS:
|
|||||||
"norm_text" : None,
|
"norm_text" : None,
|
||||||
"aux_ref_audio_paths": [],
|
"aux_ref_audio_paths": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
self.stop_flag:bool = False
|
self.stop_flag:bool = False
|
||||||
self.precision:torch.dtype = torch.float16 if self.configs.is_half else torch.float32
|
self.precision:torch.dtype = torch.float16 if self.configs.is_half else torch.float32
|
||||||
|
|
||||||
@ -279,9 +279,9 @@ class TTS:
|
|||||||
self.init_bert_weights(self.configs.bert_base_path)
|
self.init_bert_weights(self.configs.bert_base_path)
|
||||||
self.init_cnhuhbert_weights(self.configs.cnhuhbert_base_path)
|
self.init_cnhuhbert_weights(self.configs.cnhuhbert_base_path)
|
||||||
# self.enable_half_precision(self.configs.is_half)
|
# self.enable_half_precision(self.configs.is_half)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def init_cnhuhbert_weights(self, base_path: str):
|
def init_cnhuhbert_weights(self, base_path: str):
|
||||||
print(f"Loading CNHuBERT weights from {base_path}")
|
print(f"Loading CNHuBERT weights from {base_path}")
|
||||||
self.cnhuhbert_model = CNHubert(base_path)
|
self.cnhuhbert_model = CNHubert(base_path)
|
||||||
@ -289,9 +289,9 @@ class TTS:
|
|||||||
self.cnhuhbert_model = self.cnhuhbert_model.to(self.configs.device)
|
self.cnhuhbert_model = self.cnhuhbert_model.to(self.configs.device)
|
||||||
if self.configs.is_half and str(self.configs.device)!="cpu":
|
if self.configs.is_half and str(self.configs.device)!="cpu":
|
||||||
self.cnhuhbert_model = self.cnhuhbert_model.half()
|
self.cnhuhbert_model = self.cnhuhbert_model.half()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def init_bert_weights(self, base_path: str):
|
def init_bert_weights(self, base_path: str):
|
||||||
print(f"Loading BERT weights from {base_path}")
|
print(f"Loading BERT weights from {base_path}")
|
||||||
self.bert_tokenizer = AutoTokenizer.from_pretrained(base_path)
|
self.bert_tokenizer = AutoTokenizer.from_pretrained(base_path)
|
||||||
@ -300,7 +300,7 @@ class TTS:
|
|||||||
self.bert_model = self.bert_model.to(self.configs.device)
|
self.bert_model = self.bert_model.to(self.configs.device)
|
||||||
if self.configs.is_half and str(self.configs.device)!="cpu":
|
if self.configs.is_half and str(self.configs.device)!="cpu":
|
||||||
self.bert_model = self.bert_model.half()
|
self.bert_model = self.bert_model.half()
|
||||||
|
|
||||||
def init_vits_weights(self, weights_path: str):
|
def init_vits_weights(self, weights_path: str):
|
||||||
print(f"Loading VITS weights from {weights_path}")
|
print(f"Loading VITS weights from {weights_path}")
|
||||||
self.configs.vits_weights_path = weights_path
|
self.configs.vits_weights_path = weights_path
|
||||||
@ -311,11 +311,11 @@ class TTS:
|
|||||||
else:
|
else:
|
||||||
self.configs.update_version("v2")
|
self.configs.update_version("v2")
|
||||||
self.configs.save_configs()
|
self.configs.save_configs()
|
||||||
|
|
||||||
hps["model"]["version"] = self.configs.version
|
hps["model"]["version"] = self.configs.version
|
||||||
self.configs.filter_length = hps["data"]["filter_length"]
|
self.configs.filter_length = hps["data"]["filter_length"]
|
||||||
self.configs.segment_size = hps["train"]["segment_size"]
|
self.configs.segment_size = hps["train"]["segment_size"]
|
||||||
self.configs.sampling_rate = hps["data"]["sampling_rate"]
|
self.configs.sampling_rate = hps["data"]["sampling_rate"]
|
||||||
self.configs.hop_length = hps["data"]["hop_length"]
|
self.configs.hop_length = hps["data"]["hop_length"]
|
||||||
self.configs.win_length = hps["data"]["win_length"]
|
self.configs.win_length = hps["data"]["win_length"]
|
||||||
self.configs.n_speakers = hps["data"]["n_speakers"]
|
self.configs.n_speakers = hps["data"]["n_speakers"]
|
||||||
@ -330,7 +330,7 @@ class TTS:
|
|||||||
|
|
||||||
if hasattr(vits_model, "enc_q"):
|
if hasattr(vits_model, "enc_q"):
|
||||||
del vits_model.enc_q
|
del vits_model.enc_q
|
||||||
|
|
||||||
vits_model = vits_model.to(self.configs.device)
|
vits_model = vits_model.to(self.configs.device)
|
||||||
vits_model = vits_model.eval()
|
vits_model = vits_model.eval()
|
||||||
vits_model.load_state_dict(dict_s2["weight"], strict=False)
|
vits_model.load_state_dict(dict_s2["weight"], strict=False)
|
||||||
@ -338,7 +338,7 @@ class TTS:
|
|||||||
if self.configs.is_half and str(self.configs.device)!="cpu":
|
if self.configs.is_half and str(self.configs.device)!="cpu":
|
||||||
self.vits_model = self.vits_model.half()
|
self.vits_model = self.vits_model.half()
|
||||||
|
|
||||||
|
|
||||||
def init_t2s_weights(self, weights_path: str):
|
def init_t2s_weights(self, weights_path: str):
|
||||||
print(f"Loading Text2Semantic weights from {weights_path}")
|
print(f"Loading Text2Semantic weights from {weights_path}")
|
||||||
self.configs.t2s_weights_path = weights_path
|
self.configs.t2s_weights_path = weights_path
|
||||||
@ -354,18 +354,18 @@ class TTS:
|
|||||||
self.t2s_model = t2s_model
|
self.t2s_model = t2s_model
|
||||||
if self.configs.is_half and str(self.configs.device)!="cpu":
|
if self.configs.is_half and str(self.configs.device)!="cpu":
|
||||||
self.t2s_model = self.t2s_model.half()
|
self.t2s_model = self.t2s_model.half()
|
||||||
|
|
||||||
def enable_half_precision(self, enable: bool = True, save: bool = True):
|
def enable_half_precision(self, enable: bool = True, save: bool = True):
|
||||||
'''
|
'''
|
||||||
To enable half precision for the TTS model.
|
To enable half precision for the TTS model.
|
||||||
Args:
|
Args:
|
||||||
enable: bool, whether to enable half precision.
|
enable: bool, whether to enable half precision.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if str(self.configs.device) == "cpu" and enable:
|
if str(self.configs.device) == "cpu" and enable:
|
||||||
print("Half precision is not supported on CPU.")
|
print("Half precision is not supported on CPU.")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.configs.is_half = enable
|
self.configs.is_half = enable
|
||||||
self.precision = torch.float16 if enable else torch.float32
|
self.precision = torch.float16 if enable else torch.float32
|
||||||
if save:
|
if save:
|
||||||
@ -388,7 +388,7 @@ class TTS:
|
|||||||
self.bert_model = self.bert_model.float()
|
self.bert_model = self.bert_model.float()
|
||||||
if self.cnhuhbert_model is not None:
|
if self.cnhuhbert_model is not None:
|
||||||
self.cnhuhbert_model = self.cnhuhbert_model.float()
|
self.cnhuhbert_model = self.cnhuhbert_model.float()
|
||||||
|
|
||||||
def set_device(self, device: torch.device, save: bool = True):
|
def set_device(self, device: torch.device, save: bool = True):
|
||||||
'''
|
'''
|
||||||
To set the device for all models.
|
To set the device for all models.
|
||||||
@ -406,10 +406,10 @@ class TTS:
|
|||||||
self.bert_model = self.bert_model.to(device)
|
self.bert_model = self.bert_model.to(device)
|
||||||
if self.cnhuhbert_model is not None:
|
if self.cnhuhbert_model is not None:
|
||||||
self.cnhuhbert_model = self.cnhuhbert_model.to(device)
|
self.cnhuhbert_model = self.cnhuhbert_model.to(device)
|
||||||
|
|
||||||
def set_ref_audio(self, ref_audio_path:str):
|
def set_ref_audio(self, ref_audio_path:str):
|
||||||
'''
|
'''
|
||||||
To set the reference audio for the TTS model,
|
To set the reference audio for the TTS model,
|
||||||
including the prompt_semantic and refer_spepc.
|
including the prompt_semantic and refer_spepc.
|
||||||
Args:
|
Args:
|
||||||
ref_audio_path: str, the path of the reference audio.
|
ref_audio_path: str, the path of the reference audio.
|
||||||
@ -417,9 +417,9 @@ class TTS:
|
|||||||
self._set_prompt_semantic(ref_audio_path)
|
self._set_prompt_semantic(ref_audio_path)
|
||||||
self._set_ref_spec(ref_audio_path)
|
self._set_ref_spec(ref_audio_path)
|
||||||
self._set_ref_audio_path(ref_audio_path)
|
self._set_ref_audio_path(ref_audio_path)
|
||||||
|
|
||||||
def _set_ref_audio_path(self, ref_audio_path):
|
def _set_ref_audio_path(self, ref_audio_path):
|
||||||
self.prompt_cache["ref_audio_path"] = ref_audio_path
|
self.prompt_cache["ref_audio_path"] = ref_audio_path
|
||||||
|
|
||||||
def _set_ref_spec(self, ref_audio_path):
|
def _set_ref_spec(self, ref_audio_path):
|
||||||
spec = self._get_ref_spec(ref_audio_path)
|
spec = self._get_ref_spec(ref_audio_path)
|
||||||
@ -472,10 +472,10 @@ class TTS:
|
|||||||
1, 2
|
1, 2
|
||||||
) # .float()
|
) # .float()
|
||||||
codes = self.vits_model.extract_latent(hubert_feature)
|
codes = self.vits_model.extract_latent(hubert_feature)
|
||||||
|
|
||||||
prompt_semantic = codes[0, 0].to(self.configs.device)
|
prompt_semantic = codes[0, 0].to(self.configs.device)
|
||||||
self.prompt_cache["prompt_semantic"] = prompt_semantic
|
self.prompt_cache["prompt_semantic"] = prompt_semantic
|
||||||
|
|
||||||
def batch_sequences(self, sequences: List[torch.Tensor], axis: int = 0, pad_value: int = 0, max_length:int=None):
|
def batch_sequences(self, sequences: List[torch.Tensor], axis: int = 0, pad_value: int = 0, max_length:int=None):
|
||||||
seq = sequences[0]
|
seq = sequences[0]
|
||||||
ndim = seq.dim()
|
ndim = seq.dim()
|
||||||
@ -496,12 +496,12 @@ class TTS:
|
|||||||
padded_sequences.append(padded_seq)
|
padded_sequences.append(padded_seq)
|
||||||
batch = torch.stack(padded_sequences)
|
batch = torch.stack(padded_sequences)
|
||||||
return batch
|
return batch
|
||||||
|
|
||||||
def to_batch(self, data:list,
|
def to_batch(self, data:list,
|
||||||
prompt_data:dict=None,
|
prompt_data:dict=None,
|
||||||
batch_size:int=5,
|
batch_size:int=5,
|
||||||
threshold:float=0.75,
|
threshold:float=0.75,
|
||||||
split_bucket:bool=True,
|
split_bucket:bool=True,
|
||||||
device:torch.device=torch.device("cpu"),
|
device:torch.device=torch.device("cpu"),
|
||||||
precision:torch.dtype=torch.float32,
|
precision:torch.dtype=torch.float32,
|
||||||
):
|
):
|
||||||
@ -514,8 +514,8 @@ class TTS:
|
|||||||
batch_index_list = []
|
batch_index_list = []
|
||||||
if split_bucket:
|
if split_bucket:
|
||||||
index_and_len_list.sort(key=lambda x: x[1])
|
index_and_len_list.sort(key=lambda x: x[1])
|
||||||
index_and_len_list = np.array(index_and_len_list, dtype=np.int64)
|
index_and_len_list = np.array(index_and_len_list, dtype=np.int64)
|
||||||
|
|
||||||
batch_index_list_len = 0
|
batch_index_list_len = 0
|
||||||
pos = 0
|
pos = 0
|
||||||
while pos <index_and_len_list.shape[0]:
|
while pos <index_and_len_list.shape[0]:
|
||||||
@ -531,16 +531,16 @@ class TTS:
|
|||||||
pos = pos_end
|
pos = pos_end
|
||||||
break
|
break
|
||||||
pos_end=pos_end-1
|
pos_end=pos_end-1
|
||||||
|
|
||||||
assert batch_index_list_len == len(data)
|
assert batch_index_list_len == len(data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
for i in range(len(data)):
|
for i in range(len(data)):
|
||||||
if i%batch_size == 0:
|
if i%batch_size == 0:
|
||||||
batch_index_list.append([])
|
batch_index_list.append([])
|
||||||
batch_index_list[-1].append(i)
|
batch_index_list[-1].append(i)
|
||||||
|
|
||||||
|
|
||||||
for batch_idx, index_list in enumerate(batch_index_list):
|
for batch_idx, index_list in enumerate(batch_index_list):
|
||||||
item_list = [data[idx] for idx in index_list]
|
item_list = [data[idx] for idx in index_list]
|
||||||
phones_list = []
|
phones_list = []
|
||||||
@ -568,19 +568,19 @@ class TTS:
|
|||||||
|
|
||||||
all_bert_max_len = max(all_bert_max_len, all_bert_features.shape[-1])
|
all_bert_max_len = max(all_bert_max_len, all_bert_features.shape[-1])
|
||||||
all_phones_max_len = max(all_phones_max_len, all_phones.shape[-1])
|
all_phones_max_len = max(all_phones_max_len, all_phones.shape[-1])
|
||||||
|
|
||||||
phones_list.append(phones)
|
phones_list.append(phones)
|
||||||
phones_len_list.append(phones.shape[-1])
|
phones_len_list.append(phones.shape[-1])
|
||||||
all_phones_list.append(all_phones)
|
all_phones_list.append(all_phones)
|
||||||
all_phones_len_list.append(all_phones.shape[-1])
|
all_phones_len_list.append(all_phones.shape[-1])
|
||||||
all_bert_features_list.append(all_bert_features)
|
all_bert_features_list.append(all_bert_features)
|
||||||
norm_text_batch.append(item["norm_text"])
|
norm_text_batch.append(item["norm_text"])
|
||||||
|
|
||||||
phones_batch = phones_list
|
phones_batch = phones_list
|
||||||
all_phones_batch = all_phones_list
|
all_phones_batch = all_phones_list
|
||||||
all_bert_features_batch = all_bert_features_list
|
all_bert_features_batch = all_bert_features_list
|
||||||
|
|
||||||
|
|
||||||
max_len = max(all_bert_max_len, all_phones_max_len)
|
max_len = max(all_bert_max_len, all_phones_max_len)
|
||||||
# phones_batch = self.batch_sequences(phones_list, axis=0, pad_value=0, max_length=max_len)
|
# phones_batch = self.batch_sequences(phones_list, axis=0, pad_value=0, max_length=max_len)
|
||||||
#### 直接对phones和bert_features进行pad。(padding策略会影响T2S模型生成的结果,但不直接影响复读概率。影响复读概率的主要因素是mask的策略)
|
#### 直接对phones和bert_features进行pad。(padding策略会影响T2S模型生成的结果,但不直接影响复读概率。影响复读概率的主要因素是mask的策略)
|
||||||
@ -589,16 +589,16 @@ class TTS:
|
|||||||
# all_bert_features_batch = torch.zeros((len(all_bert_features_list), 1024, max_len), dtype=precision, device=device)
|
# all_bert_features_batch = torch.zeros((len(all_bert_features_list), 1024, max_len), dtype=precision, device=device)
|
||||||
# for idx, item in enumerate(all_bert_features_list):
|
# for idx, item in enumerate(all_bert_features_list):
|
||||||
# all_bert_features_batch[idx, :, : item.shape[-1]] = item
|
# all_bert_features_batch[idx, :, : item.shape[-1]] = item
|
||||||
|
|
||||||
# #### 先对phones进行embedding、对bert_features进行project,再pad到相同长度,(padding策略会影响T2S模型生成的结果,但不直接影响复读概率。影响复读概率的主要因素是mask的策略)
|
# #### 先对phones进行embedding、对bert_features进行project,再pad到相同长度,(padding策略会影响T2S模型生成的结果,但不直接影响复读概率。影响复读概率的主要因素是mask的策略)
|
||||||
# all_phones_list = [self.t2s_model.model.ar_text_embedding(item.to(self.t2s_model.device)) for item in all_phones_list]
|
# all_phones_list = [self.t2s_model.model.ar_text_embedding(item.to(self.t2s_model.device)) for item in all_phones_list]
|
||||||
# all_phones_list = [F.pad(item,(0,0,0,max_len-item.shape[0]),value=0) for item in all_phones_list]
|
# all_phones_list = [F.pad(item,(0,0,0,max_len-item.shape[0]),value=0) for item in all_phones_list]
|
||||||
# all_phones_batch = torch.stack(all_phones_list, dim=0)
|
# all_phones_batch = torch.stack(all_phones_list, dim=0)
|
||||||
|
|
||||||
# all_bert_features_list = [self.t2s_model.model.bert_proj(item.to(self.t2s_model.device).transpose(0, 1)) for item in all_bert_features_list]
|
# all_bert_features_list = [self.t2s_model.model.bert_proj(item.to(self.t2s_model.device).transpose(0, 1)) for item in all_bert_features_list]
|
||||||
# all_bert_features_list = [F.pad(item,(0,0,0,max_len-item.shape[0]), value=0) for item in all_bert_features_list]
|
# all_bert_features_list = [F.pad(item,(0,0,0,max_len-item.shape[0]), value=0) for item in all_bert_features_list]
|
||||||
# all_bert_features_batch = torch.stack(all_bert_features_list, dim=0)
|
# all_bert_features_batch = torch.stack(all_bert_features_list, dim=0)
|
||||||
|
|
||||||
batch = {
|
batch = {
|
||||||
"phones": phones_batch,
|
"phones": phones_batch,
|
||||||
"phones_len": torch.LongTensor(phones_len_list).to(device),
|
"phones_len": torch.LongTensor(phones_len_list).to(device),
|
||||||
@ -609,17 +609,17 @@ class TTS:
|
|||||||
"max_len": max_len,
|
"max_len": max_len,
|
||||||
}
|
}
|
||||||
_data.append(batch)
|
_data.append(batch)
|
||||||
|
|
||||||
return _data, batch_index_list
|
return _data, batch_index_list
|
||||||
|
|
||||||
def recovery_order(self, data:list, batch_index_list:list)->list:
|
def recovery_order(self, data:list, batch_index_list:list)->list:
|
||||||
'''
|
'''
|
||||||
Recovery the order of the audio according to the batch_index_list.
|
Recovery the order of the audio according to the batch_index_list.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data (List[list(np.ndarray)]): the out of order audio .
|
data (List[list(np.ndarray)]): the out of order audio .
|
||||||
batch_index_list (List[list[int]]): the batch index list.
|
batch_index_list (List[list[int]]): the batch index list.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list (List[np.ndarray]): the data in the original order.
|
list (List[np.ndarray]): the data in the original order.
|
||||||
'''
|
'''
|
||||||
@ -635,14 +635,14 @@ class TTS:
|
|||||||
Stop the inference process.
|
Stop the inference process.
|
||||||
'''
|
'''
|
||||||
self.stop_flag = True
|
self.stop_flag = True
|
||||||
|
|
||||||
@torch.no_grad()
|
@torch.no_grad()
|
||||||
def run(self, inputs:dict):
|
def run(self, inputs:dict):
|
||||||
"""
|
"""
|
||||||
Text to speech inference.
|
Text to speech inference.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
inputs (dict):
|
inputs (dict):
|
||||||
{
|
{
|
||||||
"text": "", # str.(required) text to be synthesized
|
"text": "", # str.(required) text to be synthesized
|
||||||
"text_lang: "", # str.(required) language of the text to be synthesized
|
"text_lang: "", # str.(required) language of the text to be synthesized
|
||||||
@ -734,7 +734,7 @@ class TTS:
|
|||||||
if not os.path.exists(ref_audio_path):
|
if not os.path.exists(ref_audio_path):
|
||||||
raise ValueError(f"{ref_audio_path} not exists")
|
raise ValueError(f"{ref_audio_path} not exists")
|
||||||
self.set_ref_audio(ref_audio_path)
|
self.set_ref_audio(ref_audio_path)
|
||||||
|
|
||||||
aux_ref_audio_paths = aux_ref_audio_paths if aux_ref_audio_paths is not None else []
|
aux_ref_audio_paths = aux_ref_audio_paths if aux_ref_audio_paths is not None else []
|
||||||
paths = set(aux_ref_audio_paths)&set(self.prompt_cache["aux_ref_audio_paths"])
|
paths = set(aux_ref_audio_paths)&set(self.prompt_cache["aux_ref_audio_paths"])
|
||||||
if not (len(list(paths)) == len(aux_ref_audio_paths) == len(self.prompt_cache["aux_ref_audio_paths"])):
|
if not (len(list(paths)) == len(aux_ref_audio_paths) == len(self.prompt_cache["aux_ref_audio_paths"])):
|
||||||
@ -744,10 +744,10 @@ class TTS:
|
|||||||
if path in [None, ""]:
|
if path in [None, ""]:
|
||||||
continue
|
continue
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
print(i18n("音频文件不存在,跳过:{}").format(path))
|
print(i18n("音频文件不存在,跳过:"), path)
|
||||||
continue
|
continue
|
||||||
self.prompt_cache["refer_spec"].append(self._get_ref_spec(path))
|
self.prompt_cache["refer_spec"].append(self._get_ref_spec(path))
|
||||||
|
|
||||||
if not no_prompt_text:
|
if not no_prompt_text:
|
||||||
prompt_text = prompt_text.strip("\n")
|
prompt_text = prompt_text.strip("\n")
|
||||||
if (prompt_text[-1] not in splits): prompt_text += "。" if prompt_lang != "en" else "."
|
if (prompt_text[-1] not in splits): prompt_text += "。" if prompt_lang != "en" else "."
|
||||||
@ -757,7 +757,7 @@ class TTS:
|
|||||||
self.prompt_cache["prompt_lang"] = prompt_lang
|
self.prompt_cache["prompt_lang"] = prompt_lang
|
||||||
phones, bert_features, norm_text = \
|
phones, bert_features, norm_text = \
|
||||||
self.text_preprocessor.segment_and_extract_feature_for_text(
|
self.text_preprocessor.segment_and_extract_feature_for_text(
|
||||||
prompt_text,
|
prompt_text,
|
||||||
prompt_lang,
|
prompt_lang,
|
||||||
self.configs.version)
|
self.configs.version)
|
||||||
self.prompt_cache["phones"] = phones
|
self.prompt_cache["phones"] = phones
|
||||||
@ -778,26 +778,26 @@ class TTS:
|
|||||||
return
|
return
|
||||||
|
|
||||||
batch_index_list:list = None
|
batch_index_list:list = None
|
||||||
data, batch_index_list = self.to_batch(data,
|
data, batch_index_list = self.to_batch(data,
|
||||||
prompt_data=self.prompt_cache if not no_prompt_text else None,
|
prompt_data=self.prompt_cache if not no_prompt_text else None,
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
threshold=batch_threshold,
|
threshold=batch_threshold,
|
||||||
split_bucket=split_bucket,
|
split_bucket=split_bucket,
|
||||||
device=self.configs.device,
|
device=self.configs.device,
|
||||||
precision=self.precision
|
precision=self.precision
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print(i18n("############ 切分文本 ############"))
|
print(f'############ {i18n("切分文本")} ############')
|
||||||
texts = self.text_preprocessor.pre_seg_text(text, text_lang, text_split_method)
|
texts = self.text_preprocessor.pre_seg_text(text, text_lang, text_split_method)
|
||||||
data = []
|
data = []
|
||||||
for i in range(len(texts)):
|
for i in range(len(texts)):
|
||||||
if i%batch_size == 0:
|
if i%batch_size == 0:
|
||||||
data.append([])
|
data.append([])
|
||||||
data[-1].append(texts[i])
|
data[-1].append(texts[i])
|
||||||
|
|
||||||
def make_batch(batch_texts):
|
def make_batch(batch_texts):
|
||||||
batch_data = []
|
batch_data = []
|
||||||
print(i18n("############ 提取文本Bert特征 ############"))
|
print(f'############ {i18n("提取文本Bert特征")} ############')
|
||||||
for text in tqdm(batch_texts):
|
for text in tqdm(batch_texts):
|
||||||
phones, bert_features, norm_text = self.text_preprocessor.segment_and_extract_feature_for_text(text, text_lang, self.configs.version)
|
phones, bert_features, norm_text = self.text_preprocessor.segment_and_extract_feature_for_text(text, text_lang, self.configs.version)
|
||||||
if phones is None:
|
if phones is None:
|
||||||
@ -810,9 +810,9 @@ class TTS:
|
|||||||
batch_data.append(res)
|
batch_data.append(res)
|
||||||
if len(batch_data) == 0:
|
if len(batch_data) == 0:
|
||||||
return None
|
return None
|
||||||
batch, _ = self.to_batch(batch_data,
|
batch, _ = self.to_batch(batch_data,
|
||||||
prompt_data=self.prompt_cache if not no_prompt_text else None,
|
prompt_data=self.prompt_cache if not no_prompt_text else None,
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
threshold=batch_threshold,
|
threshold=batch_threshold,
|
||||||
split_bucket=False,
|
split_bucket=False,
|
||||||
device=self.configs.device,
|
device=self.configs.device,
|
||||||
@ -868,10 +868,10 @@ class TTS:
|
|||||||
t_34 += t4 - t3
|
t_34 += t4 - t3
|
||||||
|
|
||||||
refer_audio_spec:torch.Tensor = [item.to(dtype=self.precision, device=self.configs.device) for item in self.prompt_cache["refer_spec"]]
|
refer_audio_spec:torch.Tensor = [item.to(dtype=self.precision, device=self.configs.device) for item in self.prompt_cache["refer_spec"]]
|
||||||
|
|
||||||
|
|
||||||
batch_audio_fragment = []
|
batch_audio_fragment = []
|
||||||
|
|
||||||
# ## vits并行推理 method 1
|
# ## vits并行推理 method 1
|
||||||
# pred_semantic_list = [item[-idx:] for item, idx in zip(pred_semantic_list, idx_list)]
|
# pred_semantic_list = [item[-idx:] for item, idx in zip(pred_semantic_list, idx_list)]
|
||||||
# pred_semantic_len = torch.LongTensor([item.shape[0] for item in pred_semantic_list]).to(self.configs.device)
|
# pred_semantic_len = torch.LongTensor([item.shape[0] for item in pred_semantic_list]).to(self.configs.device)
|
||||||
@ -914,10 +914,10 @@ class TTS:
|
|||||||
t_45 += t5 - t4
|
t_45 += t5 - t4
|
||||||
if return_fragment:
|
if return_fragment:
|
||||||
print("%.3f\t%.3f\t%.3f\t%.3f" % (t1 - t0, t2 - t1, t4 - t3, t5 - t4))
|
print("%.3f\t%.3f\t%.3f\t%.3f" % (t1 - t0, t2 - t1, t4 - t3, t5 - t4))
|
||||||
yield self.audio_postprocess([batch_audio_fragment],
|
yield self.audio_postprocess([batch_audio_fragment],
|
||||||
self.configs.sampling_rate,
|
self.configs.sampling_rate,
|
||||||
None,
|
None,
|
||||||
speed_factor,
|
speed_factor,
|
||||||
False,
|
False,
|
||||||
fragment_interval
|
fragment_interval
|
||||||
)
|
)
|
||||||
@ -935,10 +935,10 @@ class TTS:
|
|||||||
yield self.configs.sampling_rate, np.zeros(int(self.configs.sampling_rate),
|
yield self.configs.sampling_rate, np.zeros(int(self.configs.sampling_rate),
|
||||||
dtype=np.int16)
|
dtype=np.int16)
|
||||||
return
|
return
|
||||||
yield self.audio_postprocess(audio,
|
yield self.audio_postprocess(audio,
|
||||||
self.configs.sampling_rate,
|
self.configs.sampling_rate,
|
||||||
batch_index_list,
|
batch_index_list,
|
||||||
speed_factor,
|
speed_factor,
|
||||||
split_bucket,
|
split_bucket,
|
||||||
fragment_interval
|
fragment_interval
|
||||||
)
|
)
|
||||||
@ -958,7 +958,7 @@ class TTS:
|
|||||||
raise e
|
raise e
|
||||||
finally:
|
finally:
|
||||||
self.empty_cache()
|
self.empty_cache()
|
||||||
|
|
||||||
def empty_cache(self):
|
def empty_cache(self):
|
||||||
try:
|
try:
|
||||||
gc.collect() # 触发gc的垃圾回收。避免内存一直增长。
|
gc.collect() # 触发gc的垃圾回收。避免内存一直增长。
|
||||||
@ -967,13 +967,13 @@ class TTS:
|
|||||||
elif str(self.configs.device) == "mps":
|
elif str(self.configs.device) == "mps":
|
||||||
torch.mps.empty_cache()
|
torch.mps.empty_cache()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def audio_postprocess(self,
|
def audio_postprocess(self,
|
||||||
audio:List[torch.Tensor],
|
audio:List[torch.Tensor],
|
||||||
sr:int,
|
sr:int,
|
||||||
batch_index_list:list=None,
|
batch_index_list:list=None,
|
||||||
speed_factor:float=1.0,
|
speed_factor:float=1.0,
|
||||||
split_bucket:bool=True,
|
split_bucket:bool=True,
|
||||||
fragment_interval:float=0.3
|
fragment_interval:float=0.3
|
||||||
)->Tuple[int, np.ndarray]:
|
)->Tuple[int, np.ndarray]:
|
||||||
@ -982,36 +982,36 @@ class TTS:
|
|||||||
dtype=self.precision,
|
dtype=self.precision,
|
||||||
device=self.configs.device
|
device=self.configs.device
|
||||||
)
|
)
|
||||||
|
|
||||||
for i, batch in enumerate(audio):
|
for i, batch in enumerate(audio):
|
||||||
for j, audio_fragment in enumerate(batch):
|
for j, audio_fragment in enumerate(batch):
|
||||||
max_audio=torch.abs(audio_fragment).max()#简单防止16bit爆音
|
max_audio=torch.abs(audio_fragment).max()#简单防止16bit爆音
|
||||||
if max_audio>1: audio_fragment/=max_audio
|
if max_audio>1: audio_fragment/=max_audio
|
||||||
audio_fragment:torch.Tensor = torch.cat([audio_fragment, zero_wav], dim=0)
|
audio_fragment:torch.Tensor = torch.cat([audio_fragment, zero_wav], dim=0)
|
||||||
audio[i][j] = audio_fragment.cpu().numpy()
|
audio[i][j] = audio_fragment.cpu().numpy()
|
||||||
|
|
||||||
|
|
||||||
if split_bucket:
|
if split_bucket:
|
||||||
audio = self.recovery_order(audio, batch_index_list)
|
audio = self.recovery_order(audio, batch_index_list)
|
||||||
else:
|
else:
|
||||||
# audio = [item for batch in audio for item in batch]
|
# audio = [item for batch in audio for item in batch]
|
||||||
audio = sum(audio, [])
|
audio = sum(audio, [])
|
||||||
|
|
||||||
|
|
||||||
audio = np.concatenate(audio, 0)
|
audio = np.concatenate(audio, 0)
|
||||||
audio = (audio * 32768).astype(np.int16)
|
audio = (audio * 32768).astype(np.int16)
|
||||||
|
|
||||||
# try:
|
# try:
|
||||||
# if speed_factor != 1.0:
|
# if speed_factor != 1.0:
|
||||||
# audio = speed_change(audio, speed=speed_factor, sr=int(sr))
|
# audio = speed_change(audio, speed=speed_factor, sr=int(sr))
|
||||||
# except Exception as e:
|
# except Exception as e:
|
||||||
# print(f"Failed to change speed of audio: \n{e}")
|
# print(f"Failed to change speed of audio: \n{e}")
|
||||||
|
|
||||||
return sr, audio
|
return sr, audio
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def speed_change(input_audio:np.ndarray, speed:float, sr:int):
|
def speed_change(input_audio:np.ndarray, speed:float, sr:int):
|
||||||
# 将 NumPy 数组转换为原始 PCM 流
|
# 将 NumPy 数组转换为原始 PCM 流
|
||||||
raw_audio = input_audio.astype(np.int16).tobytes()
|
raw_audio = input_audio.astype(np.int16).tobytes()
|
||||||
|
|||||||
@ -49,18 +49,18 @@ def merge_short_text_in_array(texts:str, threshold:int) -> list:
|
|||||||
|
|
||||||
|
|
||||||
class TextPreprocessor:
|
class TextPreprocessor:
|
||||||
def __init__(self, bert_model:AutoModelForMaskedLM,
|
def __init__(self, bert_model:AutoModelForMaskedLM,
|
||||||
tokenizer:AutoTokenizer, device:torch.device):
|
tokenizer:AutoTokenizer, device:torch.device):
|
||||||
self.bert_model = bert_model
|
self.bert_model = bert_model
|
||||||
self.tokenizer = tokenizer
|
self.tokenizer = tokenizer
|
||||||
self.device = device
|
self.device = device
|
||||||
|
|
||||||
def preprocess(self, text:str, lang:str, text_split_method:str, version:str="v2")->List[Dict]:
|
def preprocess(self, text:str, lang:str, text_split_method:str, version:str="v2")->List[Dict]:
|
||||||
print(i18n("############ 切分文本 ############"))
|
print(f'############ {i18n("切分文本")} ############')
|
||||||
text = self.replace_consecutive_punctuation(text)
|
text = self.replace_consecutive_punctuation(text)
|
||||||
texts = self.pre_seg_text(text, lang, text_split_method)
|
texts = self.pre_seg_text(text, lang, text_split_method)
|
||||||
result = []
|
result = []
|
||||||
print(i18n("############ 提取文本Bert特征 ############"))
|
print(f'############ {i18n("提取文本Bert特征")} ############')
|
||||||
for text in tqdm(texts):
|
for text in tqdm(texts):
|
||||||
phones, bert_features, norm_text = self.segment_and_extract_feature_for_text(text, lang, version)
|
phones, bert_features, norm_text = self.segment_and_extract_feature_for_text(text, lang, version)
|
||||||
if phones is None or norm_text=="":
|
if phones is None or norm_text=="":
|
||||||
@ -77,14 +77,14 @@ class TextPreprocessor:
|
|||||||
text = text.strip("\n")
|
text = text.strip("\n")
|
||||||
if len(text) == 0:
|
if len(text) == 0:
|
||||||
return []
|
return []
|
||||||
if (text[0] not in splits and len(get_first(text)) < 4):
|
if (text[0] not in splits and len(get_first(text)) < 4):
|
||||||
text = "。" + text if lang != "en" else "." + text
|
text = "。" + text if lang != "en" else "." + text
|
||||||
print(i18n("实际输入的目标文本:"))
|
print(i18n("实际输入的目标文本:"))
|
||||||
print(text)
|
print(text)
|
||||||
|
|
||||||
seg_method = get_seg_method(text_split_method)
|
seg_method = get_seg_method(text_split_method)
|
||||||
text = seg_method(text)
|
text = seg_method(text)
|
||||||
|
|
||||||
while "\n\n" in text:
|
while "\n\n" in text:
|
||||||
text = text.replace("\n\n", "\n")
|
text = text.replace("\n\n", "\n")
|
||||||
|
|
||||||
@ -93,29 +93,29 @@ class TextPreprocessor:
|
|||||||
_texts = merge_short_text_in_array(_texts, 5)
|
_texts = merge_short_text_in_array(_texts, 5)
|
||||||
texts = []
|
texts = []
|
||||||
|
|
||||||
|
|
||||||
for text in _texts:
|
for text in _texts:
|
||||||
# 解决输入目标文本的空行导致报错的问题
|
# 解决输入目标文本的空行导致报错的问题
|
||||||
if (len(text.strip()) == 0):
|
if (len(text.strip()) == 0):
|
||||||
continue
|
continue
|
||||||
if not re.sub("\W+", "", text):
|
if not re.sub("\W+", "", text):
|
||||||
# 检测一下,如果是纯符号,就跳过。
|
# 检测一下,如果是纯符号,就跳过。
|
||||||
continue
|
continue
|
||||||
if (text[-1] not in splits): text += "。" if lang != "en" else "."
|
if (text[-1] not in splits): text += "。" if lang != "en" else "."
|
||||||
|
|
||||||
# 解决句子过长导致Bert报错的问题
|
# 解决句子过长导致Bert报错的问题
|
||||||
if (len(text) > 510):
|
if (len(text) > 510):
|
||||||
texts.extend(split_big_text(text))
|
texts.extend(split_big_text(text))
|
||||||
else:
|
else:
|
||||||
texts.append(text)
|
texts.append(text)
|
||||||
|
|
||||||
print(i18n("实际输入的目标文本(切句后):"))
|
print(i18n("实际输入的目标文本(切句后):"))
|
||||||
print(texts)
|
print(texts)
|
||||||
return texts
|
return texts
|
||||||
|
|
||||||
def segment_and_extract_feature_for_text(self, text:str, language:str, version:str="v1")->Tuple[list, torch.Tensor, str]:
|
def segment_and_extract_feature_for_text(self, text:str, language:str, version:str="v1")->Tuple[list, torch.Tensor, str]:
|
||||||
return self.get_phones_and_bert(text, language, version)
|
return self.get_phones_and_bert(text, language, version)
|
||||||
|
|
||||||
def get_phones_and_bert(self, text:str, language:str, version:str, final:bool=False):
|
def get_phones_and_bert(self, text:str, language:str, version:str, final:bool=False):
|
||||||
if language in {"en", "all_zh", "all_ja", "all_ko", "all_yue"}:
|
if language in {"en", "all_zh", "all_ja", "all_ko", "all_yue"}:
|
||||||
language = language.replace("all_","")
|
language = language.replace("all_","")
|
||||||
@ -203,7 +203,7 @@ class TextPreprocessor:
|
|||||||
phone_level_feature.append(repeat_feature)
|
phone_level_feature.append(repeat_feature)
|
||||||
phone_level_feature = torch.cat(phone_level_feature, dim=0)
|
phone_level_feature = torch.cat(phone_level_feature, dim=0)
|
||||||
return phone_level_feature.T
|
return phone_level_feature.T
|
||||||
|
|
||||||
def clean_text_inf(self, text:str, language:str, version:str="v2"):
|
def clean_text_inf(self, text:str, language:str, version:str="v2"):
|
||||||
phones, word2ph, norm_text = clean_text(text, language, version)
|
phones, word2ph, norm_text = clean_text(text, language, version)
|
||||||
phones = cleaned_text_to_sequence(phones, version)
|
phones = cleaned_text_to_sequence(phones, version)
|
||||||
@ -232,13 +232,10 @@ class TextPreprocessor:
|
|||||||
else:
|
else:
|
||||||
_text.append(text)
|
_text.append(text)
|
||||||
return _text
|
return _text
|
||||||
|
|
||||||
|
|
||||||
def replace_consecutive_punctuation(self,text):
|
def replace_consecutive_punctuation(self,text):
|
||||||
punctuations = ''.join(re.escape(p) for p in punctuation)
|
punctuations = ''.join(re.escape(p) for p in punctuation)
|
||||||
pattern = f'([{punctuations}])([{punctuations}])+'
|
pattern = f'([{punctuations}])([{punctuations}])+'
|
||||||
result = re.sub(pattern, r'\1', text)
|
result = re.sub(pattern, r'\1', text)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user