Skip to content

extremecoder-rgb/Medic-LLM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ₯ MedicLLM

FHIR-Aware Clinical RAG Assistant

Python 3.11+ FastAPI Gradio License: MIT


A clinical decision support system that combines FHIR-compliant patient data parsing, retrieval-augmented generation (RAG), and a fine-tuned Qwen 2.5 language model to deliver accurate, context-aware medical responses.

✨ Features

Feature Description
πŸ“‹ FHIR Patient Parser Validates and structures patient JSON into clinical context
πŸ” RAG Pipeline FAISS-powered vector search over medical documents
🧠 Fine-tuned LLM Qwen 2.5 with LoRA adapters trained on medical QA data
🌐 REST API FastAPI endpoints for patient parsing, queries, and summaries
πŸ–₯️ Web Interface Gradio UI for interactive clinical question-answering
πŸ“Š Evaluation Suite ROUGE, BLEU, and Exact Match metrics

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Patient JSON    │────▢│  FHIR Parser │────▢│ Patient Context  β”‚
β”‚  (FHIR-compat)  β”‚     β”‚              β”‚     β”‚                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                      β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  User Question  │────▢│ RAG (FAISS)  │───────────────
β”‚                 β”‚     β”‚              β”‚              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β–Ό
                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                         β”‚  Qwen 2.5 (LoRA)   β”‚
                                         β”‚  Clinical Inference β”‚
                                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚
                                                   β–Ό
                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                         β”‚  Clinical Answer    β”‚
                                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • uv (recommended) or pip

Installation

# Clone the repository
git clone https://github.com/your-username/MedicLLM.git
cd MedicLLM

# Create virtual environment and install dependencies
uv venv
.venv\Scripts\activate    # Windows
# source .venv/bin/activate  # macOS/Linux
uv sync

πŸ“ Project Structure

MedicLLM/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api.py              # FastAPI REST endpoints
β”‚   β”œβ”€β”€ fhir_parser.py      # FHIR patient data parser
β”‚   β”œβ”€β”€ inference.py        # LLM inference with LoRA
β”‚   β”œβ”€β”€ rag.py              # RAG pipeline (FAISS + embeddings)
β”‚   └── schemas.py          # Pydantic request/response models
β”œβ”€β”€ training/
β”‚   β”œβ”€β”€ config.py           # Training hyperparameters
β”‚   β”œβ”€β”€ dataset.py          # Medical QA dataset loader
β”‚   β”œβ”€β”€ train.py            # Fine-tuning with TRL + PEFT
β”‚   └── evaluate.py         # ROUGE / BLEU evaluation
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ medical_docs/       # Medical reference documents
β”‚   β”œβ”€β”€ medical_qa.json     # Training dataset
β”‚   └── patient_*.json      # Sample patient records
β”œβ”€β”€ tests/
β”‚   └── test_fhir_parser.py
β”œβ”€β”€ gradio_app.py           # Interactive web UI
└── pyproject.toml          # Project configuration

πŸ’‘ Usage

1. Start the API Server

uv run python -m app.api

Server runs at http://localhost:8000

2. Launch the Web UI

uv run python gradio_app.py

Gradio interface opens at http://localhost:7860

3. Query via API

Health Check:

curl http://localhost:8000/health

Ask a Clinical Question:

curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{
    "question": "What should be monitored for this patient?",
    "patient_data": {
      "patient_id": "P1001",
      "patient": {"age": 65, "gender": "male"},
      "conditions": ["Type 2 Diabetes", "Hypertension"],
      "medications": ["Metformin 500mg", "Lisinopril 10mg"],
      "observations": ["HbA1c: 7.2%"],
      "allergies": ["Penicillin"]
    }
  }'

Generate Patient Summary:

curl -X POST http://localhost:8000/summary \
  -H "Content-Type: application/json" \
  -d '{
    "patient_data": {
      "patient_id": "P1001",
      "patient": {"age": 65, "gender": "male"},
      "conditions": ["Type 2 Diabetes"],
      "medications": ["Metformin 500mg"]
    }
  }'

πŸ§ͺ Fine-Tuning

Train on Kaggle with GPU acceleration:

cd training
python train.py

Training Configuration (training/config.py):

Parameter Value
Base Model Qwen/Qwen2.5-3B-Instruct
LoRA Rank 16
LoRA Alpha 32
Epochs 3
Learning Rate 2e-4
Batch Size 4

πŸ“Š Evaluation

uv run python training/evaluate.py

Computes:

  • ROUGE-1 / ROUGE-2 / ROUGE-L β€” n-gram overlap with reference answers
  • BLEU β€” translation-quality scoring
  • Exact Match β€” strict correctness metric

πŸ”§ API Reference

Endpoint Method Description
/health GET Health check and model status
/patient POST Parse and validate patient JSON
/query POST Ask a clinical question with optional patient context
/summary POST Generate a clinical patient summary

πŸ› οΈ Tech Stack

  • Model: Qwen 2.5 3B with LoRA via PEFT
  • Training: TRL SFTTrainer, Hugging Face Accelerate
  • RAG: FAISS, Sentence Transformers (all-MiniLM-L6-v2)
  • API: FastAPI + Uvicorn
  • UI: Gradio
  • Evaluation: rouge-score, NLTK BLEU

Built with care for clinical intelligence

About

Basic RAG assistant with fine tuned capabilities

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors