File size: 1,101 Bytes
4968ea3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | """I/O utilities for JSON and YAML file operations."""
import json
import os
from pathlib import Path
from typing import Any
import yaml
def load_json(path: str) -> Any:
"""Load a JSON file."""
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def save_json(data: Any, path: str, indent: int = 2) -> None:
"""Save data to a JSON file, creating parent dirs if needed."""
ensure_dir(os.path.dirname(path))
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=indent)
def ensure_dir(path: str) -> None:
"""Create directory if it doesn't exist."""
if path:
os.makedirs(path, exist_ok=True)
def load_yaml(path: str) -> dict:
"""Load a YAML config file."""
with open(path, "r", encoding="utf-8") as f:
return yaml.safe_load(f)
def get_model_name(base_model_config_path: str) -> str:
"""Extract model name from base model config for directory naming."""
config = load_yaml(base_model_config_path)
return os.path.basename(config["model_name_or_path"].rstrip("/"))
|