Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import JSONResponse, StreamingResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
import tensorflow as tf
|
| 8 |
from tensorflow.keras.models import Model, load_model
|
| 9 |
from tensorflow.keras.preprocessing.image import img_to_array
|
|
@@ -21,14 +21,18 @@ import os
|
|
| 21 |
# Configuration
|
| 22 |
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
|
| 23 |
HEATMAP_EXPIRY = 300 # 5 minutes in seconds
|
| 24 |
-
RATE_LIMIT = "5/minute" # 5 requests per minute
|
| 25 |
|
|
|
|
| 26 |
app = FastAPI(
|
| 27 |
title="ChexNet Medical Imaging API",
|
| 28 |
description="API for chest X-ray analysis with Grad-CAM visualization",
|
| 29 |
-
version="
|
| 30 |
)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Mount static files
|
| 33 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 34 |
|
|
@@ -41,11 +45,6 @@ app.add_middleware(
|
|
| 41 |
allow_headers=["*"],
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# Initialize rate limiter (in-memory)
|
| 45 |
-
@app.on_event("startup")
|
| 46 |
-
async def startup():
|
| 47 |
-
await FastAPILimiter.init()
|
| 48 |
-
|
| 49 |
# Session storage for heatmaps
|
| 50 |
heatmap_store: Dict[str, dict] = {}
|
| 51 |
|
|
@@ -134,8 +133,8 @@ async def health_check():
|
|
| 134 |
async def get_class_names():
|
| 135 |
return {"classes": class_names}
|
| 136 |
|
| 137 |
-
@app.post("/analyze"
|
| 138 |
-
|
| 139 |
async def analyze_image(request: Request, file: UploadFile = File(...)):
|
| 140 |
"""
|
| 141 |
Analyze chest X-ray image and return predictions with Grad-CAM visualization
|
|
@@ -210,7 +209,7 @@ async def model_info():
|
|
| 210 |
"input_size": "540x540",
|
| 211 |
"classes": len(class_names),
|
| 212 |
"gradcam_layer": layer_name,
|
| 213 |
-
"rate_limit":
|
| 214 |
}
|
| 215 |
|
| 216 |
# Error handlers
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import JSONResponse, StreamingResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from slowapi import Limiter
|
| 6 |
+
from slowapi.util import get_remote_address
|
| 7 |
import tensorflow as tf
|
| 8 |
from tensorflow.keras.models import Model, load_model
|
| 9 |
from tensorflow.keras.preprocessing.image import img_to_array
|
|
|
|
| 21 |
# Configuration
|
| 22 |
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
|
| 23 |
HEATMAP_EXPIRY = 300 # 5 minutes in seconds
|
|
|
|
| 24 |
|
| 25 |
+
# Initialize FastAPI with rate limiting
|
| 26 |
app = FastAPI(
|
| 27 |
title="ChexNet Medical Imaging API",
|
| 28 |
description="API for chest X-ray analysis with Grad-CAM visualization",
|
| 29 |
+
version="2.0.0"
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Rate limiter setup
|
| 33 |
+
limiter = Limiter(key_func=get_remote_address)
|
| 34 |
+
app.state.limiter = limiter
|
| 35 |
+
|
| 36 |
# Mount static files
|
| 37 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 38 |
|
|
|
|
| 45 |
allow_headers=["*"],
|
| 46 |
)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Session storage for heatmaps
|
| 49 |
heatmap_store: Dict[str, dict] = {}
|
| 50 |
|
|
|
|
| 133 |
async def get_class_names():
|
| 134 |
return {"classes": class_names}
|
| 135 |
|
| 136 |
+
@app.post("/analyze")
|
| 137 |
+
@limiter.limit("5/minute")
|
| 138 |
async def analyze_image(request: Request, file: UploadFile = File(...)):
|
| 139 |
"""
|
| 140 |
Analyze chest X-ray image and return predictions with Grad-CAM visualization
|
|
|
|
| 209 |
"input_size": "540x540",
|
| 210 |
"classes": len(class_names),
|
| 211 |
"gradcam_layer": layer_name,
|
| 212 |
+
"rate_limit": "5 requests/minute"
|
| 213 |
}
|
| 214 |
|
| 215 |
# Error handlers
|