Download handler.py from shalev396/purchase-propensity: direct link, hf CLI and curl.
- Browser
- Download file 955 Bytes
-
https://huggingface.co/shalev396/purchase-propensity/resolve/main/handler.py
- Command line
-
hf download hf://shalev396/purchase-propensity/handler.py
-
curl -L -o handler.py https://huggingface.co/shalev396/purchase-propensity/resolve/main/handler.py
955 Bytes
| """Hugging Face Inference Endpoints entry point — deploy this repo as a CPU/GPU API. | |
| Request body: | |
| {"inputs": ["basket_add_detail", "saw_checkout"]} # list of active flags | |
| {"inputs": {"saw_checkout": 1, "sign_in": 1}, "parameters": {"model": "pytorch_mlp"}} | |
| {"inputs": [{"saw_checkout": 1}, {"saw_homepage": 1}]} # several sessions | |
| Response: {"buy": p, "no_buy": 1 - p} (a list of them for several sessions). | |
| """ | |
| 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) | |