Deploy main.py to backend/ directory
Browse files- backend/main.py +50 -46
backend/main.py
CHANGED
|
@@ -28,7 +28,7 @@ from clinical_synthesis_service import get_synthesis_service
|
|
| 28 |
# Import monitoring and infrastructure modules
|
| 29 |
from monitoring_service import get_monitoring_service
|
| 30 |
from model_versioning import get_versioning_system
|
| 31 |
-
from production_logging import get_medical_logger
|
| 32 |
from compliance_reporting import get_compliance_system
|
| 33 |
from admin_endpoints import admin_router
|
| 34 |
|
|
@@ -72,12 +72,12 @@ async def monitoring_middleware(request: Request, call_next):
|
|
| 72 |
request_id = str(uuid.uuid4())
|
| 73 |
|
| 74 |
# Log request start
|
| 75 |
-
medical_logger.info("Request received",
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
try:
|
| 83 |
# Process request
|
|
@@ -95,13 +95,13 @@ async def monitoring_middleware(request: Request, call_next):
|
|
| 95 |
)
|
| 96 |
|
| 97 |
# Log request completion
|
| 98 |
-
medical_logger.info("Request completed",
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
return response
|
| 107 |
|
|
@@ -118,14 +118,14 @@ async def monitoring_middleware(request: Request, call_next):
|
|
| 118 |
)
|
| 119 |
|
| 120 |
# Log error
|
| 121 |
-
medical_logger.error("Request failed",
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
# Re-raise the exception
|
| 131 |
raise
|
|
@@ -172,17 +172,17 @@ async def startup_event():
|
|
| 172 |
Ensures all infrastructure components are ready before accepting requests
|
| 173 |
"""
|
| 174 |
|
| 175 |
-
medical_logger.info("Starting Medical AI Platform initialization",
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
|
| 180 |
# Initialize monitoring service
|
| 181 |
monitoring_service.start_monitoring()
|
| 182 |
-
medical_logger.info("Monitoring service initialized",
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
|
| 187 |
# Initialize versioning system with current models
|
| 188 |
model_versions = [
|
|
@@ -201,15 +201,15 @@ async def startup_event():
|
|
| 201 |
metadata={"source": model_config["source"]}
|
| 202 |
)
|
| 203 |
|
| 204 |
-
medical_logger.info("Model versioning initialized",
|
| 205 |
-
|
| 206 |
-
|
| 207 |
|
| 208 |
# Initialize compliance reporting
|
| 209 |
-
medical_logger.info("Compliance reporting system initialized",
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
|
| 214 |
# Log system configuration
|
| 215 |
system_config = {
|
|
@@ -237,9 +237,9 @@ async def startup_event():
|
|
| 237 |
"components_ready": True
|
| 238 |
})
|
| 239 |
except Exception as e:
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
|
| 244 |
medical_logger.info("Medical AI Platform startup complete", {
|
| 245 |
"status": "ready",
|
|
@@ -371,7 +371,9 @@ async def get_health_dashboard():
|
|
| 371 |
"last_used": perf.get("last_used", "never")
|
| 372 |
}
|
| 373 |
except Exception as e:
|
| 374 |
-
medical_logger.warning("Failed to get model metrics",
|
|
|
|
|
|
|
| 375 |
|
| 376 |
# Get pipeline statistics
|
| 377 |
pipeline_stats = {
|
|
@@ -392,7 +394,9 @@ async def get_health_dashboard():
|
|
| 392 |
try:
|
| 393 |
synthesis_stats = synthesis_service.get_synthesis_statistics()
|
| 394 |
except Exception as e:
|
| 395 |
-
medical_logger.warning("Failed to get synthesis stats",
|
|
|
|
|
|
|
| 396 |
|
| 397 |
# Compliance overview
|
| 398 |
compliance_overview = {
|
|
@@ -463,10 +467,10 @@ async def get_health_dashboard():
|
|
| 463 |
return dashboard
|
| 464 |
|
| 465 |
except Exception as e:
|
| 466 |
-
medical_logger.error("Dashboard generation failed",
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
|
| 471 |
# Return minimal dashboard on error
|
| 472 |
return {
|
|
|
|
| 28 |
# Import monitoring and infrastructure modules
|
| 29 |
from monitoring_service import get_monitoring_service
|
| 30 |
from model_versioning import get_versioning_system
|
| 31 |
+
from production_logging import get_medical_logger, EventCategory
|
| 32 |
from compliance_reporting import get_compliance_system
|
| 33 |
from admin_endpoints import admin_router
|
| 34 |
|
|
|
|
| 72 |
request_id = str(uuid.uuid4())
|
| 73 |
|
| 74 |
# Log request start
|
| 75 |
+
medical_logger.info("Request received",
|
| 76 |
+
EventCategory.SYSTEM_EVENT,
|
| 77 |
+
request_id=request_id,
|
| 78 |
+
method=request.method,
|
| 79 |
+
path=request.url.path,
|
| 80 |
+
client=request.client.host if request.client else "unknown")
|
| 81 |
|
| 82 |
try:
|
| 83 |
# Process request
|
|
|
|
| 95 |
)
|
| 96 |
|
| 97 |
# Log request completion
|
| 98 |
+
medical_logger.info("Request completed",
|
| 99 |
+
EventCategory.SYSTEM_EVENT,
|
| 100 |
+
request_id=request_id,
|
| 101 |
+
method=request.method,
|
| 102 |
+
path=request.url.path,
|
| 103 |
+
status_code=response.status_code,
|
| 104 |
+
latency_ms=round(latency_ms, 2))
|
| 105 |
|
| 106 |
return response
|
| 107 |
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
# Log error
|
| 121 |
+
medical_logger.error("Request failed",
|
| 122 |
+
EventCategory.ERROR_EVENT,
|
| 123 |
+
request_id=request_id,
|
| 124 |
+
method=request.method,
|
| 125 |
+
path=request.url.path,
|
| 126 |
+
error=str(e),
|
| 127 |
+
error_type=type(e).__name__,
|
| 128 |
+
latency_ms=round(latency_ms, 2))
|
| 129 |
|
| 130 |
# Re-raise the exception
|
| 131 |
raise
|
|
|
|
| 172 |
Ensures all infrastructure components are ready before accepting requests
|
| 173 |
"""
|
| 174 |
|
| 175 |
+
medical_logger.info("Starting Medical AI Platform initialization",
|
| 176 |
+
EventCategory.SYSTEM_EVENT,
|
| 177 |
+
version="2.0.0",
|
| 178 |
+
timestamp=datetime.utcnow().isoformat())
|
| 179 |
|
| 180 |
# Initialize monitoring service
|
| 181 |
monitoring_service.start_monitoring()
|
| 182 |
+
medical_logger.info("Monitoring service initialized",
|
| 183 |
+
EventCategory.SYSTEM_EVENT,
|
| 184 |
+
cache_enabled=True,
|
| 185 |
+
alert_threshold=0.05)
|
| 186 |
|
| 187 |
# Initialize versioning system with current models
|
| 188 |
model_versions = [
|
|
|
|
| 201 |
metadata={"source": model_config["source"]}
|
| 202 |
)
|
| 203 |
|
| 204 |
+
medical_logger.info("Model versioning initialized",
|
| 205 |
+
EventCategory.SYSTEM_EVENT,
|
| 206 |
+
total_models=len(model_versions))
|
| 207 |
|
| 208 |
# Initialize compliance reporting
|
| 209 |
+
medical_logger.info("Compliance reporting system initialized",
|
| 210 |
+
EventCategory.SYSTEM_EVENT,
|
| 211 |
+
standards=["HIPAA", "GDPR"],
|
| 212 |
+
audit_enabled=True)
|
| 213 |
|
| 214 |
# Log system configuration
|
| 215 |
system_config = {
|
|
|
|
| 237 |
"components_ready": True
|
| 238 |
})
|
| 239 |
except Exception as e:
|
| 240 |
+
medical_logger.error("Health check failed during startup",
|
| 241 |
+
EventCategory.ERROR_EVENT,
|
| 242 |
+
error=str(e))
|
| 243 |
|
| 244 |
medical_logger.info("Medical AI Platform startup complete", {
|
| 245 |
"status": "ready",
|
|
|
|
| 371 |
"last_used": perf.get("last_used", "never")
|
| 372 |
}
|
| 373 |
except Exception as e:
|
| 374 |
+
medical_logger.warning("Failed to get model metrics",
|
| 375 |
+
EventCategory.PERFORMANCE_EVENT,
|
| 376 |
+
error=str(e))
|
| 377 |
|
| 378 |
# Get pipeline statistics
|
| 379 |
pipeline_stats = {
|
|
|
|
| 394 |
try:
|
| 395 |
synthesis_stats = synthesis_service.get_synthesis_statistics()
|
| 396 |
except Exception as e:
|
| 397 |
+
medical_logger.warning("Failed to get synthesis stats",
|
| 398 |
+
EventCategory.PERFORMANCE_EVENT,
|
| 399 |
+
error=str(e))
|
| 400 |
|
| 401 |
# Compliance overview
|
| 402 |
compliance_overview = {
|
|
|
|
| 467 |
return dashboard
|
| 468 |
|
| 469 |
except Exception as e:
|
| 470 |
+
medical_logger.error("Dashboard generation failed",
|
| 471 |
+
EventCategory.ERROR_EVENT,
|
| 472 |
+
error=str(e),
|
| 473 |
+
timestamp=datetime.utcnow().isoformat())
|
| 474 |
|
| 475 |
# Return minimal dashboard on error
|
| 476 |
return {
|