MedFormer is a multimodal medical assistant. It answers questions about medical images, uses retrieval-augmented generation (RAG) to ground its answers in PDF documents, and supports spoken input and output. It was built in 2024 as the B.Sc. capstone project in Artificial Intelligence at the University of Jordan. A demo recording is in MedFormer.mp4.
Research prototype. MedFormer has not been clinically validated and must not be used for diagnosis or treatment decisions.
image + question ──► Idefics2-8B (vision-language model) ──┐
├──► answer (text, optional speech)
PDF documents ──► chunks ──► BGE embeddings ──► Chroma ──► medical LLM (RAG)
voice ──► NeMo speech-to-text ──► question edge-tts ──► spoken answer
| Component | Model / tool |
|---|---|
| Vision-language model | Idefics2-8B (SigLIP vision encoder + Mistral-7B) |
| Medical language model for RAG | LLaMA-3-8B fine-tuned on medical QA (MedLLaMA-3); the demo also runs with JSL-MedLlama-3-8B |
| Retrieval | LangChain, Chroma vector store, BGE v1.5 embeddings via FastEmbed, PDF loaders |
| Speech | NeMo streaming speech-to-text, edge-tts text-to-speech |
| Interface | Gradio |
Both models were fine-tuned with QLoRA (LoRA adapters on a 4-bit NF4-quantised base model):
- Idefics2-8B on a document visual question answering set (DocVQA, 1,200 examples) to improve reading of text in images, such as reports and labelled scans. Result:
Reverb/idefics2-8B-docVQA-finetuned. - LLaMA-3-8B on medical question answering (
medarc/sft_multimedqa). Result:Reverb/medllama3-8B.
General multimodal benchmarks, from the project report:
| Model | Params | Avg | MMMU (val) | OCRBench | AI2D |
|---|---|---|---|---|---|
| CogVLM-17B-Chat | 17B | 47.9 | 37.3 | 590 | 63.3 |
| LLaVA-NeXT-Mistral | 7.6B | 45.8 | 37.0 | 531 | 69.0 |
| DeepSeek-VL | 7.3B | 46.2 | 38.3 | 435 | 65.3 |
| Idefics2 (base) | 8B | 53.0 | 45.2 | 626 | 72.3 |
| MedFormer | 8B | 54.2 | 45.4 | 650 | 70.9 |
Fine-tuning improved the average score over the Idefics2 base model, driven mainly by OCRBench (+24). AI2D, which tests diagram understanding, dropped by 1.4 points. These are general benchmarks. MedFormer was not evaluated on medical benchmarks such as VQA-RAD or PathVQA, which is the main gap for future work.
| Path | Contents |
|---|---|
experiments/IDEFICS2-Fine-Tuning.ipynb |
LoRA fine-tuning of Idefics2 (2024 capstone) |
experiments/LLama-3-Fine-Tuning.ipynb |
LoRA fine-tuning of LLaMA-3 on medical QA (2024 capstone) |
experiments/MedFormer-UI.ipynb |
The full system: RAG, speech and Gradio interface (2024 capstone) |
medformer.py, finetune.py, dataset_prep.py |
2025 follow-up: a custom architecture joining a SigLIP2 vision encoder to Qwen2.5-7B (or BiMediX2-8B) through a trainable cross-attention adapter. Experimental, not evaluated. |
MedFormer.mp4 |
Demo recording |
Requires a CUDA GPU that can hold the 8B models (4-bit quantisation helps on smaller cards).
git clone https://github.com/baselanaya/MedFormer.git
cd MedFormer
pip install -r requirements.txt
jupyter notebook experiments/MedFormer-UI.ipynbRun the 2025 follow-up model from the command line:
python medformer.py --image scan.png --mode answer --question "What abnormality is visible?"- Not clinically validated, and not evaluated on medical benchmarks.
- It inherits the biases and failure modes of Idefics2 and LLaMA-3: it can produce harmful stereotypes and confident but wrong statements.
- The vision model was fine-tuned on general documents, not on medical images.
Built by Basil (Basel) Anaya (idea owner and team lead), Osama Awad, Subhi Abdullah and Abdelrahman Al Khatib, King Abdullah II School for Information Technology, University of Jordan, 2024.
