uzzam2121
Fix build: bundle code in Space repo
fc7db98
Raw
History Blame Contribute Delete
1.61 kB
"""Lazy-load ComplexityPredictor from repo inference code."""
from __future__ import annotations
import sys
import time
from pathlib import Path
from app.config import MODEL_PATH, REPO_ROOT
_predictor = None
_load_error: str | None = None
def _ensure_inference_path() -> None:
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
def get_predictor():
global _predictor, _load_error
if _predictor is not None:
return _predictor
if _load_error is not None:
raise RuntimeError(_load_error)
_ensure_inference_path()
weights = MODEL_PATH / "model_weights.pt"
if not weights.exists():
_load_error = (
f"Model weights not found at {weights}. "
"Set MODEL_PATH or place deberta_best under overnight_bundle/exported_models/."
)
raise RuntimeError(_load_error)
from predictor import ComplexityPredictor
t0 = time.perf_counter()
_predictor = ComplexityPredictor.load(MODEL_PATH)
print(f"Model loaded from {MODEL_PATH} in {(time.perf_counter() - t0):.1f}s")
return _predictor
def model_status() -> tuple[bool, str | None]:
try:
get_predictor()
return True, None
except Exception as exc:
return False, str(exc)
def predict(sentence: str, target_word: str) -> tuple[dict, float, bool]:
predictor = get_predictor()
t0 = time.perf_counter()
result = predictor.predict(sentence, target_word)
latency_ms = (time.perf_counter() - t0) * 1000
target_in = target_word in sentence
return result.to_dict(), latency_ms, target_in