Spaces:
Configuration error
Configuration error
Download axis_numeric/numeric_routes.py from serety95/axis-numeric: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/spaces/serety95/axis-numeric/resolve/main/axis_numeric/numeric_routes.py
- Command line
-
hf download hf://spaces/serety95/axis-numeric/axis_numeric/numeric_routes.py
-
curl -L -o numeric_routes.py https://huggingface.co/spaces/serety95/axis-numeric/resolve/main/axis_numeric/numeric_routes.py
4.58 kB
| """@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() | |
| def model() -> dict[str, str | bool]: | |
| return {"ready": registry.ready, "version": registry.version, "status": registry.status, "apiVersion": API_VERSION} | |
| async def recognize_numeric(request: Request): | |
| return await _recognize(request, service, many=False) | |
| 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 | |