This directory houses the highly advanced Computer Vision, OCR, and PDF parsing engines for Medibot V2. It processes visual imagery (X-Rays, MRIs, physical documents) and raw lab reports to generate standardized, triage-scored clinical master reports.
- Framework: FastAPI.
- Role: The
POST /analyzeendpoint is the central gateway. It rapidly identifies the file format (Fast-Fail validation) and routes it to the appropriate ML engine. - Event-Loop Optimized: Defined as a synchronous
def(instead ofasync def) to guarantee FastAPI correctly offloads the heavy Image analysis to a background thread pool, preventing the server from freezing for concurrent users. - Strict Memory Safety: The entire pipeline executes within a
try...finallyblock. Temporary uploaded bytes (temp/) are guaranteed to be purged from disk immediately after processing, completely preventing storage leak vulnerabilities. - Database Tracking: Successfully parses results and writes verdicts into a
medibot.dbSQLite schema.
- Medical Context Validator: Prevents adversarial uploads (e.g., Resumes, Bank Statements) by scanning the first 2,000 characters of a PDF for strict clinical terminology (
pathology,ultrasound,dosage). Requires at least 2 distinct metrics to pass, instantly rejecting fake documents with a 400 response. - Expanded Prescription Parsing: Standard prescriptions are dynamically identified through a widened net of OCR keywords (
Tab, Cap, Daily, TDS, Ointment, etc.), ensuring any handwritten or printed medication list successfully generates the Medication Ledger.
ai_vision.py&magic_lens.py: Evaluates DICOM/Image files, establishes Confidence thresholds, and maps pixel-density to generate a visual Heatmap of anomalies.handwriting.py&doc_to_text.py: Pre-processes written scripts or PDFs into clean text strings.lab_parser.py: The heart of the extraction system. Employs regex biomarker matching to locate components (HbA1c, Glucose, etc.) and evaluates them recursively against a 4-Tier severity matrix.integrator.py: Compiles Vision Data and Lab Data to calculate the final Master Severity Score (1-4).report_generator.py: Packages the master diagnosis, heatmaps, and structured prescription grids into a polished visual PDF.
- Ensure all system dependencies are installed (
fastapi,uvicorn,opencv-python,reportlab, etc.). - Run the server using Uvicorn (usually on port 8001 or as specified in your macro orchestration):
uvicorn app.api:app --host 0.0.0.0 --port 8001