File size: 1,198 Bytes
ce209f5 | 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 41 42 43 | from __future__ import annotations
import json
from copy import deepcopy
from pathlib import Path
from typing import Any
import yaml
ROOT = Path(__file__).resolve().parents[1]
def load_config(path: str | Path) -> dict[str, Any]:
path = Path(path)
if not path.is_absolute():
path = ROOT / path
with path.open("r", encoding="utf-8") as f:
if path.suffix.lower() == ".json":
return json.load(f)
return yaml.safe_load(f) or {}
def deep_merge(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
merged = deepcopy(base)
for key, value in override.items():
if isinstance(value, dict) and isinstance(merged.get(key), dict):
merged[key] = deep_merge(merged[key], value)
else:
merged[key] = deepcopy(value)
return merged
def load_dataset_config(name_or_path: str) -> dict[str, Any]:
path = Path(name_or_path)
if path.suffix:
return load_config(path)
return load_config(Path("configs/datasets") / f"{name_or_path}.yaml")
def load_model_registry(path: str | Path = "configs/models/registry.yaml") -> dict[str, Any]:
return load_config(path).get("models", {})
|