Skip to content

Repository files navigation

Medibot V2 - Medical Scanner & Reporting Backend

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.

System Architecture

1. Main API (app/api.py)

  • Framework: FastAPI.
  • Role: The POST /analyze endpoint 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 of async 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...finally block. 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.db SQLite schema.

2. Security & Context Filters

  • 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.

3. Core ML Engines (core/)

  • 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.

Setup & Running

  1. Ensure all system dependencies are installed (fastapi, uvicorn, opencv-python, reportlab, etc.).
  2. 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

About

The computer vision and document processing module for analyzing medical records within the Medibot ecosystem.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages