Spaces:
Sleeping
Sleeping
File size: 2,022 Bytes
287ab0c |
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 |
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import Dict, Any
from model import DummyGenderModel
from PIL import Image
import io
import time
import uuid
app = FastAPI(title="Radiograph Gender Predictor (placeholder)",
description="Returns a random gender prediction for an uploaded radiograph image. Replace the DummyGenderModel with your trained model when ready.",
version="0.1.0")
model = DummyGenderModel() # placeholder. replace with your real model object when available.
class PredictResponse(BaseModel):
id: str
prediction: str
confidence: float
probabilities: Dict[str, float]
model_version: str
runtime_ms: int
timestamp: float
@app.get("/health")
def health():
return {"status": "ok", "model_loaded": model.is_loaded(), "model_version": model.version}
@app.post("/predict", response_model=PredictResponse)
async def predict(file: UploadFile = File(...)):
"""
Accepts an image file (radiograph). Returns a JSON with a random gender prediction.
Content-type should be one of common image types: image/jpeg, image/png, image/tiff, etc.
"""
start = time.time()
# basic content-type check (optional)
if not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="Uploaded file must be an image.")
contents = await file.read()
try:
img = Image.open(io.BytesIO(contents)).convert("RGB")
except Exception as e:
raise HTTPException(status_code=400, detail=f"Unable to parse image: {e}")
# call the placeholder model (random)
result = model.predict(img)
runtime_ms = int((time.time() - start) * 1000)
response = {
"id": str(uuid.uuid4()),
"prediction": result["label"],
"model_version": model.version,
"runtime_ms": runtime_ms,
"timestamp": time.time()
}
return JSONResponse(content=response)
|