File size: 4,578 Bytes
f1ba785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""@module numeric-inference/routes — multipart numeric recognize contract."""
import asyncio

from fastapi import FastAPI, HTTPException, Request, UploadFile
from pydantic import ValidationError

from .api_models import NumericRequest
from .form_fields import optional_flags, optional_lengths
from .model_registry import ModelRegistry
from .service import MAX_IMAGE_BYTES, NumericInferenceService

MAX_BATCH = 32
MAX_BODY_BYTES = 16 * 1024 * 1024
API_VERSION = "numeric-inference.v1"


def register_numeric_routes(app: FastAPI, service: NumericInferenceService, registry: ModelRegistry) -> None:
    """Numeric contract. Mounted by the GPU process as well as the standalone app."""
    from .api import require_api_key

    require_api_key()

    @app.get("/v1/models/numeric")
    def model() -> dict[str, str | bool]:
        return {"ready": registry.ready, "version": registry.version, "status": registry.status, "apiVersion": API_VERSION}

    @app.post("/v1/recognize/numeric")
    async def recognize_numeric(request: Request):
        return await _recognize(request, service, many=False)

    @app.post("/v1/recognize/numeric:batch")
    async def recognize_numeric_batch(request: Request):
        return await _recognize(request, service, many=True)


async def _recognize(request: Request, service: NumericInferenceService, many: bool):
    if request.headers.get("content-type", "").startswith("multipart/form-data"):
        form = await request.form()
        return await asyncio.to_thread(
            _run_files,
            service,
            _files(form.getlist("files")),
            [str(item) for item in form.getlist("requestId")],
            [str(item) for item in form.getlist("semanticClass")],
            [str(item) for item in form.getlist("expectedLength")] or None,
            [str(item) for item in form.getlist("independentAgreement")] or None,
        )
    body = await request.json()
    return await asyncio.to_thread(_run_json, service, body, many)


def _files(values: list[object]) -> list[UploadFile]:
    files = [item for item in values if isinstance(item, UploadFile)]
    if len(files) != len(values):
        raise HTTPException(status_code=422, detail="invalid_numeric_request")
    return files


def _run_files(

    service: NumericInferenceService,

    files: list[UploadFile],

    request_ids: list[str],

    semantic_classes: list[str],

    expected_lengths: list[str] | None,

    agreements: list[str] | None,

) -> list[dict]:
    if not files or len(files) > MAX_BATCH:
        raise HTTPException(status_code=413, detail="numeric_batch_too_large")
    if len(request_ids) != len(files) or len(semantic_classes) != len(files):
        raise HTTPException(status_code=422, detail="invalid_numeric_request")
    try:
        lengths = optional_lengths(expected_lengths, len(files))
        flags = optional_flags(agreements, len(files))
    except ValueError as error:
        raise HTTPException(status_code=422, detail="invalid_numeric_request") from error
    payloads = [
        {
            "requestId": request_ids[index],
            "semanticClass": semantic_classes[index],
            "image": upload.file.read(MAX_IMAGE_BYTES + 1),
            "expectedLength": lengths[index],
            "independentAgreement": flags[index],
        }
        for index, upload in enumerate(files)
    ]
    return _invoke(service, payloads)


def _run_json(service: NumericInferenceService, body: object, many: bool):
    rows = body if many else [body]
    if many != isinstance(body, list) or not isinstance(rows, list):
        raise HTTPException(status_code=422, detail="invalid_numeric_request")
    if len(rows) > MAX_BATCH:
        raise HTTPException(status_code=413, detail="numeric_batch_too_large")
    payloads = []
    for row in rows:
        try:
            payloads.append(NumericRequest.model_validate(row).model_dump())
        except ValidationError:
            raise HTTPException(status_code=422, detail="invalid_numeric_request") from None
    results = _invoke(service, payloads)
    return results if many else results[0]


def _invoke(service: NumericInferenceService, payloads: list[dict]) -> list[dict]:
    try:
        return service.recognize_images(payloads)
    except ValueError as error:
        raise HTTPException(status_code=422, detail=str(error)) from None
    except RuntimeError as error:
        raise HTTPException(status_code=503, detail=str(error)) from None