From 4f11b011fdca5a860ba83f9bdc4062b7667780fd Mon Sep 17 00:00:00 2001 From: tsukumi Date: Thu, 7 Mar 2024 03:54:02 +0000 Subject: [PATCH] Refactor: minor adjustments --- .../text_processing/chinese/bert_feature.py | 2 +- .../text_processing/english/bert_feature.py | 2 +- .../text_processing/japanese/bert_feature.py | 2 +- style_bert_vits2/text_processing/japanese/g2p.py | 10 +++++----- .../text_processing/japanese/g2p_utils.py | 2 +- style_bert_vits2/text_processing/symbols.py | 14 ++++++++------ style_bert_vits2/utils/subprocess.py | 4 +--- utils.py | 15 --------------- 8 files changed, 18 insertions(+), 33 deletions(-) diff --git a/style_bert_vits2/text_processing/chinese/bert_feature.py b/style_bert_vits2/text_processing/chinese/bert_feature.py index c2085a4..25024cb 100644 --- a/style_bert_vits2/text_processing/chinese/bert_feature.py +++ b/style_bert_vits2/text_processing/chinese/bert_feature.py @@ -7,7 +7,7 @@ from style_bert_vits2.constants import Languages from style_bert_vits2.text_processing import bert_models -models: dict[str, PreTrainedModel] = {} +models: dict[torch.device | str, PreTrainedModel] = {} def extract_bert_feature( diff --git a/style_bert_vits2/text_processing/english/bert_feature.py b/style_bert_vits2/text_processing/english/bert_feature.py index d9c1025..ec556c2 100644 --- a/style_bert_vits2/text_processing/english/bert_feature.py +++ b/style_bert_vits2/text_processing/english/bert_feature.py @@ -7,7 +7,7 @@ from style_bert_vits2.constants import Languages from style_bert_vits2.text_processing import bert_models -models: dict[str, PreTrainedModel] = {} +models: dict[torch.device | str, PreTrainedModel] = {} def extract_bert_feature( diff --git a/style_bert_vits2/text_processing/japanese/bert_feature.py b/style_bert_vits2/text_processing/japanese/bert_feature.py index 078bb5c..3ff9d7b 100644 --- a/style_bert_vits2/text_processing/japanese/bert_feature.py +++ b/style_bert_vits2/text_processing/japanese/bert_feature.py @@ -8,7 +8,7 @@ from style_bert_vits2.text_processing import bert_models from style_bert_vits2.text_processing.japanese.g2p import text_to_sep_kata -models: dict[str, PreTrainedModel] = {} +models: dict[torch.device | str, PreTrainedModel] = {} def extract_bert_feature( diff --git a/style_bert_vits2/text_processing/japanese/g2p.py b/style_bert_vits2/text_processing/japanese/g2p.py index 8ef4d3e..7968751 100644 --- a/style_bert_vits2/text_processing/japanese/g2p.py +++ b/style_bert_vits2/text_processing/japanese/g2p.py @@ -391,7 +391,7 @@ def __kata_to_phoneme_list(text: str) -> list[str]: if set(text).issubset(set(PUNCTUATIONS)): return list(text) - # `text`がカタカナ(`ー`含む)のみからなるかどうかをチェック + # `text` がカタカナ(`ー`含む)のみからなるかどうかをチェック if re.fullmatch(r"[\u30A0-\u30FF]+", text) is None: raise ValueError(f"Input must be katakana only: {text}") sorted_keys = sorted(MORA_KATA_TO_MORA_PHONEMES.keys(), key=len, reverse=True) @@ -438,15 +438,15 @@ def __align_tones( tone_index = 0 for phone in phones_with_punct: if tone_index >= len(phone_tone_list): - # 余ったpunctuationがある場合 → (punctuation, 0)を追加 + # 余った punctuation がある場合 → (punctuation, 0) を追加 result.append((phone, 0)) elif phone == phone_tone_list[tone_index][0]: - # phone_tone_listの現在の音素と一致する場合 → toneをそこから取得、(phone, tone)を追加 + # phone_tone_list の現在の音素と一致する場合 → tone をそこから取得、(phone, tone) を追加 result.append((phone, phone_tone_list[tone_index][1])) - # 探すindexを1つ進める + # 探す index を1つ進める tone_index += 1 elif phone in PUNCTUATIONS: - # phoneがpunctuationの場合 → (phone, 0)を追加 + # phone が punctuation の場合 → (phone, 0) を追加 result.append((phone, 0)) else: logger.debug(f"phones: {phones_with_punct}") diff --git a/style_bert_vits2/text_processing/japanese/g2p_utils.py b/style_bert_vits2/text_processing/japanese/g2p_utils.py index 3a91a00..4ea56e8 100644 --- a/style_bert_vits2/text_processing/japanese/g2p_utils.py +++ b/style_bert_vits2/text_processing/japanese/g2p_utils.py @@ -9,7 +9,7 @@ from style_bert_vits2.text_processing.symbols import PUNCTUATIONS def g2kata_tone(norm_text: str) -> list[tuple[str, int]]: """ テキストからカタカナとアクセントのペアのリストを返す。 - 推論時のみに使われるので、常に `raise_yomi_error=False` で g2p を呼ぶ。 + 推論時のみに使われる関数のため、常に `raise_yomi_error=False` を指定して g2p() を呼ぶ仕様になっている。 Args: norm_text: 正規化されたテキスト。 diff --git a/style_bert_vits2/text_processing/symbols.py b/style_bert_vits2/text_processing/symbols.py index d69bc1c..e3a650f 100644 --- a/style_bert_vits2/text_processing/symbols.py +++ b/style_bert_vits2/text_processing/symbols.py @@ -77,8 +77,8 @@ ZH_SYMBOLS = [ ] NUM_ZH_TONES = 6 -# japanese -JA_SYMBOLS = [ +# Japanese +JP_SYMBOLS = [ "N", "a", "a:", @@ -122,7 +122,7 @@ JA_SYMBOLS = [ "z", "zy", ] -NUM_JA_TONES = 2 +NUM_JP_TONES = 2 # English EN_SYMBOLS = [ @@ -169,23 +169,25 @@ EN_SYMBOLS = [ NUM_EN_TONES = 4 # Combine all symbols -NORMAL_SYMBOLS = sorted(set(ZH_SYMBOLS + JA_SYMBOLS + EN_SYMBOLS)) +NORMAL_SYMBOLS = sorted(set(ZH_SYMBOLS + JP_SYMBOLS + EN_SYMBOLS)) SYMBOLS = [PAD] + NORMAL_SYMBOLS + PUNCTUATION_SYMBOLS SIL_PHONEMES_IDS = [SYMBOLS.index(i) for i in PUNCTUATION_SYMBOLS] # Combine all tones -NUM_TONES = NUM_ZH_TONES + NUM_JA_TONES + NUM_EN_TONES +NUM_TONES = NUM_ZH_TONES + NUM_JP_TONES + NUM_EN_TONES # Language maps LANGUAGE_ID_MAP = {"ZH": 0, "JP": 1, "EN": 2} NUM_LANGUAGES = len(LANGUAGE_ID_MAP.keys()) +# Language tone start map LANGUAGE_TONE_START_MAP = { "ZH": 0, "JP": NUM_ZH_TONES, - "EN": NUM_ZH_TONES + NUM_JA_TONES, + "EN": NUM_ZH_TONES + NUM_JP_TONES, } + if __name__ == "__main__": a = set(ZH_SYMBOLS) b = set(EN_SYMBOLS) diff --git a/style_bert_vits2/utils/subprocess.py b/style_bert_vits2/utils/subprocess.py index b152702..5ff267b 100644 --- a/style_bert_vits2/utils/subprocess.py +++ b/style_bert_vits2/utils/subprocess.py @@ -5,8 +5,6 @@ from typing import Any, Callable from style_bert_vits2.logging import logger from style_bert_vits2.utils.stdout_wrapper import SAFE_STDOUT -PYTHON = sys.executable - def run_script_with_log(cmd: list[str], ignore_warning: bool = False) -> tuple[bool, str]: """ @@ -22,7 +20,7 @@ def run_script_with_log(cmd: list[str], ignore_warning: bool = False) -> tuple[b logger.info(f"Running: {' '.join(cmd)}") result = subprocess.run( - [PYTHON] + cmd, + [sys.executable] + cmd, stdout = SAFE_STDOUT, stderr = subprocess.PIPE, text = True, diff --git a/utils.py b/utils.py index 80dfa66..d4ff765 100644 --- a/utils.py +++ b/utils.py @@ -450,21 +450,6 @@ class HParams: return self.__dict__.__repr__() -def load_model(model_path, config_path): - hps = get_hparams_from_file(config_path) - net = SynthesizerTrn( - # len(symbols), - 108, - hps.data.filter_length // 2 + 1, - hps.train.segment_size // hps.data.hop_length, - n_speakers=hps.data.n_speakers, - **hps.model, - ).to("cpu") - _ = net.eval() - _ = load_checkpoint(model_path, net, None, skip_optimizer=True) - return net - - def mix_model( network1, network2, output_path, voice_ratio=(0.5, 0.5), tone_ratio=(0.5, 0.5) ):