File size: 4,103 Bytes
f1ba785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Validated, non-sensitive startup loading for the promoted numeric model."""
import hashlib
import json
import math
import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import Literal

from .recognizer import NumericRecognizer
from .reliability import ReliabilityTable

LoadStatus = Literal[
    "not_configured", "model_missing", "metadata_missing", "hash_mismatch",
    "calibration_missing", "calibration_mismatch", "load_failed", "ready",
]


@dataclass(frozen=True)
class RecognizerLoadResult:
    recognizer: NumericRecognizer | None
    status: LoadStatus
    reliability: ReliabilityTable = field(default_factory=ReliabilityTable.empty)


def _sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as source:
        for chunk in iter(lambda: source.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def _load_calibration(model_path: Path, model_sha256: str) -> tuple[float | None, ReliabilityTable, LoadStatus]:
    configured = os.getenv("AXIS_NUMERIC_CALIBRATION_PATH", "").strip()
    path = Path(configured) if configured else model_path.with_suffix(".calibration.json")
    try:
        if not path.is_file():
            return None, ReliabilityTable.empty(), "calibration_missing"
        artifact = json.loads(path.read_text(encoding="utf-8"))
        temperature = float(artifact["temperature"])
    except (OSError, ValueError, TypeError, KeyError, json.JSONDecodeError):
        return None, ReliabilityTable.empty(), "calibration_missing"
    if artifact.get("modelSha256") != model_sha256 or not math.isfinite(temperature) or temperature <= 0:
        return None, ReliabilityTable.empty(), "calibration_mismatch"
    return temperature, ReliabilityTable.from_artifact(artifact.get("reliability")), "ready"


def _metadata(path: Path) -> tuple[str, LoadStatus]:
    metadata_path = path.with_suffix(".meta.json")
    if not metadata_path.is_file():
        return "", "metadata_missing"
    try:
        metadata = json.loads(metadata_path.read_text(encoding="utf-8"))
        model_sha256 = metadata["sha256"]
        vocabulary, blank_index = metadata["vocabulary"], metadata["blankIndex"]
    except (OSError, KeyError, TypeError, json.JSONDecodeError):
        return "", "metadata_missing"
    valid = (
        isinstance(vocabulary, str) and bool(vocabulary)
        and isinstance(blank_index, int) and not isinstance(blank_index, bool)
        and blank_index == len(vocabulary) and isinstance(model_sha256, str)
    )
    return (model_sha256, "ready") if valid else ("", "metadata_missing")


def load_recognizer_result_from_env() -> RecognizerLoadResult:
    configured = os.getenv("AXIS_NUMERIC_MODEL_PATH", "").strip()
    if not configured:
        return RecognizerLoadResult(None, "not_configured")
    path = Path(configured)
    if not path.is_file():
        return RecognizerLoadResult(None, "model_missing")
    model_sha256, status = _metadata(path)
    if status != "ready":
        return RecognizerLoadResult(None, status)
    try:
        matches_hash = _sha256(path) == model_sha256
    except OSError:
        try:
            status = "model_missing" if not path.is_file() else "load_failed"
        except OSError:
            status = "load_failed"
        return RecognizerLoadResult(None, status)
    if not matches_hash:
        return RecognizerLoadResult(None, "hash_mismatch")
    temperature, reliability, status = _load_calibration(path, model_sha256)
    if temperature is None:
        return RecognizerLoadResult(None, status)
    try:
        from .onnx_recognizer import OnnxNumericRecognizer
        return RecognizerLoadResult(OnnxNumericRecognizer(path, temperature), "ready", reliability)
    except Exception:
        return RecognizerLoadResult(None, "load_failed")


def load_recognizer_from_env() -> RecognizerLoadResult:
    """Compatibility entry point for process startup."""
    return load_recognizer_result_from_env()