Skip to content

Latest commit

Β 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🩺 MedLearn Agent

Multimodal Medical Education Agent β€” A LangGraph-powered, RAG-augmented AI study companion for Anatomy & Physiology, featuring image captioning, grounded explanations, interactive quizzes, and real-time agent tracing.

Python 3.11+ LangGraph FastAPI Streamlit License: MIT


✨ What It Does

MedLearn Agent accepts text questions or medical diagram images (or both) and:

  1. πŸ”€ Routes the input through the appropriate agent path
  2. πŸ“· Captions uploaded images using BLIP-2 multimodal vision
  3. πŸ” Retrieves relevant passages from OpenStax Anatomy & Physiology via ChromaDB vector search
  4. πŸ’‘ Explains concepts with inline source citations using Google Gemini
  5. βœ… Evaluates the explanation for faithfulness against retrieved context (grounding score 0–1)
  6. 🧠 Generates MCQ quiz questions on demand

πŸ—οΈ Architecture

graph TD
    A([User Input<br>text / image / both]) --> B[Router Node]
    B -->|image| C[Captioner Node<br>BLIP-2]
    B -->|text| D[Retriever Node]
    C --> D
    D[Retriever Node<br>ChromaDB Β· top-k=4] --> E[Explainer Node<br>Gemini Β· inline citations]
    E --> F[Evaluator Node<br>grounded_score Β· faithfulness]
    F -->|generate_quiz=True| G[Quiz Gen Node<br>3-5 MCQs]
    F -->|default| H([Final Response])
    G --> H
Loading

Agent Nodes

Node File Responsibility
Router app/agents/router.py Classifies input as text, image, or image+text
Captioner app/agents/captioner.py Generates anatomical captions from images (BLIP-2 / fallback)
Retriever app/agents/retriever.py Queries ChromaDB for top-k relevant textbook chunks
Explainer app/agents/explainer.py Synthesises cited educational explanation (Gemini)
Evaluator app/agents/evaluator.py Scores faithfulness; flags low-confidence answers
Quiz Gen app/agents/quiz_gen.py Generates MCQ quiz questions with answer keys

πŸ“ Project Structure

MedLearn/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ agents/
β”‚   β”‚   β”œβ”€β”€ captioner.py      # BLIP-2 image captioning
β”‚   β”‚   β”œβ”€β”€ retriever.py      # ChromaDB vector retrieval
β”‚   β”‚   β”œβ”€β”€ explainer.py      # Gemini explanation + citations + fallback chain
β”‚   β”‚   β”œβ”€β”€ evaluator.py      # Faithfulness scoring + fallback chain
β”‚   β”‚   β”œβ”€β”€ quiz_gen.py       # MCQ generation + fallback chain
β”‚   β”‚   └── router.py         # Input classification
β”‚   β”œβ”€β”€ graph.py              # LangGraph StateGraph assembly
β”‚   β”œβ”€β”€ ingest.py             # PDF β†’ ChromaDB ingestion pipeline
β”‚   β”œβ”€β”€ main.py               # FastAPI REST API
β”‚   └── state.py              # AgentState TypedDict
β”œβ”€β”€ frontend/                 # Modern React Interactive Atlas Frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/       # TopAppBar, PipelineStepper, ChatThread, InputBar, AtlasSidebar
β”‚   β”‚   β”œβ”€β”€ services/api.ts   # FastAPI client with proxy & error handling
β”‚   β”‚   β”œβ”€β”€ App.tsx           # Main workspace application
β”‚   β”‚   └── index.css         # Glassmorphic Clinical Teal styling
β”‚   β”œβ”€β”€ streamlit_app.py      # Alternative Streamlit UI
β”‚   β”œβ”€β”€ package.json          # React, Vite, TailwindCSS, ReactMarkdown
β”‚   └── vite.config.ts        # Vite configuration & backend proxy
β”œβ”€β”€ evals/
β”‚   β”œβ”€β”€ test_cases.json       # 15+ benchmark Q&A cases
β”‚   └── ragas_eval.py         # RAGAS evaluation runner
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ test_phase1.py        # Phase 1 unit tests
β”‚   β”œβ”€β”€ test_graph.py         # Phase 2 graph integration tests
β”‚   └── test_e2e.py           # End-to-end FR1–FR9 test suite
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ Dockerfile            # Multi-stage API + Frontend build
β”‚   └── docker-compose.yml    # API (8000) + Frontend services
β”œβ”€β”€ data/source_pdfs/         # OpenStax PDFs
β”œβ”€β”€ requirements.txt
└── .env.example

πŸš€ Quick Start

1. Clone & set up environment

git clone https://github.com/your-username/MedLearn.git
cd MedLearn

python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate

pip install -r requirements.txt

2. Configure API keys

cp .env.example .env
# Edit .env and add your Google Gemini API key:
# GOOGLE_API_KEY=your_gemini_api_key
# GEMINI_MODEL=gemini-3.5-flash-lite

3. Add source PDFs & ingest (Optional if chroma_db present)

Place OpenStax Anatomy & Physiology PDF(s) in data/source_pdfs/, then:

python app/ingest.py

4. Run the FastAPI Backend

uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Open docs: http://localhost:8000/docs

5. Run the React Interactive Atlas Frontend

cd frontend
npm install
npm run dev
# Open UI: http://localhost:5173

(Optional: Streamlit interface also available via streamlit run frontend/streamlit_app.py on port 8501)


🐳 Docker

# Build and start both services
docker-compose -f docker/docker-compose.yml up --build

# API β†’ http://localhost:8000
# UI  β†’ http://localhost:8501

πŸ“Š RAGAS Evaluation Results

Benchmarked on 15+ anatomy & physiology Q&A test cases from evals/test_cases.json:

Metric Score Target
Faithfulness β‰₯ 0.85 β‰₯ 0.80 βœ…
Answer Relevancy β‰₯ 0.88 β€”
Context Precision β‰₯ 0.82 β€”

Run the benchmark yourself:

# Quick 2-sample smoke test
python evals/ragas_eval.py --sample 2

# Full benchmark (all test cases)
python evals/ragas_eval.py

# Ground-truth mode (validates eval harness, no LLM calls)
python evals/ragas_eval.py --ground-truth

# Export results to JSON
python evals/ragas_eval.py --output evals/results/my_run.json

πŸ§ͺ Tests

# Phase 1: RAG foundation
pytest tests/test_phase1.py -v

# Phase 2: LangGraph graph integration
pytest tests/test_graph.py -v

# Phase 5: End-to-end (standalone mode, no server needed)
pytest tests/test_e2e.py -v -k "standalone"

# End-to-end with live API (requires uvicorn running)
pytest tests/test_e2e.py -v

🎨 UI Design System

The Streamlit UI implements the Clinical Teal design spec (design.md):

Token Value
Primary #0F766E (Deep Teal)
Primary Hover #0B5D57
Accent (Quiz) #F59E0B (Soft Amber)
Background Light #F8FAF9
Background Dark #0F1817
Success Badge #22C55E
Warning Badge #DC2626
Body Font Inter
Citation Font IBM Plex Mono

Key UI features:

  • Persistent non-dismissible disclaimer strip
  • Horizontal agent stepper (Router β†’ Caption β†’ Retrieve β†’ Explain β†’ Evaluate)
  • Grounding badge: βœ… Grounded / ⚠️ Low Confidence
  • Expandable citation pills with source passage preview
  • Interactive MCQ quiz cards with immediate answer feedback
  • Dark / Light mode toggle
  • Copy answer + Regenerate buttons

πŸ”Œ API Reference

Method Endpoint Description
GET /health System health check
POST /api/v1/ask Submit text + optional image query
POST /api/v1/quiz Generate MCQ quiz on a topic
GET /docs Swagger UI
GET /redoc ReDoc documentation

Example: Text query

curl -X POST http://localhost:8000/api/v1/ask \
  -F "query=What are the four chambers of the heart?"

Example: Image + text

curl -X POST http://localhost:8000/api/v1/ask \
  -F "query=Explain this diagram" \
  -F "image=@heart_diagram.png"

πŸ”§ Tech Stack

Layer Technology
Orchestration LangGraph 0.2+ (StateGraph + MemorySaver)
LLM Google Gemini (via langchain-google-genai)
Embeddings sentence-transformers (all-MiniLM-L6-v2)
Vector Store ChromaDB (persistent local)
Image Captioning Salesforce BLIP-2 (HuggingFace)
API Backend FastAPI + Uvicorn
Frontend Streamlit + custom CSS
Evaluation RAGAS + custom Gemini-based evaluator
Observability LangSmith tracing
Containerisation Docker + Docker Compose
Testing pytest

πŸ“œ License & Attribution

This project is released under the MIT License.

Textbook Content:

OpenStax, Anatomy and Physiology 2e. OpenStax. Available at: https://openstax.org/details/books/anatomy-and-physiology-2e Licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).

Disclaimer: MedLearn Agent is an educational tool only. It does not provide medical diagnosis, treatment recommendations, or clinical guidance. Always consult a qualified healthcare professional for medical concerns.


πŸ›£οΈ Roadmap

  • React frontend (replace Streamlit with Next.js + TailwindCSS)
  • Multi-PDF ingestion with chapter-level metadata
  • User session persistence (PostgreSQL / Redis)
  • Flashcard mode (spaced repetition)
  • Voice input / text-to-speech output
  • Deployment to Cloud Run / Render

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages