Refactor: unify invoke format of open() function
This commit is contained in:
@@ -73,10 +73,10 @@ if __name__ == "__main__":
|
|||||||
config_path = args.config
|
config_path = args.config
|
||||||
hps = HyperParameters.load_from_json(config_path)
|
hps = HyperParameters.load_from_json(config_path)
|
||||||
lines = []
|
lines = []
|
||||||
with open(hps.data.training_files, encoding="utf-8") as f:
|
with open(hps.data.training_files, "r", encoding="utf-8") as f:
|
||||||
lines.extend(f.readlines())
|
lines.extend(f.readlines())
|
||||||
|
|
||||||
with open(hps.data.validation_files, encoding="utf-8") as f:
|
with open(hps.data.validation_files, "r", encoding="utf-8") as f:
|
||||||
lines.extend(f.readlines())
|
lines.extend(f.readlines())
|
||||||
add_blank = [hps.data.add_blank] * len(lines)
|
add_blank = [hps.data.add_blank] * len(lines)
|
||||||
|
|
||||||
|
|||||||
@@ -238,7 +238,7 @@ class Config:
|
|||||||
"If you have no special needs, please do not modify default_config.yml."
|
"If you have no special needs, please do not modify default_config.yml."
|
||||||
)
|
)
|
||||||
# sys.exit(0)
|
# sys.exit(0)
|
||||||
with open(file=config_path, mode="r", encoding="utf-8") as file:
|
with open(config_path, "r", encoding="utf-8") as file:
|
||||||
yaml_config: Dict[str, any] = yaml.safe_load(file.read())
|
yaml_config: Dict[str, any] = yaml.safe_load(file.read())
|
||||||
model_name: str = yaml_config["model_name"]
|
model_name: str = yaml_config["model_name"]
|
||||||
self.model_name: str = model_name
|
self.model_name: str = model_name
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ def load_filepaths_and_text(
|
|||||||
list[list[str]]: ファイルパスとテキストのリスト
|
list[list[str]]: ファイルパスとテキストのリスト
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with open(filename, encoding="utf-8") as f:
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
filepaths_and_text = [line.strip().split(split) for line in f]
|
filepaths_and_text = [line.strip().split(split) for line in f]
|
||||||
return filepaths_and_text
|
return filepaths_and_text
|
||||||
|
|
||||||
@@ -249,7 +249,8 @@ def check_git_hash(model_dir_path: Union[str, Path]) -> None:
|
|||||||
|
|
||||||
path = os.path.join(model_dir_path, "githash")
|
path = os.path.join(model_dir_path, "githash")
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
saved_hash = open(path).read()
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
saved_hash = f.read()
|
||||||
if saved_hash != cur_hash:
|
if saved_hash != cur_hash:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"git hash values are different. {}(saved) != {}(current)".format(
|
"git hash values are different. {}(saved) != {}(current)".format(
|
||||||
@@ -257,4 +258,5 @@ def check_git_hash(model_dir_path: Union[str, Path]) -> None:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
open(path, "w").write(cur_hash)
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
|
f.write(cur_hash)
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ def load_checkpoint(
|
|||||||
else:
|
else:
|
||||||
model.load_state_dict(new_state_dict, strict=False)
|
model.load_state_dict(new_state_dict, strict=False)
|
||||||
|
|
||||||
logger.info("Loaded '{}' (iteration {})".format(checkpoint_path, iteration))
|
logger.info(f"Loaded '{checkpoint_path}' (iteration {iteration})")
|
||||||
|
|
||||||
return model, optimizer, learning_rate, iteration
|
return model, optimizer, learning_rate, iteration
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ def load_model(
|
|||||||
language: Languages,
|
language: Languages,
|
||||||
pretrained_model_name_or_path: Optional[str] = None,
|
pretrained_model_name_or_path: Optional[str] = None,
|
||||||
cache_dir: Optional[str] = None,
|
cache_dir: Optional[str] = None,
|
||||||
revision: str = 'main',
|
revision: str = "main",
|
||||||
) -> Union[PreTrainedModel, DebertaV2Model]:
|
) -> Union[PreTrainedModel, DebertaV2Model]:
|
||||||
"""
|
"""
|
||||||
指定された言語の BERT モデルをロードし、ロード済みの BERT モデルを返す。
|
指定された言語の BERT モデルをロードし、ロード済みの BERT モデルを返す。
|
||||||
@@ -96,7 +96,7 @@ def load_tokenizer(
|
|||||||
language: Languages,
|
language: Languages,
|
||||||
pretrained_model_name_or_path: Optional[str] = None,
|
pretrained_model_name_or_path: Optional[str] = None,
|
||||||
cache_dir: Optional[str] = None,
|
cache_dir: Optional[str] = None,
|
||||||
revision: str = 'main',
|
revision: str = "main",
|
||||||
) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast, DebertaV2Tokenizer]:
|
) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast, DebertaV2Tokenizer]:
|
||||||
"""
|
"""
|
||||||
指定された言語の BERT モデルをロードし、ロード済みの BERT トークナイザーを返す。
|
指定された言語の BERT モデルをロードし、ロード済みの BERT トークナイザーを返す。
|
||||||
|
|||||||
@@ -8,10 +8,11 @@ from style_bert_vits2.nlp.chinese.tone_sandhi import ToneSandhi
|
|||||||
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
||||||
|
|
||||||
|
|
||||||
__PINYIN_TO_SYMBOL_MAP = {
|
with open(Path(__file__).parent / "opencpop-strict.txt", "r", encoding="utf-8") as f:
|
||||||
|
__PINYIN_TO_SYMBOL_MAP = {
|
||||||
line.split("\t")[0]: line.strip().split("\t")[1]
|
line.split("\t")[0]: line.strip().split("\t")[1]
|
||||||
for line in open(Path(__file__).parent / "opencpop-strict.txt").readlines()
|
for line in f.readlines()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def g2p(text: str) -> tuple[list[str], list[int], list[int]]:
|
def g2p(text: str) -> tuple[list[str], list[int], list[int]]:
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ def get_dict() -> dict[str, list[list[str]]]:
|
|||||||
def read_dict() -> dict[str, list[list[str]]]:
|
def read_dict() -> dict[str, list[list[str]]]:
|
||||||
g2p_dict = {}
|
g2p_dict = {}
|
||||||
start_line = 49
|
start_line = 49
|
||||||
with open(CMU_DICT_PATH) as f:
|
with open(CMU_DICT_PATH, "r", encoding="utf-8") as f:
|
||||||
line = f.readline()
|
line = f.readline()
|
||||||
line_index = 1
|
line_index = 1
|
||||||
while line:
|
while line:
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ if __name__ == "__main__":
|
|||||||
device = config.style_gen_config.device
|
device = config.style_gen_config.device
|
||||||
|
|
||||||
training_lines: list[str] = []
|
training_lines: list[str] = []
|
||||||
with open(hps.data.training_files, encoding="utf-8") as f:
|
with open(hps.data.training_files, "r", encoding="utf-8") as f:
|
||||||
training_lines.extend(f.readlines())
|
training_lines.extend(f.readlines())
|
||||||
with ThreadPoolExecutor(max_workers=num_processes) as executor:
|
with ThreadPoolExecutor(max_workers=num_processes) as executor:
|
||||||
training_results = list(
|
training_results = list(
|
||||||
@@ -94,7 +94,7 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
|
|
||||||
val_lines: list[str] = []
|
val_lines: list[str] = []
|
||||||
with open(hps.data.validation_files, encoding="utf-8") as f:
|
with open(hps.data.validation_files, "r", encoding="utf-8") as f:
|
||||||
val_lines.extend(f.readlines())
|
val_lines.extend(f.readlines())
|
||||||
|
|
||||||
with ThreadPoolExecutor(max_workers=num_processes) as executor:
|
with ThreadPoolExecutor(max_workers=num_processes) as executor:
|
||||||
|
|||||||
@@ -47,11 +47,11 @@ def merge_style(model_name_a, model_name_b, weight, output_name, style_triple_li
|
|||||||
os.path.join(assets_root, model_name_b, "style_vectors.npy")
|
os.path.join(assets_root, model_name_b, "style_vectors.npy")
|
||||||
) # (style_num_b, 256)
|
) # (style_num_b, 256)
|
||||||
with open(
|
with open(
|
||||||
os.path.join(assets_root, model_name_a, "config.json"), encoding="utf-8"
|
os.path.join(assets_root, model_name_a, "config.json"), "r", encoding="utf-8"
|
||||||
) as f:
|
) as f:
|
||||||
config_a = json.load(f)
|
config_a = json.load(f)
|
||||||
with open(
|
with open(
|
||||||
os.path.join(assets_root, model_name_b, "config.json"), encoding="utf-8"
|
os.path.join(assets_root, model_name_b, "config.json"), "r", encoding="utf-8"
|
||||||
) as f:
|
) as f:
|
||||||
config_b = json.load(f)
|
config_b = json.load(f)
|
||||||
style2id_a = config_a["data"]["style2id"]
|
style2id_a = config_a["data"]["style2id"]
|
||||||
@@ -88,7 +88,7 @@ def merge_style(model_name_a, model_name_b, weight, output_name, style_triple_li
|
|||||||
# recipe.jsonを読み込んで、style_triple_listを追記
|
# recipe.jsonを読み込んで、style_triple_listを追記
|
||||||
info_path = os.path.join(assets_root, output_name, "recipe.json")
|
info_path = os.path.join(assets_root, output_name, "recipe.json")
|
||||||
if os.path.exists(info_path):
|
if os.path.exists(info_path):
|
||||||
with open(info_path, encoding="utf-8") as f:
|
with open(info_path, "r", encoding="utf-8") as f:
|
||||||
info = json.load(f)
|
info = json.load(f)
|
||||||
else:
|
else:
|
||||||
info = {}
|
info = {}
|
||||||
@@ -261,12 +261,12 @@ def update_two_model_names_dropdown(model_holder: TTSModelHolder):
|
|||||||
|
|
||||||
def load_styles_gr(model_name_a, model_name_b):
|
def load_styles_gr(model_name_a, model_name_b):
|
||||||
config_path_a = os.path.join(assets_root, model_name_a, "config.json")
|
config_path_a = os.path.join(assets_root, model_name_a, "config.json")
|
||||||
with open(config_path_a, encoding="utf-8") as f:
|
with open(config_path_a, "r", encoding="utf-8") as f:
|
||||||
config_a = json.load(f)
|
config_a = json.load(f)
|
||||||
styles_a = list(config_a["data"]["style2id"].keys())
|
styles_a = list(config_a["data"]["style2id"].keys())
|
||||||
|
|
||||||
config_path_b = os.path.join(assets_root, model_name_b, "config.json")
|
config_path_b = os.path.join(assets_root, model_name_b, "config.json")
|
||||||
with open(config_path_b, encoding="utf-8") as f:
|
with open(config_path_b, "r", encoding="utf-8") as f:
|
||||||
config_b = json.load(f)
|
config_b = json.load(f)
|
||||||
styles_b = list(config_b["data"]["style2id"].keys())
|
styles_b = list(config_b["data"]["style2id"].keys())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user