File size: 883 Bytes
b8e43ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Hugging Face Inference Endpoints entry point — deploy this repo as a CPU/GPU API.

Request: {"inputs": <image>} where <image> is a base64 string (optionally a data: URI) or, when the
endpoint receives raw image bytes (Content-Type: image/*), the PIL image the toolkit decodes.
Optional {"parameters": {"top_k": 5}}. Response: the `Predictor.predict` dict ({"faces": [...], "n_faces": n}).
"""
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import model as M  # noqa: E402


class EndpointHandler:
    def __init__(self, path: str = ""):
        self.predictor = M.load(path or HERE, "cuda" if M.cuda_available() else "cpu")

    def __call__(self, data: dict):
        inputs = data.pop("inputs", data)
        parameters = data.pop("parameters", None) or {}
        return self.predictor.predict(inputs, **parameters)