|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from dataclasses import dataclass |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +ARTIFACT_ROOT_ENV_VAR = "AI_MODEL_WEIGHTS_DIR" |
| 9 | +DEFAULT_ARTIFACT_ROOT = Path("AI/data/weights") |
| 10 | +PROJECT_ROOT = Path(__file__).resolve().parents[4] |
| 11 | + |
| 12 | + |
| 13 | +@dataclass(frozen=True, slots=True) |
| 14 | +class ModelArtifactPaths: |
| 15 | + root_dir: str |
| 16 | + model_dir: str |
| 17 | + model_path: str |
| 18 | + scaler_path: str | None = None |
| 19 | + metadata_path: str | None = None |
| 20 | + |
| 21 | + |
| 22 | +def _resolve_absolute(raw_path: str | Path) -> Path: |
| 23 | + path = Path(raw_path) |
| 24 | + if not path.is_absolute(): |
| 25 | + path = PROJECT_ROOT / path |
| 26 | + return path.resolve() |
| 27 | + |
| 28 | + |
| 29 | +def _normalize_mode(raw_mode: str | None) -> str: |
| 30 | + mode = (raw_mode or "prod").strip().lower() |
| 31 | + if mode in {"simulation", "sim", "test", "tests", "dev", "development", "qa"}: |
| 32 | + return "tests" |
| 33 | + if mode in {"live", "production", "prod"}: |
| 34 | + return "prod" |
| 35 | + return mode |
| 36 | + |
| 37 | + |
| 38 | +def resolve_artifact_root(config_weights_dir: str | None = None) -> str: |
| 39 | + env_root = os.getenv(ARTIFACT_ROOT_ENV_VAR) |
| 40 | + selected_root = ( |
| 41 | + env_root.strip() |
| 42 | + if env_root and env_root.strip() |
| 43 | + else (config_weights_dir or str(DEFAULT_ARTIFACT_ROOT)) |
| 44 | + ) |
| 45 | + return str(_resolve_absolute(selected_root)) |
| 46 | + |
| 47 | + |
| 48 | +def resolve_artifact_file(*relative_parts: str, config_weights_dir: str | None = None) -> str: |
| 49 | + if not relative_parts: |
| 50 | + raise ValueError("At least one path part is required.") |
| 51 | + artifact_root = Path(resolve_artifact_root(config_weights_dir)) |
| 52 | + return str((artifact_root.joinpath(*relative_parts)).resolve()) |
| 53 | + |
| 54 | + |
| 55 | +def resolve_model_artifacts( |
| 56 | + model_name: str, |
| 57 | + mode: str | None = None, |
| 58 | + config_weights_dir: str | None = None, |
| 59 | + model_dir: str | None = None, |
| 60 | +) -> ModelArtifactPaths: |
| 61 | + normalized_model = model_name.strip().lower() |
| 62 | + normalized_mode = _normalize_mode(mode) |
| 63 | + root_dir = Path(resolve_artifact_root(config_weights_dir)) |
| 64 | + |
| 65 | + if normalized_model == "transformer": |
| 66 | + suffix = "_prod" |
| 67 | + mode_dir = "prod" |
| 68 | + if normalized_mode == "tests": |
| 69 | + suffix = "_test" |
| 70 | + mode_dir = "tests" |
| 71 | + |
| 72 | + resolved_model_dir = _resolve_absolute(model_dir) if model_dir else (root_dir / "transformer" / mode_dir) |
| 73 | + model_path = resolved_model_dir / f"multi_horizon_model{suffix}.keras" |
| 74 | + scaler_path = resolved_model_dir / f"multi_horizon_scaler{suffix}.pkl" |
| 75 | + return ModelArtifactPaths( |
| 76 | + root_dir=str(root_dir), |
| 77 | + model_dir=str(resolved_model_dir), |
| 78 | + model_path=str(model_path), |
| 79 | + scaler_path=str(scaler_path), |
| 80 | + metadata_path=None, |
| 81 | + ) |
| 82 | + |
| 83 | + if normalized_model in {"itransformer", "i_transformer", "i-transformer"}: |
| 84 | + resolved_model_dir = _resolve_absolute(model_dir) if model_dir else (root_dir / "itransformer") |
| 85 | + return ModelArtifactPaths( |
| 86 | + root_dir=str(root_dir), |
| 87 | + model_dir=str(resolved_model_dir), |
| 88 | + model_path=str(resolved_model_dir / "multi_horizon_model.keras"), |
| 89 | + scaler_path=str(resolved_model_dir / "multi_horizon_scaler.pkl"), |
| 90 | + metadata_path=str(resolved_model_dir / "metadata.json"), |
| 91 | + ) |
| 92 | + |
| 93 | + if normalized_model == "tcn": |
| 94 | + resolved_model_dir = _resolve_absolute(model_dir) if model_dir else (root_dir / "tcn") |
| 95 | + return ModelArtifactPaths( |
| 96 | + root_dir=str(root_dir), |
| 97 | + model_dir=str(resolved_model_dir), |
| 98 | + model_path=str(resolved_model_dir / "model.pt"), |
| 99 | + scaler_path=str(resolved_model_dir / "scaler.pkl"), |
| 100 | + metadata_path=str(resolved_model_dir / "metadata.json"), |
| 101 | + ) |
| 102 | + |
| 103 | + if normalized_model == "patchtst": |
| 104 | + resolved_model_dir = _resolve_absolute(model_dir) if model_dir else (root_dir / "patchtst") |
| 105 | + return ModelArtifactPaths( |
| 106 | + root_dir=str(root_dir), |
| 107 | + model_dir=str(resolved_model_dir), |
| 108 | + model_path=str(resolved_model_dir / "PatchTST_best.pt"), |
| 109 | + scaler_path=None, |
| 110 | + metadata_path=None, |
| 111 | + ) |
| 112 | + |
| 113 | + raise ValueError(f"Unsupported model name for artifact resolution: {model_name}") |
0 commit comments