更换前端为WeTextProcessing

This commit is contained in:
passerbya
2024-07-09 08:22:06 +08:00
committed by GitHub
parent 2f496104ec
commit 88c8bf7b9e

View File

@@ -21,12 +21,9 @@ import torchaudio.compliance.kaldi as kaldi
import torchaudio import torchaudio
import os import os
import inflect import inflect
try: from tn.chinese.normalizer import Normalizer as ZhNormalizer
import ttsfrd from tn.english.normalizer import Normalizer as EnNormalizer
use_ttsfrd = True
except:
print("failed to import ttsfrd, please normalize input text manually")
use_ttsfrd = False
from cosyvoice.utils.frontend_utils import contains_chinese, replace_blank, replace_corner_mark, remove_bracket, spell_out_number, split_paragraph from cosyvoice.utils.frontend_utils import contains_chinese, replace_blank, replace_corner_mark, remove_bracket, spell_out_number, split_paragraph
@@ -53,14 +50,8 @@ class CosyVoiceFrontEnd:
self.instruct = instruct self.instruct = instruct
self.allowed_special = allowed_special self.allowed_special = allowed_special
self.inflect_parser = inflect.engine() self.inflect_parser = inflect.engine()
self.use_ttsfrd = use_ttsfrd self.zh_tn_model = ZhNormalizer(remove_erhua=False,full_to_half=False)
if self.use_ttsfrd: self.en_tn_model = EnNormalizer()
self.frd = ttsfrd.TtsFrontendEngine()
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
assert self.frd.initialize('{}/../../pretrained_models/CosyVoice-ttsfrd/resource'.format(ROOT_DIR)) is True, 'failed to initialize ttsfrd resource'
self.frd.set_lang_type('pinyin')
self.frd.enable_pinyin_mix(True)
self.frd.set_breakmodel_index(1)
def _extract_text_token(self, text): def _extract_text_token(self, text):
text_token = self.tokenizer.encode(text, allowed_special=self.allowed_special) text_token = self.tokenizer.encode(text, allowed_special=self.allowed_special)
@@ -95,8 +86,7 @@ class CosyVoiceFrontEnd:
def text_normalize(self, text, split=True): def text_normalize(self, text, split=True):
text = text.strip() text = text.strip()
if contains_chinese(text): if contains_chinese(text):
if self.use_ttsfrd: text = self.zh_tn_model.normalize(text)
text = self.frd.get_frd_extra_info(text, 'input')
text = text.replace("\n", "") text = text.replace("\n", "")
text = replace_blank(text) text = replace_blank(text)
text = replace_corner_mark(text) text = replace_corner_mark(text)
@@ -107,6 +97,7 @@ class CosyVoiceFrontEnd:
token_min_n=60, merge_len=20, token_min_n=60, merge_len=20,
comma_split=False)] comma_split=False)]
else: else:
text = self.en_tn_model.normalize(text)
text = spell_out_number(text, self.inflect_parser) text = spell_out_number(text, self.inflect_parser)
texts = [i for i in split_paragraph(text, partial(self.tokenizer.encode, allowed_special=self.allowed_special), "en", token_max_n=80, texts = [i for i in split_paragraph(text, partial(self.tokenizer.encode, allowed_special=self.allowed_special), "en", token_max_n=80,
token_min_n=60, merge_len=20, token_min_n=60, merge_len=20,