Spaces:
Configuration error
Configuration error
Download axis_numeric/model_loader.py from serety95/axis-numeric: direct link, hf CLI and curl.
- Browser
- Download file 4.1 kB
-
https://huggingface.co/spaces/serety95/axis-numeric/resolve/main/axis_numeric/model_loader.py
- Command line
-
hf download hf://spaces/serety95/axis-numeric/axis_numeric/model_loader.py
-
curl -L -o model_loader.py https://huggingface.co/spaces/serety95/axis-numeric/resolve/main/axis_numeric/model_loader.py
4.1 kB
| """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", | |
| ] | |
| 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() | |