Spaces:
Sleeping
Sleeping
| 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 | |
| def health(): | |
| return {"status": "ok", "model_loaded": model.is_loaded(), "model_version": model.version} | |
| 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) | |