You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
graph TB
subgraph "Frontend Layer"
A[Next.js 14 App Router]
A1[Doctor Dashboard]
A2[Patient Portal]
A3[Research Assistant UI]
A --> A1
A --> A2
A --> A3
end
subgraph "Authentication"
B[Clerk Auth]
B --> B1[Role-Based Routing]
B --> B2[User Sync]
end
subgraph "Backend API Layer"
C[Next.js API Routes]
C1[/api/upload]
C2[/api/ml-proxy]
C3[/api/ai]
C4[/api/doctor]
C5[/api/conversations]
C6[/api/research]
C7[/api/ocr]
C8[/api/medications]
C --> C1
C --> C2
C --> C3
C --> C4
C --> C5
C --> C6
C --> C7
C --> C8
end
subgraph "ML Service Layer - FastAPI :8000"
D[FastAPI Server]
D1[POST /predict β Image Inference]
D2[POST /ocr/extract β OCR Processing]
D3[POST /ocr/clean-report β Prescription Digitization]
D4[POST /ocr/prescriptions-only β Medication Extraction]
D5[POST /research/ask β Research Q&A]
D6[POST /research/crawl-latest β Web Scraping]
D7[GET /research/stats]
D8[GET /research/export-qa]
D --> D1
D --> D2
D --> D3
D --> D4
D --> D5
D --> D6
D --> D7
D --> D8
end
subgraph "ML Models β Unified Checkpoint"
E[ModalityRouter β ResNet34]
F1[BrainExpert β EfficientNetB2]
F2[LungExpert β DenseNet121]
F3[SkinExpert β ResNet50]
F4[ECGExpert β EfficientNetB0]
F5[AudioExpert β AST/ResNet50]
E --> F1
E --> F2
E --> F3
E --> F4
E --> F5
end
subgraph "OCR Pipeline"
G1[Google Cloud Vision API β Primary]
G2[Multi-pass Tesseract β Fallback]
G1 -.-> G4[Groq LLM β Text Cleaning]
G2 -.-> G4
end
subgraph "Research System"
H1[Firecrawl v4 β Web Scraper]
H2[BM25 β Keyword Search]
H3[TF-IDF β Cosine Similarity]
H4[JSON Article Store]
H1 --> H4
H4 --> H2
H4 --> H3
H2 -.-> H5[Groq LLM β RAG Answers]
H3 -.-> H5
end
subgraph "Data Storage"
J[SQLite Database β Drizzle ORM]
J1[Users, Profiles & Hospitals (Tenancy)]
J2[Cases, Artifacts & Immutable Reports]
J3[Prescriptions & Medications]
J4[Conversations & Messages]
J5[Appointments & Follow-Ups]
J6[Exercise Routines & Logs]
J7[Notifications]
J --> J1
J --> J2
J --> J3
J --> J4
J --> J5
J --> J6
J --> J7
end
subgraph "External APIs"
K1[Groq API β LLaMA 3.3 70B]
K2[Firecrawl API β Web Scraping]
K3[Google Cloud Vision API β OCR]
end
A --> B
B --> C
C --> D
D --> E
D --> G1
D --> G2
D --> H1
C --> J
D --> K1
H1 --> K2
G4 --> K1
H5 --> K1
G1 --> K3
classDef frontend fill:#a8e6cf,stroke:#333,stroke-width:2px
classDef backend fill:#ffd3b6,stroke:#333,stroke-width:2px
classDef ml fill:#ffaaa5,stroke:#333,stroke-width:2px
classDef db fill:#a8dadc,stroke:#333,stroke-width:2px
classDef external fill:#e0aaff,stroke:#333,stroke-width:2px
class A,A1,A2,A3 frontend
class B,C,C1,C2,C3,C4,C5,C6,C7,C8 backend
class D,E,F1,F2,F3,F4,G1,G2,G4,H1,H2,H3,H4,H5 ml
class J,J1,J2,J3,J4,J5,J6,J7 db
class K1,K2,K3 external
Loading
2. End-to-End Medical Imaging + RAG Pipeline (Detailed)
graph TB
subgraph "Step 1: Image Upload & Preprocessing"
A[Patient Uploads Medical Image]
A --> B{Image Type?}
B -->|DICOM| C[DICOM Parser]
B -->|JPG/PNG| D[Standard Image Loader]
C --> E[Resize to 224x224]
D --> E
E --> F[Normalize RGB - ImageNet Stats]
F --> G[Tensor Conversion]
end
subgraph "Step 2: Modality Routing"
G --> H[ModalityRouter - ResNet34]
H --> I{Which Expert?}
I -->|Brain Case| J1[Route to BrainExpert]
I -->|Lung Case| J2[Route to LungExpert]
I -->|Skin Case| J3[Route to SkinExpert]
I -->|ECG Case| J4[Route to ECGExpert]
end
subgraph "Step 3: Expert Model Inference"
J1 --> K1[BrainExpert - EfficientNetB2]
J2 --> K2[LungExpert - DenseNet121]
J3 --> K3[SkinExpert - ResNet50]
J4 --> K4[ECGExpert - EfficientNetB0]
K1 --> L[MC Dropout Sampling - 25 Forward Passes]
K2 --> L
K3 --> L
K4 --> L
L --> M[Mean Prediction]
L --> N[Uncertainty Calculation - Std Dev]
N --> O{Uncertainty > 0.15?}
O -->|Yes| P[REJECT - Too Uncertain]
O -->|No| Q[ACCEPT - Proceed]
end
subgraph "Step 4: Explainability - GradCAM"
Q --> R[Backpropagate to Last Conv Layer]
R --> S[Compute Gradient Weights]
S --> T[Weight Feature Maps]
T --> U[ReLU Activation]
U --> V[Upsample to Original Size]
V --> W[Overlay Heatmap on Image]
W --> X[Export Heatmap PNG]
end
subgraph "Step 5: Embedding Extraction for RAG"
Q --> Y[Extract Features from Backbone]
Y --> Z[Embedding Head - FC Layer 2048β512]
Z --> AA[L2 Normalization]
AA --> AB[512-Dimensional Vector]
end
subgraph "Step 6: RAG - Similar Case Retrieval"
AB --> AC[FAISS Vector Database]
AC --> AD{Search Method}
AD -->|Cosine Similarity| AE[IndexFlatIP Search]
AE --> AF[Filter by Same Anatomy]
AF --> AG[Return Top-5 Neighbors]
AG --> AH[Similar Case #1 - 92% Match]
AG --> AI[Similar Case #2 - 89% Match]
AG --> AJ[Similar Case #3 - 87% Match]
AG --> AK[Similar Case #4 - 85% Match]
AG --> AL[Similar Case #5 - 82% Match]
end
subgraph "Step 7: Historical Case Metadata"
AH --> AM1[Case ID: 8821<br/>Diagnosis: Pneumonia<br/>Outcome: Recovered in 10 days<br/>Treatment: Antibiotics]
AI --> AM2[Case ID: 1029<br/>Diagnosis: Pneumonia<br/>Outcome: ICU admission required<br/>Treatment: IV antibiotics]
AJ --> AM3[Case ID: 4532<br/>Diagnosis: Bacterial Infection<br/>Outcome: Full recovery<br/>Treatment: Oral antibiotics]
AK --> AM4[Case ID: 7821<br/>Diagnosis: Viral Pneumonia<br/>Outcome: Self-limiting<br/>Treatment: Supportive care]
AL --> AM5[Case ID: 3291<br/>Diagnosis: Aspiration Pneumonia<br/>Outcome: Resolved<br/>Treatment: Antibiotics + PT]
end
subgraph "Step 8: Context Assembly for Groq"
Q --> AN[Current Diagnosis + Confidence]
X --> AO[Heatmap Evidence]
AM1 --> AP[Evidence Bundle]
AM2 --> AP
AM3 --> AP
AM4 --> AP
AM5 --> AP
AN --> AP
AO --> AP
AP --> AQ[Patient Demographics]
AP --> AR[Medical History]
AP --> AS[Symptoms]
AQ --> AT{Groq LLM Prompt}
AR --> AT
AS --> AT
end
subgraph "Step 9: Groq Report Generation"
AT --> AU[Groq API Call - llama-3.3-70b-versatile]
AU --> AV[System Prompt: Medical Report Writer]
AV --> AW[Evidence-Based Reasoning]
AW --> AX[Generate Structured Report]
AX --> AY[FINDINGS Section]
AX --> AZ[IMPRESSION Section]
AX --> BA[RECOMMENDATIONS Section]
AY --> BB[Dense consolidation in right lower lobe...<br/>Citing similar cases #8821, #1029]
AZ --> BC[1. Bacterial Pneumonia likely<br/>2. No pleural effusion<br/>3. Recommend antibiotics]
BA --> BD[Follow-up X-ray in 48-72 hours<br/>Blood cultures + sputum analysis]
end
subgraph "Step 10: Doctor Review & Finalization"
BB --> BE[Display to Doctor]
BC --> BE
BD --> BE
BE --> BF{Doctor Edits?}
BF -->|Yes| BG[Modify Findings/Impression]
BF -->|No| BH[Approve As-Is]
BG --> BI[Final Report]
BH --> BI
BI --> BJ[Doctor Signature + Timestamp]
BJ --> BK[Generate PDF with jsPDF]
BK --> BL[Save to Database]
BL --> BM[Notify Patient]
end
subgraph "Step 11: Feedback Loop - Update RAG"
BM --> BN[Store Final Diagnosis]
BN --> BO[Store Treatment Outcome]
BO --> BP[Extract Embedding]
BP --> BQ[Add to FAISS Index]
BQ --> BR[Knowledge Base Updated]
BR --> AC
end
classDef preprocessing fill:#e3f2fd,stroke:#1976d2,stroke-width:2px
classDef routing fill:#fff3e0,stroke:#f57c00,stroke-width:2px
classDef inference fill:#fce4ec,stroke:#c2185b,stroke-width:2px
classDef explainability fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
classDef rag fill:#e8f5e9,stroke:#388e3c,stroke-width:2px
classDef groq fill:#fff9c4,stroke:#f9a825,stroke-width:2px
classDef doctor fill:#e0f2f1,stroke:#00796b,stroke-width:2px
class A,B,C,D,E,F,G preprocessing
class H,I,J1,J2,J3,J4 routing
class K1,K2,K3,K4,L,M,N,O,P,Q inference
class R,S,T,U,V,W,X explainability
class Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM1,AM2,AM3,AM4,AM5 rag
class AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ,BA,BB,BC,BD groq
class BE,BF,BG,BH,BI,BJ,BK,BL,BM,BN,BO,BP,BQ,BR doctor
Loading
3. Research Assistant RAG Architecture
graph TB
subgraph "Step 1: Web Crawling β Firecrawl v4"
A[User Query or Scheduled Crawl]
A --> B[Firecrawl API β scrape method]
B --> C{Medical Sources}
C -->|Research| D1[PubMed Central]
C -->|News| D2[Medical News Today]
C -->|Guidelines| D3[WHO]
C -->|Clinical| D4[Mayo Clinic]
D1 --> E[Rate Limiter β 15/hr, 50/day]
D2 --> E
D3 --> E
D4 --> E
E --> F{Rate OK?}
F -->|Yes| G[Scrape URL to Markdown]
F -->|No| H[Return Cached or Skip]
G --> I[24-hour JSON Cache]
I --> J[Crawled Articles]
end
subgraph "Step 2: Content Processing & Indexing"
J --> K[Clean Markdown Artifacts]
K --> L[Chunk Text β 500 words, 100 overlap]
L --> M[Tokenize for BM25]
M --> N[JSON Article Store]
N --> O[Build BM25 Index]
end
subgraph "Step 3: Hybrid Search"
P[User Question] --> Q{Parallel Search}
Q -->|Path 1| R[BM25 Keyword Search]
R --> S[TF-IDF Ranked Results]
Q -->|Path 2| T[TF-IDF Cosine Similarity]
T --> U[Vector Similarity Results]
S --> V[Merge & Deduplicate]
U --> V
V --> W[Top-K Most Relevant Chunks]
end
subgraph "Step 4: Auto-Crawl if Needed"
W --> X{Results < 2?}
X -->|Yes| Y[Crawl Medical Sources for Query]
Y --> Z[Index New Articles]
Z --> W
X -->|No| AA[Proceed to RAG]
end
subgraph "Step 5: Groq RAG Answer Generation"
AA --> AB[Build Context from Top-K Chunks]
AB --> AC[System Prompt β Medical Research Assistant]
AC --> AD[Groq API β llama-3.3-70b-versatile]
AD --> AE[Answer with Source Citations]
end
subgraph "Step 6: Response Assembly"
AE --> AF[Formatted Answer β Markdown]
AF --> AG[Source Cards with Relevance Scores]
AG --> AH[Confidence Level β high/medium/low]
AH --> AI[Log Q&A to History]
AI --> AJ[Display to User]
end
classDef crawling fill:#e3f2fd,stroke:#1976d2,stroke-width:2px
classDef indexing fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
classDef search fill:#e8f5e9,stroke:#388e3c,stroke-width:2px
classDef groq fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px
classDef display fill:#e0f2f1,stroke:#00796b,stroke-width:2px
class A,B,C,D1,D2,D3,D4,E,F,G,H,I,J crawling
class K,L,M,N,O indexing
class P,Q,R,S,T,U,V,W,X,Y,Z,AA search
class AB,AC,AD,AE groq
class AF,AG,AH,AI,AJ display
Loading
π οΈ Technology Stack
Frontend
Technology
Version
Purpose
Next.js
14.2.x
React framework with App Router
React
18.x
UI component library
TypeScript
5.x
Type-safe development
Tailwind CSS
3.3.0
Utility-first styling
Framer Motion
12.28.1
Animations & transitions
Clerk
6.36.9
Authentication & user management
Lucide React
0.562.0
Icon library
Spline
4.1.0
3D graphics for marketing page
jsPDF
4.1.0
PDF generation
Recharts
3.7.0
Data visualization
SWR
2.4.0
Data fetching & caching
Radix UI
latest
Accessible UI primitives
Backend
Technology
Version
Purpose
Next.js API Routes
14.2.x
REST API endpoints
Neon PostgreSQL
Serverless
Cloud Edge Database
@neondatabase/serverless
latest
Edge WebSocket SQL Driver
Drizzle ORM
0.45.1
Type-safe database ORM
FastAPI
0.104+
Python ML inference server
Uvicorn
0.24+
ASGI server
Machine Learning
Technology
Version
Purpose
PyTorch
2.1+
Deep learning framework
torchvision
0.16+
Pre-trained model architectures
timm
0.9.12+
EfficientNet model zoo
OpenCV
4.8+
Image processing & heatmap overlay
Pillow
10.1+
Image loading & preprocessing
NumPy
1.26+
Numerical computing
OCR Pipeline
Technology
Version
Purpose
Google Cloud Vision API
3.5+
Primary OCR β handwritten + printed
pytesseract
0.3.10+
Fallback OCR engine
pdf2image
1.16+
PDF to image conversion
OpenCV
4.8+
Image preprocessing (binarize, deskew)
Research System
Technology
Version
Purpose
Firecrawl
v4.12+
Web scraping (markdown extraction)
rank-bm25
0.2.2
BM25 keyword search
TF-IDF
built-in
Cosine similarity fallback
JSON store
β
Article & chunk persistence
AI/LLM
Technology
Version
Purpose
Groq API
latest
Fast LLM inference
LLaMA 3.3 70B Versatile
β
Report generation, OCR cleaning, research Q&A, chat
POST /research/ask
Content-Type: application/x-www-form-urlencoded
Body:
query=What are the latest developments in AI for chest X-ray analysis?
top_k=5
crawl_if_empty=true
Response:
{
"status": "success",
"question": "What are the latest developments...",
"answer": "Recent studies show AI accuracy has improved significantly...",
"sources": [
{
"title": "PubMed Central - AI Radiology Research",
"url": "https://pubmed.ncbi.nlm.nih.gov/?term=...",
"source_name": "PubMed Central",
"relevance_score": 0.94
}
],
"method": "hybrid_search+rag",
"confidence": "high",
"crawl_performed": false
}
git clone https://github.com/atharvavdeo/VaidyaVision---IIIT-Pune.git
cd VaidyaVision---IIIT-Pune
Step 2: Frontend Setup
cd medical-ai-platform
# Install dependencies
npm install
# Create environment file
cp .env.example .env.local
# Edit .env.local with your keys:# NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_...# CLERK_SECRET_KEY=sk_test_...# GROQ_API_KEY=gsk_...# GOOGLE_API_KEY=... (for OCR)# FIRECRAWL_API_KEY=fc-... (for research)# Initialize database
npx drizzle-kit generate
npx drizzle-kit migrate
# Seed demo data
npm run db:seed
# Start dev server
npm run dev
# Open http://localhost:3000
Step 3: ML Service Setup
cd ml-service
# Create virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate# Install dependencies
pip install -r requirements.txt
# Ensure the unified model checkpoint exists:# ml-service/medical_ai_system_final.pth (255MB)# Start ML server with API keys
GROQ_API_KEY=gsk_... \
FIRECRAWL_API_KEY=fc-... \
GOOGLE_API_KEY=... \
python server.py
# Server runs on http://localhost:8000# API docs: http://localhost:8000/docs
Step 4: Verify Installation
# Test ML health
curl http://localhost:8000/
# Test inference (replace with actual image)
curl -X POST http://localhost:8000/predict \
-F "file=@test_xray.jpg" \
-F "modality=lung"# Test OCR
curl -X POST http://localhost:8000/ocr/extract \
-F "file=@prescription.jpg"# Test Research
curl -X POST http://localhost:8000/research/ask \
-F "query=What is deep learning in medical imaging?"
1. Login β Doctor Dashboard
2. View pending scans (sorted by priority β critical first)
3. Click scan β Review AI prediction
β’ Diagnosis: "Bacterial Pneumonia" β 94% confidence
β’ GradCAM heatmap highlighting affected region
β’ Uncertainty: 0.08 (well below 0.15 threshold)
4. Click "Generate Report" β Groq LLM creates draft
β’ FINDINGS: Dense consolidation in right lower lobe...
β’ IMPRESSION: 1. Bacterial Pneumonia 2. No pleural effusion
β’ RECOMMENDATIONS: Broad-spectrum antibiotics, follow-up in 48-72 hours
5. Edit findings/impression if needed
6. Sign report β PDF auto-downloads with letterhead
7. Patient notified automatically
2. Patient Workflow
1. Login β Patient Dashboard
2. Upload chest X-ray + describe symptoms
3. Wait for doctor review (notification sent when ready)
4. View results: diagnosis, report, download PDF
5. Upload prescription photo β OCR extracts medications
β’ Drug names, dosages, frequencies extracted
β’ Smart timing reminders generated (8 AM, 2 PM, 8 PM)
6. Track daily medication intake (taken/missed/skipped)
7. Log exercise routines prescribed by doctor
8. Chat with doctor if questions arise
9. Book follow-up appointment
10. Play Pill Tic-Tac-Toe while waiting! π
3. Research Assistant
1. Navigate to Research page (both doctor & patient)
2. Ask: "What are the latest developments in AI for radiology?"
3. System:
a. Searches indexed articles (BM25 + TF-IDF)
b. If <2 results, auto-crawls PubMed/Medical News Today (rate-limited)
c. Indexes new content into chunks
d. Generates answer with Groq LLM using retrieved context
4. Returns markdown answer with cited sources
5. Source cards show title, URL, relevance score
6. Q&A saved to history for future reference