Spaces:
Paused
Paused
Commit ·
f95c45e
1
Parent(s): d57d5a9
app.py
Browse files
__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
|
app.py
CHANGED
|
@@ -70,8 +70,22 @@ try:
|
|
| 70 |
logging.info("Successfully imported create_app and initialize_agents")
|
| 71 |
|
| 72 |
# Create the app instance
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Initialize agents with minimal preloading for Hugging Face Spaces
|
| 77 |
try:
|
|
@@ -178,3 +192,17 @@ if app is None:
|
|
| 178 |
|
| 179 |
# Export the app for Hugging Face Spaces
|
| 180 |
__all__ = ["app"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
logging.info("Successfully imported create_app and initialize_agents")
|
| 71 |
|
| 72 |
# Create the app instance
|
| 73 |
+
try:
|
| 74 |
+
app = create_app()
|
| 75 |
+
logging.info("App instance created successfully")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
logging.error(f"Failed to create app: {e}")
|
| 78 |
+
# Create minimal fallback app
|
| 79 |
+
from fastapi import FastAPI
|
| 80 |
+
app = FastAPI(title="Medical AI Service (fallback)")
|
| 81 |
+
|
| 82 |
+
@app.get("/")
|
| 83 |
+
async def root():
|
| 84 |
+
return {"message": "Medical AI Service - Fallback mode", "error": str(e)}
|
| 85 |
+
|
| 86 |
+
@app.get("/health")
|
| 87 |
+
async def health():
|
| 88 |
+
return {"status": "degraded", "message": "App creation failed", "error": str(e)}
|
| 89 |
|
| 90 |
# Initialize agents with minimal preloading for Hugging Face Spaces
|
| 91 |
try:
|
|
|
|
| 192 |
|
| 193 |
# Export the app for Hugging Face Spaces
|
| 194 |
__all__ = ["app"]
|
| 195 |
+
|
| 196 |
+
# Ensure the app is available at module level
|
| 197 |
+
if 'app' not in locals():
|
| 198 |
+
logging.error("No app instance created, creating emergency fallback...")
|
| 199 |
+
from fastapi import FastAPI
|
| 200 |
+
app = FastAPI(title="Medical AI Service (emergency fallback)")
|
| 201 |
+
|
| 202 |
+
@app.get("/")
|
| 203 |
+
async def root():
|
| 204 |
+
return {"message": "Emergency fallback mode", "status": "error"}
|
| 205 |
+
|
| 206 |
+
@app.get("/health")
|
| 207 |
+
async def health():
|
| 208 |
+
return {"status": "error", "message": "Emergency fallback mode"}
|
services/ai-service/src/__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/services/ai-service/src/__pycache__/app.cpython-311.pyc and b/services/ai-service/src/__pycache__/app.cpython-311.pyc differ
|
|
|
services/ai-service/src/ai_med_extract/__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/services/ai-service/src/ai_med_extract/__pycache__/app.cpython-311.pyc and b/services/ai-service/src/ai_med_extract/__pycache__/app.cpython-311.pyc differ
|
|
|
services/ai-service/src/ai_med_extract/app.py
CHANGED
|
@@ -14,7 +14,19 @@ from .api_middleware import SecurityHeadersMiddleware
|
|
| 14 |
from .scalable_service_mesh import ScalableServiceMesh, initialize_scalable_mesh, scalable_lifespan
|
| 15 |
from .database_audit import initialize_db_audit_logger
|
| 16 |
from .monitoring_observability import initialize_monitoring
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Ensure reasonable default for thread usage
|
| 20 |
torch.set_num_threads(1)
|
|
@@ -71,11 +83,16 @@ async def lifespan(app: FastAPI):
|
|
| 71 |
service_mesh = None
|
| 72 |
if redis_client:
|
| 73 |
try:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
logging.warning(f"Scalable service mesh initialization failed: {e}")
|
| 81 |
service_mesh = None
|
|
@@ -403,13 +420,62 @@ def initialize_agents(app: FastAPI, *, preload_small_models: bool = True):
|
|
| 403 |
|
| 404 |
If preload_small_models is False, agents will be created using lazy/fallback loaders only.
|
| 405 |
"""
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
# Initialize lightweight agents
|
| 415 |
text_extractor_agent = TextExtractorAgent()
|
|
@@ -543,13 +609,25 @@ def initialize_agents(app: FastAPI, *, preload_small_models: bool = True):
|
|
| 543 |
print(f"[GGUF] Preload failed (non-fatal): {e}")
|
| 544 |
|
| 545 |
# Register routes only after agents are prepared
|
| 546 |
-
|
| 547 |
-
|
|
|
|
| 548 |
|
| 549 |
-
|
| 550 |
-
|
| 551 |
|
| 552 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
# Mark the app as ready for readiness checks
|
| 554 |
try:
|
| 555 |
app._is_ready = True
|
|
|
|
| 14 |
from .scalable_service_mesh import ScalableServiceMesh, initialize_scalable_mesh, scalable_lifespan
|
| 15 |
from .database_audit import initialize_db_audit_logger
|
| 16 |
from .monitoring_observability import initialize_monitoring
|
| 17 |
+
|
| 18 |
+
# Import model_manager with error handling
|
| 19 |
+
try:
|
| 20 |
+
from .utils.model_manager import model_manager
|
| 21 |
+
except ImportError as e:
|
| 22 |
+
logging.warning(f"Failed to import model_manager: {e}")
|
| 23 |
+
# Create a fallback model manager
|
| 24 |
+
class FallbackModelManager:
|
| 25 |
+
def get_model_loader(self, *args, **kwargs):
|
| 26 |
+
return None
|
| 27 |
+
def generate_text(self, *args, **kwargs):
|
| 28 |
+
return "Model not available"
|
| 29 |
+
model_manager = FallbackModelManager()
|
| 30 |
|
| 31 |
# Ensure reasonable default for thread usage
|
| 32 |
torch.set_num_threads(1)
|
|
|
|
| 83 |
service_mesh = None
|
| 84 |
if redis_client:
|
| 85 |
try:
|
| 86 |
+
# Check if model_manager is available before using it
|
| 87 |
+
if hasattr(model_manager, 'get_model_loader'):
|
| 88 |
+
service_mesh = await initialize_scalable_mesh(model_manager, redis_client, {
|
| 89 |
+
'database_url': database_url,
|
| 90 |
+
'audit_logger': db_audit_logger
|
| 91 |
+
})
|
| 92 |
+
logging.info("Scalable service mesh initialized successfully")
|
| 93 |
+
else:
|
| 94 |
+
logging.warning("Model manager not available, skipping service mesh initialization")
|
| 95 |
+
service_mesh = None
|
| 96 |
except Exception as e:
|
| 97 |
logging.warning(f"Scalable service mesh initialization failed: {e}")
|
| 98 |
service_mesh = None
|
|
|
|
| 420 |
|
| 421 |
If preload_small_models is False, agents will be created using lazy/fallback loaders only.
|
| 422 |
"""
|
| 423 |
+
try:
|
| 424 |
+
# Local imports to avoid heavy work at module import time
|
| 425 |
+
from .agents.text_extractor import TextExtractorAgent
|
| 426 |
+
from .agents.phi_scrubber import PHIScrubberAgent
|
| 427 |
+
from .agents.summarizer import SummarizerAgent
|
| 428 |
+
from .agents.medical_data_extractor import MedicalDataExtractorAgent
|
| 429 |
+
from .agents.patient_summary_agent import PatientSummarizerAgent
|
| 430 |
+
|
| 431 |
+
# Import model_manager with error handling
|
| 432 |
+
try:
|
| 433 |
+
from .utils.model_manager import model_manager
|
| 434 |
+
except ImportError as e:
|
| 435 |
+
logging.warning(f"Failed to import model_manager in initialize_agents: {e}")
|
| 436 |
+
# Create a fallback model manager
|
| 437 |
+
class FallbackModelManager:
|
| 438 |
+
def get_model_loader(self, *args, **kwargs):
|
| 439 |
+
return None
|
| 440 |
+
def generate_text(self, *args, **kwargs):
|
| 441 |
+
return "Model not available"
|
| 442 |
+
model_manager = FallbackModelManager()
|
| 443 |
+
except Exception as e:
|
| 444 |
+
logging.error(f"Failed to import agents: {e}")
|
| 445 |
+
# Create minimal fallback agents
|
| 446 |
+
class MinimalTextExtractor:
|
| 447 |
+
def extract_text(self, *args, **kwargs):
|
| 448 |
+
return "Text extraction not available"
|
| 449 |
+
|
| 450 |
+
class MinimalPHIScrubber:
|
| 451 |
+
def scrub_phi(self, *args, **kwargs):
|
| 452 |
+
return "PHI scrubbing not available"
|
| 453 |
+
|
| 454 |
+
class MinimalSummarizer:
|
| 455 |
+
def generate(self, *args, **kwargs):
|
| 456 |
+
return "Summarization not available"
|
| 457 |
+
|
| 458 |
+
class MinimalMedicalExtractor:
|
| 459 |
+
def generate(self, *args, **kwargs):
|
| 460 |
+
return "Medical extraction not available"
|
| 461 |
+
|
| 462 |
+
class MinimalPatientSummarizer:
|
| 463 |
+
def generate(self, *args, **kwargs):
|
| 464 |
+
return "Patient summarization not available"
|
| 465 |
+
|
| 466 |
+
class FallbackModelManager:
|
| 467 |
+
def get_model_loader(self, *args, **kwargs):
|
| 468 |
+
return None
|
| 469 |
+
def generate_text(self, *args, **kwargs):
|
| 470 |
+
return "Model not available"
|
| 471 |
+
|
| 472 |
+
# Use fallback classes
|
| 473 |
+
TextExtractorAgent = MinimalTextExtractor
|
| 474 |
+
PHIScrubberAgent = MinimalPHIScrubber
|
| 475 |
+
SummarizerAgent = MinimalSummarizer
|
| 476 |
+
MedicalDataExtractorAgent = MinimalMedicalExtractor
|
| 477 |
+
PatientSummarizerAgent = MinimalPatientSummarizer
|
| 478 |
+
model_manager = FallbackModelManager()
|
| 479 |
|
| 480 |
# Initialize lightweight agents
|
| 481 |
text_extractor_agent = TextExtractorAgent()
|
|
|
|
| 609 |
print(f"[GGUF] Preload failed (non-fatal): {e}")
|
| 610 |
|
| 611 |
# Register routes only after agents are prepared
|
| 612 |
+
try:
|
| 613 |
+
from .api.routes_fastapi import register_routes
|
| 614 |
+
from .health_endpoints import router as health_router
|
| 615 |
|
| 616 |
+
register_routes(app, agents)
|
| 617 |
+
app.include_router(health_router, prefix="/health")
|
| 618 |
|
| 619 |
+
logging.info("Agents initialized and routes registered")
|
| 620 |
+
except Exception as e:
|
| 621 |
+
logging.error(f"Failed to register routes: {e}")
|
| 622 |
+
# Add basic health endpoint as fallback
|
| 623 |
+
@app.get("/health")
|
| 624 |
+
async def basic_health():
|
| 625 |
+
return {"status": "degraded", "message": "Routes not fully registered", "error": str(e)}
|
| 626 |
+
|
| 627 |
+
@app.get("/")
|
| 628 |
+
async def root():
|
| 629 |
+
return {"message": "Medical AI Service - Limited functionality", "status": "degraded"}
|
| 630 |
+
|
| 631 |
# Mark the app as ready for readiness checks
|
| 632 |
try:
|
| 633 |
app._is_ready = True
|