Update app/main.py
Browse files- app/main.py +60 -65
app/main.py
CHANGED
|
@@ -1,65 +1,60 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
app
|
| 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 |
-
buf
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
async def health():
|
| 62 |
-
return {"status": "ok", "classes": CLASS_NAMES}
|
| 63 |
-
# ---------------------------
|
| 64 |
-
# End of app/main.py
|
| 65 |
-
# ---------------------------
|
|
|
|
| 1 |
+
# File: app/main.py
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, Query
|
| 3 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import numpy as np
|
| 7 |
+
import traceback
|
| 8 |
+
|
| 9 |
+
# Import the model utilities
|
| 10 |
+
from app.model import predict, gradcam, CLASS_NAMES
|
| 11 |
+
|
| 12 |
+
app = FastAPI(title="Brain Tumor MRI Classifier (InceptionV3 + Grad-CAM)")
|
| 13 |
+
|
| 14 |
+
@app.post("/predict")
|
| 15 |
+
async def predict_image(file: UploadFile = File(...)):
|
| 16 |
+
try:
|
| 17 |
+
contents = await file.read()
|
| 18 |
+
pil_img = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 19 |
+
label, confidence, probs = predict(pil_img)
|
| 20 |
+
return JSONResponse({
|
| 21 |
+
"predicted_label": label,
|
| 22 |
+
"confidence": round(confidence, 3),
|
| 23 |
+
"probabilities": {k: round(v, 6) for k, v in probs.items()}
|
| 24 |
+
})
|
| 25 |
+
except Exception as e:
|
| 26 |
+
tb = traceback.format_exc()
|
| 27 |
+
return JSONResponse({"error": str(e), "trace": tb}, status_code=500)
|
| 28 |
+
|
| 29 |
+
@app.post("/gradcam")
|
| 30 |
+
async def gradcam_image(file: UploadFile = File(...), interpolant: float = Query(0.5, ge=0.0, le=1.0)):
|
| 31 |
+
"""
|
| 32 |
+
Returns a PNG image (overlay) produced by gradcam().
|
| 33 |
+
`interpolant` controls mixing (0..1).
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
contents = await file.read()
|
| 37 |
+
pil_img = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 38 |
+
|
| 39 |
+
# Compute overlay (this calls the optimized gradcam in model.py)
|
| 40 |
+
overlay = gradcam(pil_img, interpolant=float(interpolant))
|
| 41 |
+
|
| 42 |
+
# Ensure correct dtype and shape
|
| 43 |
+
overlay = np.asarray(overlay).astype("uint8")
|
| 44 |
+
if overlay.ndim == 2:
|
| 45 |
+
overlay = np.stack([overlay] * 3, axis=-1)
|
| 46 |
+
|
| 47 |
+
# Convert to PNG bytes
|
| 48 |
+
buf = io.BytesIO()
|
| 49 |
+
Image.fromarray(overlay).save(buf, format="PNG")
|
| 50 |
+
buf.seek(0)
|
| 51 |
+
return StreamingResponse(buf, media_type="image/png")
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
tb = traceback.format_exc()
|
| 55 |
+
return JSONResponse({"error": str(e), "trace": tb}, status_code=500)
|
| 56 |
+
|
| 57 |
+
# Optional health endpoint
|
| 58 |
+
@app.get("/health")
|
| 59 |
+
async def health():
|
| 60 |
+
return {"status": "ok", "classes": CLASS_NAMES}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|