Download app.py from m4az19/saef-api: direct link, hf CLI and curl.
- Browser
- Download file 5.98 kB
-
https://huggingface.co/spaces/m4az19/saef-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/m4az19/saef-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/m4az19/saef-api/resolve/main/app.py
5.98 kB
| """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) | |
| 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 | |
| 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() | |
| 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"))} | |
| def labels(request: Request): | |
| return request.app.state.engine.labels | |
| 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) | |
| 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) | |
| 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>""" | |