--- title: MediLLM emoji: π©Ί colorFrom: indigo colorTo: purple sdk: gradio sdk_version: 5.43.1 app_file: app/demo/demo.py pinned: true --- # π©Ί MediLLM: Multimodal Clinical Triage Assistant
> A multimodal AI system for clinical triage that fuses **Electronic Medical Records (EMR text)** with **Chest X-ray images** to predict triage level (**High / Medium / Low**). > Includes advanced interpretability (Grad-CAM, token attention, attention rollout), automated training pipelines, CI/CD, and deployment via **Docker** & **Hugging Face Spaces**. --- ## π Demo π [**Try it on Hugging Face Spaces**](https://huggingface.co/spaces/Preetham22/medi-llm) --- ## β¨ Features - LLM + Vision Fusion - Few-shot Prompt Tuning - Real-time Inference via FastAPI - Deployed with Docker ## π§ Model Architecture This project uses a fusion of: - 𧬠ClinicalBERT for EMR text - π©» ResNet-50 for chest X-rays - β Concatenated deatures passed into a classification head  ## π Dataset Sources - This project uses a subset of the [COVID-19 Radiography Dataset](https://www.kaggle.com/datasets/tawsifurrahman/covid19-radiography-database) ## π Sample EMR Records - This project generates synthetic EMR records linked to chest X-ray images. - A sample CSV file (`sample_emr_records.csv`) is provided for demonstration purposes: π [`sample_data/emr_records.csv`](sample_data/sample_emr_records.csv) | patient_id | image_path | emr_text | triage_level | | ----------------- | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------ | | COVID-1 | images/COVID/COVID-1.png | Progressive difficulty in breathing. Oxygen saturation is below the normal range | high | | NORMAL-1 | images/NORMAL/Normal-1.png | Routine checkup with no abnormal findings. The patient denies cough or chest pain | low | | VIRAL PNEUMONIA-1 | images/VIRAL PNEUMONIA/Viral Pneumonia-1.png | Crackles are auscultated in the lower lobes. The patient presents with fatigue and mild respiratory distress | medium | > This sample includes 3-5 rows per class. To generate the full dataset, run `generate_emr_csv.py`. ## π Dataset Notes - This project uses synthetic EMR records aligned with publicly available chest X-ray images. EMR notes were generated using medically-inspired templates, mapped to classes (e.g., COVID -> high triage level). > β οΈ Note: This is **simulated data** and is for **educational purposes only**. No patient information is used. ## βοΈ Training Pipeline Overview The MediLLM training pipeline includes the following steps: --- 1. **𧬠Synthetic Dataset Generation** - EMR notes are dynamically generated using class-specific medical templates, ambiguous cases, noise injection and randomized vitals with a little bit of blur. - Aligned with chest X-ray images (COVID, NORMAL, VIRAL PNEUMONIA). - Balanced dataset of 300 samples per class (900 total) via `generate_emr_csv.py`. 2. **π§ͺ Data Augmentation** - Strong augmentation applied on X-rays: - Random cropping, rotation, color jittering, and Gaussian blur. - Text inputs tokenized using ClinicalBERT tokeinizer. 3. **π¦ Dataset Loader** - `TriageDataset.py` handles fusion of images and EMR text. - Includes dynamic image transformation and BERT-style tokenized text. - Stratified splitting via `StratifiedShuffleSplit` ensures class-balanced validation. 4. **π§ Model Architecture** - Text encoder: `Bio_ClinicalBERT` - Image encoder: Pretrained `ResNet-50` - Fusion: Concatenation -> Feedforward classifier -> Softmax 5. **π§ͺHyperparameter Tuning** - `train_optuna.py` Optuna is used for automated hyperparameter search. - Search space includes: - Learning rate - Dropout - Batch size - Hidden dimension - F1 Score (weighted) is the target metric. - Logging and visualization powered by **Weights & Biases (W&B)**. ## π How to Run Hyperparameter Tuning ```bash python train_optuna.py --n_trials 25 ``` ## π Insights from Tuning & Dataset Evolution