File size: 1,883 Bytes
5e8bfe6 | 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 | """Hugging Face Inference Endpoints entry point.
The platform imports `EndpointHandler` from this file, constructs it once with the repository path, and calls it per
request. Everything the model needs is in the repository; nothing is fetched at request time.
Request: {"inputs": [[...132 floats...], ...]} a batch of pre-extracted features
{"inputs": {"features": [[...]]}} equivalent
{"inputs": "info"} what this checkpoint is
Response: [{"predicted": ..., "family": ..., "family_probability": ..., "probabilities": {...}, "alert": ...}, ...]
"""
from __future__ import annotations
from inference import Detector
class EndpointHandler:
"""Loads the detector once, scores each request.
Input: the repository path the platform passes in
Output: callable handler
"""
def __init__(self, path: str = ""):
self.detector = Detector(path or ".")
def __call__(self, data: dict) -> list[dict]:
"""Score one request.
Input: the platform's payload dict
Output: one result per event, or a single-item list carrying the model description
Errors are returned rather than raised, so a malformed request produces a readable message instead of a 500
with no explanation.
"""
payload = data.get("inputs", data)
if payload == "info" or (isinstance(payload, dict) and payload.get("info")):
return [self.detector.info()]
if isinstance(payload, dict):
payload = payload.get("features", payload.get("inputs"))
if payload is None:
return [{"error": "send {'inputs': [[...132 floats...]]} or {'inputs': 'info'}"}]
try:
return self.detector.decide(payload)
except (ValueError, TypeError) as error:
return [{"error": str(error)}]
|