How saurabhdvdi/medical-report-analyzer works
Purpose and Entry Points
Medical Report Analyzer is a full-stack, role-based clinical-record platform. It ingests PDF/image lab reports, converts them into structured lab values, supports longitudinal analytics and doctor-patient consent, and exposes a grounded AI assistant. Backend startup and router assembly are in backend/main.py; the SPA starts at frontend/src/main.jsx and routes/authenticates through frontend/src/App.jsx.
Client Application Boundary
React/Vite provides separate patient and doctor workflows: report management, health dashboards, medicine tracking, doctor discovery, and authorized patient review. frontend/src/contexts/AuthContext.jsx owns session/JWT state; API-facing feature modules include frontend/src/services/reportService.js, frontend/src/services/analyticsService.js, and frontend/src/services/aiService.js. Role-gated navigation/view access is centered in frontend/src/components/RoleRoute.jsx.
API, Identity, and Persistence
FastAPI exposes modular REST domains through backend/routes/auth.py, backend/routes/reports.py, backend/routes/analytics.py, backend/routes/patient.py, and related routers. JWT authentication, password hashing, and request identity dependencies are in backend/auth.py and backend/core/security.py. SQLAlchemy database/session lifecycle is in backend/database.py; core clinical, user, access-grant, report, lab-value, medicine, and note entities are defined in backend/models.py with additional artery modeling in backend/models/artery.py. SQLite is the default datastore; PyMySQL/MySQL is an optional deployment target.
Report Ingestion Pipeline
Upload handling persists the original report and drives extraction: multi-engine OCR reads PDFs/images, parser stages identify candidate labs, normalization standardizes values/units/ranges, and validation/classification marks usable or abnormal results. Primary stages reside in backend/services/ocr_service.py, backend/services/report_parser.py, backend/services/lab_candidate_extractor.py, backend/services/normalizer.py, and backend/services/lab_validator.py. OCR depends on PyTesseract, EasyOCR, PyMuPDF, and optionally Poppler; ingestion may invoke LLM summarization through backend/ai/llm_service.py.
Analytics and Clinical Views
Structured LabValue history feeds deterministic comparison, trend analysis, risk scoring, and generated observations used by dashboards and the AI tool layer. Key services are backend/services/analytics_service.py, backend/services/comparison_service.py, backend/services/risk_engine.py, and backend/services/insights.py. The frontend renders this data in patient/doctor dashboards and trend-oriented pages, notably frontend/src/pages/PatientDashboard.jsx, frontend/src/pages/DoctorInterface.jsx, and frontend/src/pages/HealthTrendsPage.jsx.
Consent Boundary and Collaboration
Patients and doctors are distinct roles. Doctor access to a patient requires an approved patient-doctor access relationship; revoked, pending, or rejected relationships must not expose patient records. This boundary is applied in authorization logic and domain routes, especially backend/auth.py, backend/routes/doctor.py, and backend/routes/patient.py. Doctor profiles/taxonomy, patient discovery, access lifecycle, and clinical notes are supported by backend/routes/doctor.py, backend/routes/doctor_notes.py, and backend/services/doctor_taxonomy_seed.py.
AI Agent and MCP Execution
backend/routes/ai_routes.py accepts chat and comparison requests and invokes the LangGraph-based assistant in backend/ai/agent.py. The agent establishes a security-scoped target, classifies intent, selects MCP tools, gathers grounded context/RAG, generates via the configured LLM, then returns citations and suggested follow-ups. backend/mcp/tools.py defines the security-aware tool registry and backend/mcp/client.py executes it; backend/ai/rag_service.py and backend/ai/suggestion_service.py supply context and next questions. LLM abstraction in backend/ai/llm_service.py supports local Ollama or Groq cloud models.
Deployment Runtime
The system runs Python/FastAPI/Uvicorn and a Node-built React SPA. Docker definitions exist at backend/Dockerfile, frontend/Dockerfile, docker-compose.yml, and docker-compose.prod.yml; Nginx reverse-proxy/static serving configuration is nginx/nginx.conf. External runtime dependencies include an LLM endpoint (Ollama or Groq), OCR binaries/services, and SQLite or MySQL.
Components 22
React SPA
- React/Vite app · Patient and doctor workflows.
frontend/src/main.jsx - Session and role gates
frontend/src/contexts/AuthContext.jsx - API service modules · Reports, analytics, and AI requests.
frontend/src/services/reportService.js - Clinical dashboards · Patient, doctor, and trend views.
frontend/src/pages/PatientDashboard.jsx
FastAPI API
- FastAPI application · Startup and modular router registration.
backend/main.py - JWT identity and consent · Enforces roles and approved doctor-patient access.
backend/auth.py - Clinical REST routes · Reports, analytics, patient, doctor, and notes domains.
backend/routes/reports.py - AI chat routes
backend/routes/ai_routes.py - SQLAlchemy models · Users, records, grants, reports, lab values, medicines, and notes.
backend/models.py
Report ingestion
- OCR extraction · Reads uploaded PDF and image reports with multiple engines.
backend/services/ocr_service.py - Lab candidate parsing · Identifies candidate lab results.
backend/services/lab_candidate_extractor.py - Normalize and validate · Standardizes units/ranges and classifies usable or abnormal values.
backend/services/normalizer.py
Clinical intelligence
- Analytics pipeline · Comparison, trends, risk scoring, and observations.
backend/services/analytics_service.py - Risk scoring
backend/services/risk_engine.py
AI assistant
- LangGraph assistant · Scopes target, classifies intent, and returns grounded answers.
backend/ai/agent.py - Security-aware MCP tools
backend/mcp/tools.py - Grounded context
backend/ai/rag_service.py - LLM abstraction · Uses local Ollama or Groq.
backend/ai/llm_service.py
Runtime dependencies
- SQLite / MySQL · SQLite default; MySQL optional.
- OCR engines · Tesseract, EasyOCR, PyMuPDF, and optional Poppler.
- Ollama or Groq
- Nginx deployment edge · Serves the SPA and proxies application traffic.
nginx/nginx.conf
Connections 29
- React/Vite app → Session and role gates: initializes
- React/Vite app → API service modules: uses
- React/Vite app → Clinical dashboards: renders
- Session and role gates → JWT identity and consent: JWT
- API service modules → FastAPI application: REST requests
- Clinical dashboards → API service modules: loads data
- Nginx deployment edge → React/Vite app: serves
- Nginx deployment edge → FastAPI application: proxies
- FastAPI application → Clinical REST routes: mounts
- FastAPI application → AI chat routes: mounts
- Clinical REST routes → JWT identity and consent: authorizes
- Clinical REST routes → OCR extraction: uploads report
- Clinical REST routes → Analytics pipeline: requests insights
- JWT identity and consent → SQLAlchemy models: checks identities and grants
- SQLAlchemy models → SQLite / MySQL: persists
- OCR extraction → OCR engines: uses
- OCR extraction → Lab candidate parsing: extracted text
- Lab candidate parsing → Normalize and validate: candidates
- Normalize and validate → SQLAlchemy models: stores reports and lab values
- SQLAlchemy models → Analytics pipeline: lab history
- Analytics pipeline → Risk scoring: calculates
- AI chat routes → JWT identity and consent: scopes requester
- AI chat routes → LangGraph assistant: chat and comparison
- LangGraph assistant → Security-aware MCP tools: selects tools
- Security-aware MCP tools → SQLAlchemy models: retrieves authorized records
- LangGraph assistant → Grounded context: gathers context
- Grounded context → SQLAlchemy models: grounds in clinical data
- LangGraph assistant → LLM abstraction: generates response
- LLM abstraction → Ollama or Groq: invokes