From 5de4884075a4b40c1b20c95d39397726f31083df Mon Sep 17 00:00:00 2001 From: tsukumi Date: Fri, 8 Mar 2024 07:31:33 +0000 Subject: [PATCH] Fix: import error pyopenjtalk_worker.initialize() has the side effect of starting another process and should not be executed automatically on import. --- bert_gen.py | 3 --- style_bert_vits2/nlp/english/__init__.py | 2 +- style_bert_vits2/nlp/japanese/g2p.py | 5 +++-- .../nlp/japanese/pyopenjtalk_worker/__init__.py | 14 +++++++------- .../nlp/japanese/user_dict/__init__.py | 7 +++++-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/bert_gen.py b/bert_gen.py index 79ea65f..26df64d 100644 --- a/bert_gen.py +++ b/bert_gen.py @@ -10,11 +10,8 @@ from config import config from style_bert_vits2.logging import logger from style_bert_vits2.models import commons from style_bert_vits2.nlp import cleaned_text_to_sequence, extract_bert_feature -from style_bert_vits2.nlp.japanese import pyopenjtalk_worker as pyopenjtalk from style_bert_vits2.utils.stdout_wrapper import SAFE_STDOUT -pyopenjtalk.initialize() - def process_line(x): line, add_blank = x diff --git a/style_bert_vits2/nlp/english/__init__.py b/style_bert_vits2/nlp/english/__init__.py index e5041d1..b2067c3 100644 --- a/style_bert_vits2/nlp/english/__init__.py +++ b/style_bert_vits2/nlp/english/__init__.py @@ -273,7 +273,7 @@ def __cache_dict(g2p_dict: dict[str, list[list[str]]], file_path: Path) -> None: def __get_dict() -> dict[str, list[list[str]]]: - if os.path.exists(CACHE_PATH): + if CACHE_PATH.exists(): with open(CACHE_PATH, "rb") as pickle_file: g2p_dict = pickle.load(pickle_file) else: diff --git a/style_bert_vits2/nlp/japanese/g2p.py b/style_bert_vits2/nlp/japanese/g2p.py index 45e7817..0c44563 100644 --- a/style_bert_vits2/nlp/japanese/g2p.py +++ b/style_bert_vits2/nlp/japanese/g2p.py @@ -8,8 +8,6 @@ from style_bert_vits2.nlp.japanese.mora_list import MORA_KATA_TO_MORA_PHONEMES from style_bert_vits2.nlp.japanese.normalizer import replace_punctuation from style_bert_vits2.nlp.symbols import PUNCTUATIONS -pyopenjtalk.initialize() - def g2p( norm_text: str, @@ -114,6 +112,9 @@ def text_to_sep_kata( tuple[list[str], list[str]]: 分割された単語リストと、その読み(カタカナ or 記号1文字)のリスト """ + # pyopenjtalk_worker を初期化 + pyopenjtalk.initialize() + # parsed: OpenJTalkの解析結果 parsed = pyopenjtalk.run_frontend(norm_text) sep_text: list[str] = [] diff --git a/style_bert_vits2/nlp/japanese/pyopenjtalk_worker/__init__.py b/style_bert_vits2/nlp/japanese/pyopenjtalk_worker/__init__.py index fc8f6da..90419f5 100644 --- a/style_bert_vits2/nlp/japanese/pyopenjtalk_worker/__init__.py +++ b/style_bert_vits2/nlp/japanese/pyopenjtalk_worker/__init__.py @@ -50,11 +50,11 @@ def unset_user_dict(): def initialize(port: int = WORKER_PORT) -> None: - import time - import socket - import sys import atexit import signal + import socket + import sys + import time logger.debug("initialize") global WORKER_CLIENT @@ -83,7 +83,7 @@ def initialize(port: int = WORKER_PORT) -> None: else: # align with Windows behavior # start_new_session is same as specifying setsid in preexec_fn - subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) # type: ignore + subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) # wait until server listening count = 0 @@ -92,10 +92,10 @@ def initialize(port: int = WORKER_PORT) -> None: client = WorkerClient(port) break except socket.error: - time.sleep(1) + time.sleep(0.5) count += 1 - # 10: max number of retries - if count == 10: + # 20: max number of retries + if count == 20: raise TimeoutError("サーバーに接続できませんでした") WORKER_CLIENT = client diff --git a/style_bert_vits2/nlp/japanese/user_dict/__init__.py b/style_bert_vits2/nlp/japanese/user_dict/__init__.py index 097cc6a..3032c01 100644 --- a/style_bert_vits2/nlp/japanese/user_dict/__init__.py +++ b/style_bert_vits2/nlp/japanese/user_dict/__init__.py @@ -20,8 +20,6 @@ from style_bert_vits2.nlp.japanese import pyopenjtalk_worker as pyopenjtalk from style_bert_vits2.nlp.japanese.user_dict.word_model import UserDictWord, WordTypes from style_bert_vits2.nlp.japanese.user_dict.part_of_speech_data import MAX_PRIORITY, MIN_PRIORITY, part_of_speech_data -pyopenjtalk.initialize() - # root_dir = engine_root() # save_dir = get_save_dir() @@ -81,6 +79,11 @@ def update_dict( compiled_dict_path : Path コンパイル済み辞書ファイルのパス """ + + # pyopenjtalk_worker を初期化 + # ファイルを開く前に実行する必要がある + pyopenjtalk.initialize() + random_string = uuid4() tmp_csv_path = compiled_dict_path.with_suffix( f".dict_csv-{random_string}.tmp"