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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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""
Medical Report Analysis Platform - Main Backend Application
Comprehensive AI-powered medical document analysis with multi-model processing
With HIPAA/GDPR Security & Compliance Features
"""

from fastapi import FastAPI, File, UploadFile, HTTPException, BackgroundTasks, Request, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from pathlib import Path
from typing import List, Dict, Optional, Any
import os
import tempfile
import logging
from datetime import datetime
import uuid

# Import processing modules
from pdf_processor import PDFProcessor
from document_classifier import DocumentClassifier
from model_router import ModelRouter
from analysis_synthesizer import AnalysisSynthesizer
from security import get_security_manager, ComplianceValidator, DataEncryption

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Initialize FastAPI app
app = FastAPI(
    title="Medical Report Analysis Platform",
    description="HIPAA/GDPR Compliant AI-powered medical document analysis",
    version="2.0.0"
)

# CORS configuration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Configure appropriately for production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount static files (frontend)
static_dir = Path(__file__).parent / "static"
if static_dir.exists():
    app.mount("/assets", StaticFiles(directory=static_dir / "assets"), name="assets")
    logger.info("Static files mounted successfully")

# Initialize processing components
pdf_processor = PDFProcessor()
document_classifier = DocumentClassifier()
model_router = ModelRouter()
analysis_synthesizer = AnalysisSynthesizer()

# Initialize security components
security_manager = get_security_manager()
compliance_validator = ComplianceValidator()
data_encryption = DataEncryption()

logger.info("Security and compliance features initialized")

# Request/Response Models
class AnalysisStatus(BaseModel):
    job_id: str
    status: str
    progress: float
    message: str

class AnalysisResult(BaseModel):
    job_id: str
    document_type: str
    confidence: float
    analysis: Dict[str, Any]
    specialized_results: List[Dict[str, Any]]
    summary: str
    timestamp: str

class HealthCheck(BaseModel):
    status: str
    version: str
    timestamp: str

# In-memory job tracking (use Redis/database in production)
job_tracker: Dict[str, Dict[str, Any]] = {}


@app.get("/api", response_model=HealthCheck)
async def api_root():
    """API health check endpoint"""
    return HealthCheck(
        status="healthy",
        version="1.0.0",
        timestamp=datetime.utcnow().isoformat()
    )


@app.get("/")
async def root():
    """Serve frontend"""
    static_dir = Path(__file__).parent / "static"
    index_file = static_dir / "index.html"
    
    if index_file.exists():
        return FileResponse(index_file)
    else:
        return {"message": "Medical Report Analysis Platform API", "version": "1.0.0"}


@app.get("/health")
async def health_check():
    """Detailed health check with component status"""
    return {
        "status": "healthy",
        "components": {
            "pdf_processor": "ready",
            "classifier": "ready",
            "model_router": "ready",
            "synthesizer": "ready",
            "security": "ready",
            "compliance": "active"
        },
        "timestamp": datetime.utcnow().isoformat()
    }


@app.get("/compliance-status")
async def get_compliance_status():
    """Get HIPAA/GDPR compliance status"""
    return compliance_validator.check_compliance()


@app.post("/auth/login")
async def login(email: str, password: str):
    """
    User authentication endpoint
    In production, validate credentials against secure database
    """
    # Demo authentication - in production, validate against database
    logger.warning("Demo authentication - implement secure auth in production")
    
    # For demo, accept any credentials
    user_id = str(uuid.uuid4())
    token = security_manager.create_access_token(user_id, email)
    
    return {
        "access_token": token,
        "token_type": "bearer",
        "user_id": user_id,
        "email": email
    }


@app.post("/analyze", response_model=AnalysisStatus)
async def analyze_document(
    request: Request,
    file: UploadFile = File(...),
    background_tasks: BackgroundTasks = BackgroundTasks(),
    current_user: Dict[str, Any] = Depends(security_manager.get_current_user)
):
    """
    Upload and analyze a medical document with audit logging
    
    This endpoint initiates the two-layer processing:
    - Layer 1: PDF extraction and classification
    - Layer 2: Specialized model analysis
    
    Security: Logs all PHI access for HIPAA compliance
    """
    
    # Generate unique job ID
    job_id = str(uuid.uuid4())
    
    # Audit log: Document upload
    client_ip = request.client.host if request.client else "unknown"
    security_manager.audit_logger.log_phi_access(
        user_id=current_user.get("user_id", "unknown"),
        document_id=job_id,
        action="UPLOAD",
        ip_address=client_ip
    )
    
    # Validate file type
    if not file.filename.lower().endswith('.pdf'):
        raise HTTPException(
            status_code=400,
            detail="Only PDF files are supported"
        )
    
    # Initialize job tracking
    job_tracker[job_id] = {
        "status": "processing",
        "progress": 0.0,
        "filename": file.filename,
        "user_id": current_user.get("user_id"),
        "created_at": datetime.utcnow().isoformat()
    }
    
    try:
        # Save uploaded file temporarily
        with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
            content = await file.read()
            tmp_file.write(content)
            tmp_file_path = tmp_file.name
        
        # Schedule background processing
        background_tasks.add_task(
            process_document_pipeline,
            job_id,
            tmp_file_path,
            file.filename,
            current_user.get("user_id")
        )
        
        logger.info(f"Analysis job {job_id} created for file: {file.filename}")
        
        return AnalysisStatus(
            job_id=job_id,
            status="processing",
            progress=0.0,
            message="Document uploaded successfully. Analysis in progress."
        )
        
    except Exception as e:
        logger.error(f"Error creating analysis job: {str(e)}")
        job_tracker[job_id]["status"] = "failed"
        job_tracker[job_id]["error"] = str(e)
        
        # Audit log: Failed upload
        security_manager.audit_logger.log_access(
            user_id=current_user.get("user_id", "unknown"),
            action="UPLOAD_FAILED",
            resource=f"document:{job_id}",
            ip_address=client_ip,
            status="FAILED",
            details={"error": str(e)}
        )
        
        raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")


@app.get("/status/{job_id}", response_model=AnalysisStatus)
async def get_analysis_status(job_id: str):
    """Get the current status of an analysis job"""
    
    if job_id not in job_tracker:
        raise HTTPException(status_code=404, detail="Job not found")
    
    job_data = job_tracker[job_id]
    
    return AnalysisStatus(
        job_id=job_id,
        status=job_data["status"],
        progress=job_data.get("progress", 0.0),
        message=job_data.get("message", "Processing...")
    )


@app.get("/results/{job_id}", response_model=AnalysisResult)
async def get_analysis_results(job_id: str):
    """Retrieve the analysis results for a completed job"""
    
    if job_id not in job_tracker:
        raise HTTPException(status_code=404, detail="Job not found")
    
    job_data = job_tracker[job_id]
    
    if job_data["status"] != "completed":
        raise HTTPException(
            status_code=400,
            detail=f"Analysis not completed. Current status: {job_data['status']}"
        )
    
    return AnalysisResult(**job_data["result"])


@app.get("/supported-models")
async def get_supported_models():
    """Get list of supported medical AI models by domain"""
    return {
        "domains": {
            "clinical_notes": {
                "models": ["MedGemma 27B", "Bio_ClinicalBERT"],
                "tasks": ["summarization", "entity_extraction", "coding"]
            },
            "radiology": {
                "models": ["MedGemma 4B Multimodal", "MONAI"],
                "tasks": ["vqa", "report_generation", "segmentation"]
            },
            "pathology": {
                "models": ["Path Foundation", "UNI2-h"],
                "tasks": ["slide_classification", "embedding_generation"]
            },
            "cardiology": {
                "models": ["HuBERT-ECG"],
                "tasks": ["ecg_analysis", "event_prediction"]
            },
            "laboratory": {
                "models": ["DrLlama", "Lab-AI"],
                "tasks": ["normalization", "explanation"]
            },
            "drug_interactions": {
                "models": ["CatBoost DDI", "DrugGen"],
                "tasks": ["interaction_classification"]
            },
            "diagnosis": {
                "models": ["MedGemma 27B"],
                "tasks": ["differential_diagnosis", "triage"]
            },
            "coding": {
                "models": ["Rayyan Med Coding", "ICD-10 Predictors"],
                "tasks": ["icd10_extraction", "cpt_coding"]
            },
            "mental_health": {
                "models": ["MentalBERT"],
                "tasks": ["screening", "sentiment_analysis"]
            }
        }
    }


async def process_document_pipeline(job_id: str, file_path: str, filename: str, user_id: str = "unknown"):
    """
    Background task for processing medical documents through the full pipeline
    
    Pipeline stages:
    1. PDF Extraction (text, images, tables)
    2. Document Classification
    3. Intelligent Routing
    4. Specialized Model Analysis
    5. Result Synthesis
    
    Security: All stages logged for HIPAA compliance
    """
    
    try:
        # Stage 1: PDF Processing
        job_tracker[job_id]["progress"] = 0.1
        job_tracker[job_id]["message"] = "Extracting content from PDF..."
        logger.info(f"Job {job_id}: Starting PDF extraction")
        
        pdf_content = await pdf_processor.extract_content(file_path)
        
        # Stage 2: Document Classification
        job_tracker[job_id]["progress"] = 0.3
        job_tracker[job_id]["message"] = "Classifying document type..."
        logger.info(f"Job {job_id}: Classifying document")
        
        classification = await document_classifier.classify(pdf_content)
        
        # Audit log: Classification complete
        security_manager.audit_logger.log_phi_access(
            user_id=user_id,
            document_id=job_id,
            action="CLASSIFY",
            ip_address="internal"
        )
        
        # Stage 3: Model Routing
        job_tracker[job_id]["progress"] = 0.4
        job_tracker[job_id]["message"] = "Routing to specialized models..."
        logger.info(f"Job {job_id}: Routing to models - {classification['document_type']}")
        
        model_tasks = model_router.route(classification, pdf_content)
        
        # Stage 4: Specialized Analysis
        job_tracker[job_id]["progress"] = 0.5
        job_tracker[job_id]["message"] = "Running specialized analysis..."
        logger.info(f"Job {job_id}: Running {len(model_tasks)} specialized models")
        
        specialized_results = []
        for i, task in enumerate(model_tasks):
            result = await model_router.execute_task(task)
            specialized_results.append(result)
            progress = 0.5 + (0.3 * (i + 1) / len(model_tasks))
            job_tracker[job_id]["progress"] = progress
        
        # Stage 5: Result Synthesis
        job_tracker[job_id]["progress"] = 0.9
        job_tracker[job_id]["message"] = "Synthesizing results..."
        logger.info(f"Job {job_id}: Synthesizing results")
        
        final_analysis = await analysis_synthesizer.synthesize(
            classification,
            specialized_results,
            pdf_content
        )
        
        # Complete
        job_tracker[job_id]["progress"] = 1.0
        job_tracker[job_id]["status"] = "completed"
        job_tracker[job_id]["message"] = "Analysis complete"
        job_tracker[job_id]["result"] = {
            "job_id": job_id,
            "document_type": classification["document_type"],
            "confidence": classification["confidence"],
            "analysis": final_analysis,
            "specialized_results": specialized_results,
            "summary": final_analysis.get("summary", ""),
            "timestamp": datetime.utcnow().isoformat()
        }
        
        logger.info(f"Job {job_id}: Analysis completed successfully")
        
        # Audit log: Analysis complete
        security_manager.audit_logger.log_phi_access(
            user_id=user_id,
            document_id=job_id,
            action="ANALYSIS_COMPLETE",
            ip_address="internal"
        )
        
        # Secure cleanup of temporary file
        data_encryption.secure_delete(file_path)
        
    except Exception as e:
        logger.error(f"Job {job_id}: Analysis failed - {str(e)}")
        job_tracker[job_id]["status"] = "failed"
        job_tracker[job_id]["message"] = f"Analysis failed: {str(e)}"
        job_tracker[job_id]["error"] = str(e)
        
        # Audit log: Analysis failed
        security_manager.audit_logger.log_access(
            user_id=user_id,
            action="ANALYSIS_FAILED",
            resource=f"document:{job_id}",
            ip_address="internal",
            status="FAILED",
            details={"error": str(e)}
        )
        
        # Cleanup on error
        if os.path.exists(file_path):
            data_encryption.secure_delete(file_path)


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)