Commit
·
ffa5197
1
Parent(s):
6772223
Update app/fastapi_server.py
Browse files- app/fastapi_server.py +28 -30
app/fastapi_server.py
CHANGED
|
@@ -168,7 +168,7 @@ class ModelManager:
|
|
| 168 |
logger.error(f"Prediction failed: {e}")
|
| 169 |
raise HTTPException(
|
| 170 |
status_code=500,
|
| 171 |
-
detail=f"Prediction failed: {str(e)}"
|
| 172 |
# Background task functions
|
| 173 |
async def log_prediction(text: str, prediction: str, confidence: float, client_ip: str, processing_time: float):
|
| 174 |
"""Log prediction details with error handling for file access"""
|
|
@@ -236,34 +236,6 @@ async def log_batch_prediction(total_texts: int, successful_predictions: int, cl
|
|
| 236 |
logger.error(f"Failed to log batch prediction: {e}")
|
| 237 |
|
| 238 |
|
| 239 |
-
# Custom OpenAPI
|
| 240 |
-
def custom_openapi():
|
| 241 |
-
if app.openapi_schema:
|
| 242 |
-
return app.openapi_schema
|
| 243 |
-
|
| 244 |
-
openapi_schema = get_openapi(
|
| 245 |
-
title="Fake News Detection API",
|
| 246 |
-
version="2.0.0",
|
| 247 |
-
description="Production-ready API for fake news detection with comprehensive monitoring and security features",
|
| 248 |
-
routes=app.routes,
|
| 249 |
-
)
|
| 250 |
-
|
| 251 |
-
# Add security definitions
|
| 252 |
-
openapi_schema["components"]["securitySchemes"] = {
|
| 253 |
-
"Bearer": {
|
| 254 |
-
"type": "http",
|
| 255 |
-
"scheme": "bearer",
|
| 256 |
-
"bearerFormat": "JWT",
|
| 257 |
-
}
|
| 258 |
-
}
|
| 259 |
-
|
| 260 |
-
app.openapi_schema = openapi_schema
|
| 261 |
-
return app.openapi_schema
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
# Set the custom OpenAPI function AFTER app is created
|
| 265 |
-
app.openapi = custom_openapi
|
| 266 |
-
|
| 267 |
if __name__ == "__main__":
|
| 268 |
uvicorn.run(
|
| 269 |
"fastapi_server:app",
|
|
@@ -357,6 +329,33 @@ app.add_middleware(
|
|
| 357 |
allowed_hosts=["*"] # Configure appropriately for production
|
| 358 |
)
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
# Request/Response models (unchanged)
|
| 361 |
class PredictionRequest(BaseModel):
|
| 362 |
text: str = Field(..., min_length=1, max_length=10000,
|
|
@@ -729,7 +728,6 @@ async def health_check():
|
|
| 729 |
environment_info={"error": str(e)}
|
| 730 |
)
|
| 731 |
|
| 732 |
-
|
| 733 |
@app.get("/metrics")
|
| 734 |
async def get_metrics():
|
| 735 |
"""
|
|
|
|
| 168 |
logger.error(f"Prediction failed: {e}")
|
| 169 |
raise HTTPException(
|
| 170 |
status_code=500,
|
| 171 |
+
detail=f"Prediction failed: {str(e)}"
|
| 172 |
# Background task functions
|
| 173 |
async def log_prediction(text: str, prediction: str, confidence: float, client_ip: str, processing_time: float):
|
| 174 |
"""Log prediction details with error handling for file access"""
|
|
|
|
| 236 |
logger.error(f"Failed to log batch prediction: {e}")
|
| 237 |
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
if __name__ == "__main__":
|
| 240 |
uvicorn.run(
|
| 241 |
"fastapi_server:app",
|
|
|
|
| 329 |
allowed_hosts=["*"] # Configure appropriately for production
|
| 330 |
)
|
| 331 |
|
| 332 |
+
# Custom OpenAPI setup - RIGHT AFTER app creation
|
| 333 |
+
def custom_openapi():
|
| 334 |
+
if app.openapi_schema:
|
| 335 |
+
return app.openapi_schema
|
| 336 |
+
|
| 337 |
+
openapi_schema = get_openapi(
|
| 338 |
+
title="Fake News Detection API",
|
| 339 |
+
version="2.0.0",
|
| 340 |
+
description="Production-ready API for fake news detection with comprehensive monitoring and security features",
|
| 341 |
+
routes=app.routes,
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
# Add security definitions
|
| 345 |
+
openapi_schema["components"]["securitySchemes"] = {
|
| 346 |
+
"Bearer": {
|
| 347 |
+
"type": "http",
|
| 348 |
+
"scheme": "bearer",
|
| 349 |
+
"bearerFormat": "JWT",
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
app.openapi_schema = openapi_schema
|
| 354 |
+
return app.openapi_schema
|
| 355 |
+
|
| 356 |
+
# Set the custom OpenAPI function
|
| 357 |
+
app.openapi = custom_openapi
|
| 358 |
+
|
| 359 |
# Request/Response models (unchanged)
|
| 360 |
class PredictionRequest(BaseModel):
|
| 361 |
text: str = Field(..., min_length=1, max_length=10000,
|
|
|
|
| 728 |
environment_info={"error": str(e)}
|
| 729 |
)
|
| 730 |
|
|
|
|
| 731 |
@app.get("/metrics")
|
| 732 |
async def get_metrics():
|
| 733 |
"""
|