sachinchandrankallar commited on
Commit
bfe20a9
·
1 Parent(s): 2890ee7

✅ Application imports successfully ✅ App instance creates without errors ✅ All agents initialize properly ✅ Routes register correctly ✅ No memory leaks or hanging operations

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
@@ -43,31 +43,16 @@ os.environ.setdefault("MKL_NUM_THREADS", "1")
43
  # Create a robust app instance with multiple fallback strategies
44
  app = None
45
 
46
- # Strategy 1: Try to import from ai_med_extract package
47
  try:
48
  logging.info("Attempting to import from ai_med_extract package...")
49
  logging.info(f"Python path: {sys.path[:3]}")
50
  logging.info(f"Current working directory: {os.getcwd()}")
51
  logging.info(f"Files in current directory: {os.listdir('.')}")
52
 
53
- # Import with explicit path handling for better linter support
54
-
55
- # Try to load the module dynamically to avoid linter warnings
56
- ai_med_extract_app_path = os.path.join(src_dir, "ai_med_extract", "app.py")
57
- if os.path.exists(ai_med_extract_app_path):
58
- spec = importlib.util.spec_from_file_location("ai_med_extract.app", ai_med_extract_app_path)
59
- if spec and spec.loader:
60
- ai_med_extract_app = importlib.util.module_from_spec(spec)
61
- spec.loader.exec_module(ai_med_extract_app)
62
- create_app = ai_med_extract_app.create_app
63
- initialize_agents = ai_med_extract_app.initialize_agents
64
- logging.info("Successfully imported create_app and initialize_agents via dynamic import")
65
- else:
66
- raise ImportError("Could not load ai_med_extract.app module")
67
- else:
68
- # Fallback to regular import
69
- from ai_med_extract.app import create_app, initialize_agents # type: ignore
70
- logging.info("Successfully imported create_app and initialize_agents")
71
 
72
  # Create the app instance
73
  try:
@@ -104,69 +89,30 @@ except Exception as e:
104
  import traceback
105
  logging.error(f"Full traceback: {traceback.format_exc()}")
106
 
107
- # Strategy 2: Try direct import from the nested structure
108
- try:
109
- logging.info("Attempting direct import from nested structure...")
110
- # Add the specific path to ai_med_extract
111
- ai_med_extract_path = os.path.join(src_dir, "ai_med_extract")
112
- if ai_med_extract_path not in sys.path:
113
- sys.path.insert(0, ai_med_extract_path)
114
-
115
- logging.info(f"Added ai_med_extract_path to sys.path: {ai_med_extract_path}")
116
- logging.info(f"Files in ai_med_extract directory: {os.listdir(ai_med_extract_path) if os.path.exists(ai_med_extract_path) else 'Directory not found'}")
117
-
118
- # Try dynamic import for the nested structure as well
119
- ai_med_extract_app_path = os.path.join(ai_med_extract_path, "app.py")
120
- if os.path.exists(ai_med_extract_app_path):
121
- spec = importlib.util.spec_from_file_location("ai_med_extract.app", ai_med_extract_app_path)
122
- if spec and spec.loader:
123
- ai_med_extract_app = importlib.util.module_from_spec(spec)
124
- spec.loader.exec_module(ai_med_extract_app)
125
- create_app = ai_med_extract_app.create_app
126
- initialize_agents = ai_med_extract_app.initialize_agents
127
- logging.info("Successfully imported via dynamic import from nested structure")
128
- else:
129
- from ai_med_extract.app import create_app, initialize_agents # type: ignore
130
- else:
131
- from ai_med_extract.app import create_app, initialize_agents # type: ignore
132
-
133
- app = create_app()
134
-
135
- try:
136
- initialize_agents(app, preload_small_models=False)
137
- logging.info("Agents initialized successfully via direct import")
138
- except Exception as e:
139
- logging.warning(f"Agent initialization failed via direct import: {e}")
140
-
141
- except Exception as e:
142
- logging.error(f"Direct import also failed: {e}")
143
- import traceback
144
- logging.error(f"Direct import traceback: {traceback.format_exc()}")
145
-
146
- # Strategy 3: Create a minimal FastAPI app as final fallback
147
- logging.info("Creating minimal fallback app...")
148
- from fastapi import FastAPI
149
- app = FastAPI(title="Medical AI Service (fallback)")
150
-
151
- @app.get("/")
152
- async def root():
153
- return {
154
- "message": "Medical AI Service - Fallback mode",
155
- "error": str(e),
156
- "status": "degraded"
157
- }
158
-
159
- @app.get("/health")
160
- async def health():
161
- return {"status": "degraded", "message": "Fallback mode active"}
162
-
163
- @app.get("/docs")
164
- async def docs():
165
- return {"message": "API documentation not available in fallback mode"}
166
-
167
- @app.get("/redoc")
168
- async def redoc():
169
- return {"message": "ReDoc not available in fallback mode"}
170
 
171
  # Ensure we have an app instance
172
  if app is None:
 
43
  # Create a robust app instance with multiple fallback strategies
44
  app = None
45
 
46
+ # Simplified import strategy with better error handling
47
  try:
48
  logging.info("Attempting to import from ai_med_extract package...")
49
  logging.info(f"Python path: {sys.path[:3]}")
50
  logging.info(f"Current working directory: {os.getcwd()}")
51
  logging.info(f"Files in current directory: {os.listdir('.')}")
52
 
53
+ # Direct import approach - simpler and more reliable
54
+ from ai_med_extract.app import create_app, initialize_agents # type: ignore
55
+ logging.info("Successfully imported create_app and initialize_agents")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Create the app instance
58
  try:
 
89
  import traceback
90
  logging.error(f"Full traceback: {traceback.format_exc()}")
91
 
92
+ # Create a minimal FastAPI app as fallback
93
+ logging.info("Creating minimal fallback app...")
94
+ from fastapi import FastAPI
95
+ app = FastAPI(title="Medical AI Service (fallback)")
96
+
97
+ @app.get("/")
98
+ async def root():
99
+ return {
100
+ "message": "Medical AI Service - Fallback mode",
101
+ "error": str(e),
102
+ "status": "degraded"
103
+ }
104
+
105
+ @app.get("/health")
106
+ async def health():
107
+ return {"status": "degraded", "message": "Fallback mode active"}
108
+
109
+ @app.get("/docs")
110
+ async def docs():
111
+ return {"message": "API documentation not available in fallback mode"}
112
+
113
+ @app.get("/redoc")
114
+ async def redoc():
115
+ return {"message": "ReDoc not available in fallback mode"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  # Ensure we have an app instance
118
  if app is None:
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
@@ -50,16 +50,18 @@ async def lifespan(app: FastAPI):
50
  redis_client = None
51
  db_audit_logger = None
52
  service_mesh = None
 
53
 
54
  try:
55
- # Initialize Redis (optional)
56
  redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
57
  redis_client = None
58
 
59
  try:
60
- redis_client = redis.from_url(redis_url, decode_responses=True)
61
- # Test Redis connection
62
- await redis_client.ping()
 
63
  logging.info("Redis connected successfully")
64
  except Exception as redis_error:
65
  logging.warning(f"Redis connection failed: {redis_error}")
@@ -205,10 +207,20 @@ def create_app(config: dict = None, initialize: bool = True) -> FastAPI:
205
  os.environ.setdefault(env_var, path)
206
  os.makedirs(path, exist_ok=True)
207
 
208
- # Global exception handler
209
  @app.exception_handler(Exception)
210
  async def global_exception_handler(request, exc):
211
  logging.error(f"Unhandled error: {str(exc)}", exc_info=True)
 
 
 
 
 
 
 
 
 
 
212
  return JSONResponse(
213
  status_code=500,
214
  content={"error": str(exc), "status": "error"}
@@ -494,12 +506,17 @@ def initialize_agents(app: FastAPI, *, preload_small_models: bool = True):
494
  summ_loader = model_manager.get_model_loader(_mc.get_default_model("summarization"), "summarization")
495
  summarizer_agent = SummarizerAgent(summ_loader)
496
  logging.info("SummarizerAgent initialized with bart-base")
497
- except Exception:
 
498
  from .utils.model_loader_gguf import create_fallback_pipeline
499
 
500
  class FallbackSummarizer:
501
  def generate(self, text, **kwargs):
502
- return create_fallback_pipeline().generate_full_summary(text)
 
 
 
 
503
 
504
  summarizer_agent = SummarizerAgent(FallbackSummarizer())
505
 
@@ -508,12 +525,17 @@ def initialize_agents(app: FastAPI, *, preload_small_models: bool = True):
508
  med_generator = med_loader.load()
509
  medical_data_extractor_agent = MedicalDataExtractorAgent(med_generator)
510
  logging.info("MedicalDataExtractorAgent initialized with distilgpt2")
511
- except Exception:
 
512
  from .utils.model_loader_gguf import create_fallback_pipeline
513
 
514
  class FallbackExtractor:
515
  def generate(self, prompt, **kwargs):
516
- return create_fallback_pipeline().generate(prompt)
 
 
 
 
517
 
518
  medical_data_extractor_agent = MedicalDataExtractorAgent(FallbackExtractor())
519
 
@@ -619,6 +641,9 @@ def initialize_agents(app: FastAPI, *, preload_small_models: bool = True):
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():
@@ -627,6 +652,14 @@ def initialize_agents(app: FastAPI, *, preload_small_models: bool = True):
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:
 
50
  redis_client = None
51
  db_audit_logger = None
52
  service_mesh = None
53
+ monitoring = None
54
 
55
  try:
56
+ # Initialize Redis (optional) - with timeout to prevent hanging
57
  redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
58
  redis_client = None
59
 
60
  try:
61
+ import asyncio
62
+ redis_client = redis.from_url(redis_url, decode_responses=True, socket_timeout=5, socket_connect_timeout=5)
63
+ # Test Redis connection with timeout
64
+ await asyncio.wait_for(redis_client.ping(), timeout=5.0)
65
  logging.info("Redis connected successfully")
66
  except Exception as redis_error:
67
  logging.warning(f"Redis connection failed: {redis_error}")
 
207
  os.environ.setdefault(env_var, path)
208
  os.makedirs(path, exist_ok=True)
209
 
210
+ # Global exception handler with memory cleanup
211
  @app.exception_handler(Exception)
212
  async def global_exception_handler(request, exc):
213
  logging.error(f"Unhandled error: {str(exc)}", exc_info=True)
214
+
215
+ # Clean up memory on errors
216
+ try:
217
+ import gc
218
+ gc.collect()
219
+ if torch.cuda.is_available():
220
+ torch.cuda.empty_cache()
221
+ except Exception:
222
+ pass
223
+
224
  return JSONResponse(
225
  status_code=500,
226
  content={"error": str(exc), "status": "error"}
 
506
  summ_loader = model_manager.get_model_loader(_mc.get_default_model("summarization"), "summarization")
507
  summarizer_agent = SummarizerAgent(summ_loader)
508
  logging.info("SummarizerAgent initialized with bart-base")
509
+ except Exception as e:
510
+ logging.warning(f"Failed to load summarization model: {e}")
511
  from .utils.model_loader_gguf import create_fallback_pipeline
512
 
513
  class FallbackSummarizer:
514
  def generate(self, text, **kwargs):
515
+ try:
516
+ return create_fallback_pipeline().generate_full_summary(text)
517
+ except Exception as fallback_error:
518
+ logging.error(f"Fallback summarizer failed: {fallback_error}")
519
+ return f"Summarization failed: {str(fallback_error)}"
520
 
521
  summarizer_agent = SummarizerAgent(FallbackSummarizer())
522
 
 
525
  med_generator = med_loader.load()
526
  medical_data_extractor_agent = MedicalDataExtractorAgent(med_generator)
527
  logging.info("MedicalDataExtractorAgent initialized with distilgpt2")
528
+ except Exception as e:
529
+ logging.warning(f"Failed to load medical extraction model: {e}")
530
  from .utils.model_loader_gguf import create_fallback_pipeline
531
 
532
  class FallbackExtractor:
533
  def generate(self, prompt, **kwargs):
534
+ try:
535
+ return create_fallback_pipeline().generate(prompt)
536
+ except Exception as fallback_error:
537
+ logging.error(f"Fallback extractor failed: {fallback_error}")
538
+ return f"Medical extraction failed: {str(fallback_error)}"
539
 
540
  medical_data_extractor_agent = MedicalDataExtractorAgent(FallbackExtractor())
541
 
 
641
  logging.info("Agents initialized and routes registered")
642
  except Exception as e:
643
  logging.error(f"Failed to register routes: {e}")
644
+ import traceback
645
+ logging.error(f"Route registration traceback: {traceback.format_exc()}")
646
+
647
  # Add basic health endpoint as fallback
648
  @app.get("/health")
649
  async def basic_health():
 
652
  @app.get("/")
653
  async def root():
654
  return {"message": "Medical AI Service - Limited functionality", "status": "degraded"}
655
+
656
+ @app.get("/docs")
657
+ async def docs():
658
+ return {"message": "API documentation not available in degraded mode"}
659
+
660
+ @app.get("/redoc")
661
+ async def redoc():
662
+ return {"message": "ReDoc not available in degraded mode"}
663
 
664
  # Mark the app as ready for readiness checks
665
  try: