forked from Project-MONAI/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
198 lines (162 loc) · 6.06 KB
/
main.py
File metadata and controls
198 lines (162 loc) · 6.06 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
FastAPI Application for MONAI Model Inference
This module provides a REST API for deploying MONAI model bundles.
It demonstrates how to serve medical imaging AI models in production.
"""
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI, File, UploadFile, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from .inference import inference_engine
from .model_loader import model_loader
from .schemas import HealthResponse, PredictionResponse, ErrorResponse
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifespan context manager for FastAPI app.
Handles startup and shutdown events.
"""
# Startup: Load the MONAI model
logger.info("Starting up: Loading MONAI model...")
try:
model_loader.load_model(model_name="spleen_ct_segmentation", bundle_dir="./models")
logger.info("Model loaded successfully!")
except Exception as e:
logger.error(f"Failed to load model: {e}")
# Continue anyway - model loading can be retried
yield
# Shutdown: Cleanup
logger.info("Shutting down...")
# Initialize FastAPI app
app = FastAPI(
title="MONAI Inference API",
description="REST API for deploying MONAI model bundles for medical image inference",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, specify exact origins
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
"""Global exception handler for unexpected errors."""
logger.error(f"Unexpected error: {str(exc)}")
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={
"error": "InternalServerError",
"detail": "An unexpected error occurred. Please try again.",
"status_code": 500,
},
)
@app.get(
"/",
summary="Root endpoint",
description="Returns basic API information",
)
async def root():
"""Root endpoint - API information."""
return {
"name": "MONAI Inference API",
"version": "1.0.0",
"description": "FastAPI deployment for MONAI models",
"endpoints": {
"health": "/health",
"predict": "/predict",
"docs": "/docs",
},
}
@app.get(
"/health",
response_model=HealthResponse,
summary="Health check",
description="Check if the service and model are ready",
)
async def health_check():
"""
Health check endpoint.
Returns:
HealthResponse: Service and model status
"""
is_loaded = model_loader.is_loaded()
return HealthResponse(
status="healthy" if is_loaded else "model_not_loaded",
model_loaded=is_loaded,
device=str(model_loader.device) if is_loaded else "unknown",
)
@app.post(
"/predict",
response_model=PredictionResponse,
summary="Run inference",
description="Upload a medical image and get predictions",
responses={
200: {"description": "Successful prediction"},
400: {"model": ErrorResponse, "description": "Bad request"},
500: {"model": ErrorResponse, "description": "Internal server error"},
},
)
async def predict(file: UploadFile = File(..., description="Medical image file (NIfTI format: .nii or .nii.gz)")):
"""
Run inference on uploaded medical image.
Args:
file: Uploaded image file (NIfTI format)
Returns:
PredictionResponse: Prediction results with metadata
Raises:
HTTPException: If file format is invalid or inference fails
"""
# Validate file format
if not file.filename.endswith((".nii", ".nii.gz")):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid file format. Supported formats: .nii, .nii.gz"
)
# Check if model is loaded
if not model_loader.is_loaded():
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Model not loaded. Please try again later."
)
try:
# Read file content
contents = await file.read()
# Run inference
result = await inference_engine.process_image(image_bytes=contents, filename=file.filename)
return PredictionResponse(**result)
except ValueError as e:
# Client error (bad input)
logger.warning(f"Bad request: {str(e)}")
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
except RuntimeError as e:
# Server error (inference failed)
logger.error(f"Inference error: {str(e)}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Inference failed: {str(e)}")
except Exception as e:
# Unexpected error
logger.error(f"Unexpected error during prediction: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An unexpected error occurred during prediction"
)
if __name__ == "__main__":
import uvicorn
# For development only - use proper ASGI server in production
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True, log_level="info")