From e94733678955430f4e0c8ee5a26627077c0ffad9 Mon Sep 17 00:00:00 2001 From: 34j <55338215+34j@users.noreply.github.com> Date: Sun, 30 Apr 2023 18:08:39 +0900 Subject: [PATCH] fix: specify encoding to utf-8 in read_text() and write_text() (#524) --- src/so_vits_svc_fork/dataset.py | 2 +- src/so_vits_svc_fork/gui.py | 6 ++-- .../preprocessing/preprocess_flist_config.py | 28 ++++++++----------- src/so_vits_svc_fork/utils.py | 2 +- tests/test_main.py | 4 +-- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/so_vits_svc_fork/dataset.py b/src/so_vits_svc_fork/dataset.py index 7aed7482..f28f57bf 100644 --- a/src/so_vits_svc_fork/dataset.py +++ b/src/so_vits_svc_fork/dataset.py @@ -19,7 +19,7 @@ def __init__(self, hps: HParams, is_validation: bool = False): for x in Path( hps.data.validation_files if is_validation else hps.data.training_files ) - .read_text() + .read_text("utf-8") .splitlines() ] self.hps = hps diff --git a/src/so_vits_svc_fork/gui.py b/src/so_vits_svc_fork/gui.py index 5bec8673..113840ee 100644 --- a/src/so_vits_svc_fork/gui.py +++ b/src/so_vits_svc_fork/gui.py @@ -29,9 +29,11 @@ def play_audio(path: Path | str): def load_presets() -> dict: - defaults = json.loads(GUI_DEFAULT_PRESETS_PATH.read_text()) + defaults = json.loads(GUI_DEFAULT_PRESETS_PATH.read_text("utf-8")) users = ( - json.loads(GUI_PRESETS_PATH.read_text()) if GUI_PRESETS_PATH.exists() else {} + json.loads(GUI_PRESETS_PATH.read_text("utf-8")) + if GUI_PRESETS_PATH.exists() + else {} ) # prioriy: defaults > users # order: defaults -> users diff --git a/src/so_vits_svc_fork/preprocessing/preprocess_flist_config.py b/src/so_vits_svc_fork/preprocessing/preprocess_flist_config.py index e6654709..01f92130 100644 --- a/src/so_vits_svc_fork/preprocessing/preprocess_flist_config.py +++ b/src/so_vits_svc_fork/preprocessing/preprocess_flist_config.py @@ -52,32 +52,28 @@ def preprocess_config( LOG.info(f"Writing {train_list_path}") train_list_path.parent.mkdir(parents=True, exist_ok=True) - with train_list_path.open("w", encoding="utf-8") as f: - for fname in train: - wavpath = fname.as_posix() - f.write(wavpath + "\n") + train_list_path.write_text( + "\n".join([x.as_posix() for x in train]), encoding="utf-8" + ) LOG.info(f"Writing {val_list_path}") val_list_path.parent.mkdir(parents=True, exist_ok=True) - with val_list_path.open("w", encoding="utf-8") as f: - for fname in val: - wavpath = fname.as_posix() - f.write(wavpath + "\n") + val_list_path.write_text("\n".join([x.as_posix() for x in val]), encoding="utf-8") LOG.info(f"Writing {test_list_path}") test_list_path.parent.mkdir(parents=True, exist_ok=True) - with test_list_path.open("w", encoding="utf-8") as f: - for fname in test: - wavpath = fname.as_posix() - f.write(wavpath + "\n") + test_list_path.write_text("\n".join([x.as_posix() for x in test]), encoding="utf-8") config = deepcopy( json.loads( ( - CONFIG_TEMPLATE_DIR / f"{config_name}.json" - if not config_name.endswith(".json") - else config_name - ).read_text() + CONFIG_TEMPLATE_DIR + / ( + config_name + if config_name.endswith(".json") + else config_name + ".json" + ) + ).read_text(encoding="utf-8") ) ) config["spk"] = spk_dict diff --git a/src/so_vits_svc_fork/utils.py b/src/so_vits_svc_fork/utils.py index 49a2e554..d1474098 100644 --- a/src/so_vits_svc_fork/utils.py +++ b/src/so_vits_svc_fork/utils.py @@ -377,7 +377,7 @@ def get_backup_hparams( def get_hparams(config_path: Path | str) -> HParams: - config = json.loads(Path(config_path).read_text()) + config = json.loads(Path(config_path).read_text("utf-8")) hparams = HParams(**config) return hparams diff --git a/tests/test_main.py b/tests/test_main.py index 36985dbf..380f3793 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -63,7 +63,7 @@ def test_train(self): from so_vits_svc_fork.train import train config_path = Path("tests/logs/44k/config.json") - config_json = json.loads(config_path.read_text()) + config_json = json.loads(config_path.read_text("utf-8")) config_json["train"]["epochs"] = 1 - config_path.write_text(json.dumps(config_json)) + config_path.write_text(json.dumps(config_json), "utf-8") train(config_path, "tests/logs/44k")