| """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("/")) |
|
|