A locally deployed Retrieval-Augmented Generation (RAG) system for medical case analysis, designed to retrieve clinically relevant evidence from medical documents and generate grounded responses. The project compares TF-IDF, all-mpnet-base-v2, and Bio_ClinicalBERT retrievers, supports local answer generation, and evaluates performance with Precision@k, Recall@k, BLEU, and ROUGE-L.
This project is built as a full medical RAG workflow rather than a simple chatbot wrapper. It supports:
- ingestion of medical
.txtand.pdfdocuments - chunking and indexing of clinical passages
- retriever comparison across baseline and embedding-based methods
- grounded answer generation from retrieved evidence
- evaluation on bundled and external medical QA benchmarks
Privacy-Centric Deployment: Runs locally for privacy-sensitive medical document analysis.
Retriever Comparison: Compares TF-IDF, all-mpnet-base-v2, and Bio_ClinicalBERT on the same benchmark pipeline.
Grounded Medical Answers: Generates responses from retrieved evidence passages instead of unconstrained free-form output.
Quantified Evaluation: Measures retrieval and answer quality using Precision@k, Recall@k, BLEU, and ROUGE-L.
Interactive Interface: Streamlit app for document upload, evidence inspection, querying, and evaluation.
flowchart LR
A["Upload Medical Documents"] --> B["Parse PDF / TXT Files"]
B --> C["Preprocess and Chunk Text"]
C --> D["Embedding / Indexing Layer"]
Q["Clinical Query"] --> E["Retriever Search"]
D --> E
E --> F["Top-k Relevant Passages"]
F --> G["Prompt Builder"]
G --> H["Generation Layer"]
H --> I["Grounded Medical Answer"]
I --> J["Evaluation Dashboard"]
The pipeline works as follows:
- Upload medical case documents in
.txtor.pdfformat. - Parse and preprocess the text from each document.
- Chunk documents into short evidence passages.
- Build lexical or embedding-based retrieval indexes.
- Retrieve top-k passages for a clinical query.
- Build a grounded prompt from retrieved evidence.
- Generate a response using the selected generator.
- Evaluate retrieval and answer quality on benchmark queries.
Programming Language: Python 3.10+
Libraries
- PyTorch
- Hugging Face Transformers
- SentenceTransformers
- scikit-learn
- PyPDF2
- pandas
- NumPy
- Streamlit
- Plotly
Retrievers
TF-IDFall-mpnet-base-v2Bio_ClinicalBERT
Generators
extractive-fallback- optional Hugging Face generator such as
google/flan-t5-base - extensible to local domain models such as
gemma-2-2borMedical-Llama3-8B
Evaluation
- Retrieval:
Precision@k,Recall@k - Generation:
BLEU,ROUGE-L
Corpus
PubMedQAabstracts or medically sourced passage exports
Evaluation
MedQuADfor medically grounded question-answer referencesHealthSearchQAfor realistic consumer-style health questions
The app is wired to automatically use local exports placed in:
data/external/pubmedqa_corpus.jsonl
data/external/medquad_eval.csv
data/external/healthsearchqa_eval.csv
Equivalent .json or .csv variants are also supported for the corpus, and .json / .jsonl are supported for evaluation files.
git clone https://github.com/aadyasingh55/Medical-RAG-System.git
cd Medical-RAG-System
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
streamlit run app.pyEvaluation on the bundled benchmark compared TF-IDF, all-mpnet-base-v2, and Bio_ClinicalBERT over 9 indexed chunks and 3 medical queries.
| Retriever | Precision@3 | Recall@3 | BLEU | ROUGE-L | Precision uplift vs TF-IDF |
|---|---|---|---|---|---|
| TF-IDF | 0.667 | 1.000 | 0.372 | 0.522 | 0.00% |
| all-mpnet-base-v2 | 0.833 | 1.000 | 0.238 | 0.335 | +24.89% |
| Bio_ClinicalBERT | 0.444 | 1.000 | 0.372 | 0.522 | -33.43% |
all-mpnet-base-v2improvedPrecision@3from66.7%to83.3%, a24.9%uplift over the TF-IDF baseline while maintaining100%Recall@3.TF-IDFretained stronger generation overlap on this small benchmark withBLEU 0.372andROUGE-L 0.522.
Retrieval performance
Generation quality
- The project is structured to support a public dataset stack of
PubMedQAfor retrieval andMedQuAD + HealthSearchQAfor evaluation without changing the app architecture. - Domain-specific retrievers and generators are loaded lazily and may require local model availability.
- If a heavyweight model is unavailable, the app falls back to a baseline retrieval and extractive generation path.
- This is an educational and portfolio system, not a diagnostic tool.