File size: 5,978 Bytes
174cc12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""HF Docker Space: one warmed-up FP32 BiomedCLIP instance, no inference queue."""
from __future__ import annotations

import base64
import binascii
import hmac
import os
from contextlib import asynccontextmanager
from typing import Union

from fastapi import Depends, FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from pydantic import BaseModel, Field, model_validator
from starlette.concurrency import run_in_threadpool

from engine import BiomedEngine

MAX_IMAGE_BYTES = 6 * 1024 * 1024
MAX_BASE64_CHARS = ((MAX_IMAGE_BYTES + 2) // 3) * 4 + 128


class Region(BaseModel):
    left: float = Field(ge=0, le=1)
    top: float = Field(ge=0, le=1)
    right: float = Field(ge=0, le=1)
    bottom: float = Field(ge=0, le=1)

    @model_validator(mode="after")
    def ordered(self):
        if self.left >= self.right or self.top >= self.bottom:
            raise ValueError("ROI must have positive width and height.")
        return self


class ImageRequest(BaseModel):
    image_base64: str = Field(min_length=4, max_length=MAX_BASE64_CHARS)
    frame_id: Union[int, str, None] = None
    roi: Region | None = None


@asynccontextmanager
async def lifespan(application: FastAPI):
    application.state.engine = await run_in_threadpool(BiomedEngine)
    yield
    application.state.engine = None


app = FastAPI(title="SA'EF FP32 BiomedCLIP", lifespan=lifespan)
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["GET", "POST"],
                   allow_headers=["Content-Type", "X-API-Key", "Authorization"])


def authorize(request: Request):
    expected = os.environ.get("API_KEY", "")
    if not expected:
        return
    value = request.headers.get("X-API-Key", "")
    if not value:
        value = request.headers.get("Authorization", "").removeprefix("Bearer ")
    if not hmac.compare_digest(value.encode(), expected.encode()):
        raise HTTPException(401, "Supply your Space API_KEY in X-API-Key.")


def infer(request: Request, data: bytes, frame_id=None, roi=None):
    engine = request.app.state.engine
    if not data or len(data) > MAX_IMAGE_BYTES:
        raise HTTPException(413, "Supply an image of at most 6 MiB.")
    if not engine.lock.acquire(blocking=False):
        raise HTTPException(429, "Inference busy. Drop this frame and send the newest one later.",
                            headers={"Retry-After": "1"})
    try:
        return engine.classify(data, frame_id=frame_id, roi=roi)
    except ValueError as error:
        raise HTTPException(400, str(error)) from error
    finally:
        engine.lock.release()


@app.get("/health")
def health(request: Request):
    engine = request.app.state.engine
    return {"status": "ready", "device": engine.device.type, "precision": "float32",
            "classes": len(engine.labels), "supports_localization": False,
            "queue_policy": "reject_if_busy", "api_key_required": bool(os.environ.get("API_KEY"))}


@app.get("/labels", dependencies=[Depends(authorize)])
def labels(request: Request):
    return request.app.state.engine.labels


@app.post("/predict", dependencies=[Depends(authorize)])
def predict(body: ImageRequest, request: Request):
    encoded = body.image_base64
    if encoded.startswith("data:"):
        if "," not in encoded:
            raise HTTPException(400, "Invalid image data URL.")
        encoded = encoded.split(",", 1)[1]
    try:
        image = base64.b64decode(encoded, validate=True)
    except (binascii.Error, ValueError) as error:
        raise HTTPException(400, "image_base64 must contain valid base64.") from error
    return infer(request, image, body.frame_id, body.roi.model_dump() if body.roi else None)


@app.post("/classify", dependencies=[Depends(authorize)])
async def classify(request: Request, frame_id: str | None = None):
    # Raw JPEG avoids multipart/base64 overhead for live-camera requests.
    payload = bytearray()
    async for chunk in request.stream():
        payload.extend(chunk)
        if len(payload) > MAX_IMAGE_BYTES:
            raise HTTPException(413, "Image exceeds 6 MiB.")
    return await run_in_threadpool(infer, request, bytes(payload), frame_id)


@app.get("/", response_class=HTMLResponse)
def index():
    return """<!doctype html><html lang="en"><meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>SA'EF BiomedCLIP</title><style>
    body{font:16px system-ui;max-width:760px;margin:40px auto;padding:20px;color:#172334}
    input,button{font:inherit;padding:10px;margin:8px 0}pre{white-space:pre-wrap;overflow-wrap:anywhere}
    </style><h1>SA'EF · Full-precision BiomedCLIP</h1>
    <p>Upload a photo to inspect ranked image/text similarities. This classifier supplies no wound coordinates.</p>
    <input id="key" type="password" placeholder="API key, if configured" aria-label="API key"><br>
    <input id="file" type="file" accept="image/jpeg,image/png,image/webp" aria-label="Test image"><br>
    <button id="go">Classify image</button><pre id="output" role="status"></pre>
    <p><a href="/docs">API documentation</a> · <a href="/health">Health</a></p>
    <script>
    document.getElementById('go').onclick=async()=>{
      const button=document.getElementById('go'), out=document.getElementById('output');
      const file=document.getElementById('file').files[0];if(!file){out.textContent='Select an image.';return;}
      button.disabled=true;out.textContent='Analyzing…';const started=performance.now();
      try{const response=await fetch('/classify',{method:'POST',headers:{'Content-Type':file.type,
        'X-API-Key':document.getElementById('key').value},body:file});
        const data=await response.json();out.textContent='Round trip: '+Math.round(performance.now()-started)+' ms\\n'+JSON.stringify(data,null,2);
      }catch(error){out.textContent=String(error);}finally{button.disabled=false;}
    };</script></html>"""