File size: 948 Bytes
116f4ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hugging Face Inference Endpoints handler for Kodiak (copied into every exported model folder).

Deploy: model page -> Deploy -> Inference Endpoints (CPU is enough; ~80 ms per request on 8 cores). The endpoint installs
`requirements.txt` from the repo, then calls EndpointHandler for each request.

Request body (one request or a list):
    {"inputs": {"state": "...", "questions": [...], "options": {...}}}
    {"inputs": [{"state": ..., "questions": [...]}, ...]}
Response: the Kodiak Response for each request (see the repo's schema/ folder).
"""

from typing import Any

from kodiak_s1.hub import Kodiak


class EndpointHandler:
    def __init__(self, path: str = ""):
        self.kodiak = Kodiak.from_pretrained(path or ".")

    def __call__(self, data: dict[str, Any]) -> list[dict]:
        inputs = data.get("inputs", data)
        requests = inputs if isinstance(inputs, list) else [inputs]
        return self.kodiak.answer(requests)