File size: 1,352 Bytes
b04d205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""A minimal `POST /v1/systemone` server for DecidaBERT-large-v2.

    pip install torch transformers safetensors fastapi uvicorn huggingface_hub
    PORT=8000 python decision_server.py

POST /v1/systemone        {"state": ..., "questions": {qid: {"type", "instructions", "criteria"}}}
POST /v1/systemone/batch  {"requests": [ {"state", "questions"}, ... ]}
"""
import os
import time

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

from decidabert_v2 import load

MODEL = load(os.environ.get("MODEL_PATH", os.path.dirname(os.path.abspath(__file__))))
app = FastAPI(title="DecidaBERT-large-v2")


class Request(BaseModel):
    state: object
    questions: dict
    model: str | None = None


class Batch(BaseModel):
    requests: list[Request]
    model: str | None = None


def _answer(r: Request) -> dict:
    t0 = time.perf_counter()
    answers = MODEL.predict(r.state, r.questions)
    return {"model": "DecidaBERT-large-v2", "answers": answers, "latency_ms": round((time.perf_counter() - t0) * 1000, 1)}


@app.post("/v1/systemone")
def systemone(r: Request) -> dict:
    return _answer(r)


@app.post("/v1/systemone/batch")
def batch(b: Batch) -> dict:
    return {"responses": [_answer(r) for r in b.requests]}


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))