Spaces:
Sleeping
Sleeping
Upload README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Chest X-ray Recommender
|
| 3 |
+
emoji: 🩻
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.16.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: Visual chest X-ray recommender powered by CLIP
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# 🩻 Chest X-ray Recommender
|
| 15 |
+
|
| 16 |
+
A visual recommendation engine for chest X-rays, built with CLIP embeddings
|
| 17 |
+
and Gradio. Upload an X-ray **or** describe a finding in words and the app
|
| 18 |
+
returns 3–5 of the most visually similar studies from a pre-computed catalog
|
| 19 |
+
drawn from [`MLforHealthcare/mimic-cxr`](https://huggingface.co/datasets/MLforHealthcare/mimic-cxr).
|
| 20 |
+
|
| 21 |
+
> ⚠️ **Educational demo only. Not a medical device. Do not use for clinical decisions.**
|
| 22 |
+
|
| 23 |
+
## How it works
|
| 24 |
+
|
| 25 |
+
1. The companion notebook (`Assignment_3_MIMIC_CXR_Recommender.ipynb`) sub-samples
|
| 26 |
+
2,000 chest X-rays from MIMIC-CXR.
|
| 27 |
+
2. Each image is embedded with
|
| 28 |
+
[`openai/clip-vit-base-patch32`](https://huggingface.co/openai/clip-vit-base-patch32)
|
| 29 |
+
into a 512-dimensional vector and L2-normalised.
|
| 30 |
+
3. Embeddings, base64 thumbnails, KMeans cluster labels, and the original
|
| 31 |
+
radiology reports are saved together to `embeddings.parquet`.
|
| 32 |
+
4. This Space loads that single parquet on startup. At query time, the user's
|
| 33 |
+
text or image is encoded with the same CLIP model and the catalog is ranked
|
| 34 |
+
by cosine similarity.
|
| 35 |
+
5. The app returns the **top-3** matches plus up to **2 extra** matches (5 total)
|
| 36 |
+
when consecutive scores differ by ≤ 0.02, giving the user "second opinions"
|
| 37 |
+
when the model is uncertain.
|
| 38 |
+
|
| 39 |
+
## Files in this Space
|
| 40 |
+
|
| 41 |
+
| File | Purpose |
|
| 42 |
+
|----------------------|---------|
|
| 43 |
+
| `app.py` | Gradio interface + recommendation logic |
|
| 44 |
+
| `requirements.txt` | Python dependencies |
|
| 45 |
+
| `embeddings.parquet` | Pre-computed catalog (built by the notebook) |
|
| 46 |
+
| `README.md` | This file (also drives the Space card) |
|
| 47 |
+
|
| 48 |
+
## Running locally
|
| 49 |
+
|
| 50 |
+
```bash
|
| 51 |
+
pip install -r requirements.txt
|
| 52 |
+
python app.py
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
The app launches at <http://127.0.0.1:7860>.
|
| 56 |
+
|
| 57 |
+
## Configuration
|
| 58 |
+
|
| 59 |
+
A few environment variables tweak the behaviour without code changes (set them
|
| 60 |
+
in your Space under **Settings → Variables and secrets**):
|
| 61 |
+
|
| 62 |
+
| Variable | Default | Description |
|
| 63 |
+
|-------------------|------------------------------------|-------------|
|
| 64 |
+
| `CLIP_MODEL_ID` | `openai/clip-vit-base-patch32` | Any HF CLIP model. Try `flaviagiammarino/pubmed-clip-vit-base-patch32` for medical fine-tuning. |
|
| 65 |
+
| `EMBEDDINGS_FILE` | `embeddings.parquet` | Path to the catalog file. |
|
| 66 |
+
| `K_MIN` | `3` | Minimum number of recommendations. |
|
| 67 |
+
| `K_MAX` | `5` | Maximum number of recommendations (if score gaps are tight). |
|
| 68 |
+
| `GAP_THRESHOLD` | `0.02` | Cosine-similarity gap below which to include extra matches. |
|
| 69 |
+
| `VIDEO_EMBED_ID` | (empty) | YouTube video ID for the walk-through. When set, the Space adds an embed at the bottom. |
|
| 70 |
+
|
| 71 |
+
## Acknowledgements
|
| 72 |
+
|
| 73 |
+
- Dataset: [MLforHealthcare/mimic-cxr](https://huggingface.co/datasets/MLforHealthcare/mimic-cxr)
|
| 74 |
+
- Embedding model: [openai/clip-vit-base-patch32](https://huggingface.co/openai/clip-vit-base-patch32)
|
| 75 |
+
- Course: Data Science Assignment 3 — Embeddings, RecSys, Spaces
|