Spaces:
Runtime error
Runtime error
File size: 1,613 Bytes
fc7db98 | 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 | """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
|