--- title: Federated DP Fair Disease Risk emoji: 🩺 colorFrom: blue colorTo: green sdk: gradio sdk_version: 5.9.1 python_version: "3.10" app_file: app.py pinned: false license: mit --- # Privacy-Preserving Federated Learning for Early Multi-Disease Risk Prediction with Fairness Constraints **Diseases:** diabetes (diabetic retinopathy, retinal fundus images) and heart disease (cardiomegaly, chest X-rays). **Domain:** medical image processing. **Cost:** zero (free HF Space CPU + free public datasets). ## What it does 1. **Image processing (OpenCV):** CLAHE for X-rays; border crop + Ben-Graham local-contrast normalisation for fundus photos; resize to 224. 2. **Frozen MobileNetV2** (ImageNet weights) turns each image into a 1280-d embedding, computed once and cached. No deep-network training is needed. 3. **Simulated hospitals** (non-IID Dirichlet split) each train a logistic head on their own embeddings with **DP-SGD** (per-example clipping + Gaussian noise, Poisson sampling). 4. **FedAvg** aggregates client updates. Raw images and embeddings never leave a hospital. 5. **Fairness controller:** the server watches aggregate recall (TPR) of two patient groups and re-weights examples (public weights, applied after clipping) to shrink the gap (equal opportunity). 6. **Privacy accounting:** RDP accountant reports record-level (epsilon, delta=1e-5) per hospital. 7. The app compares **Baseline FedAvg vs +DP vs +DP+Fairness** and prints a copy-paste results table. Fairness groups: patient **sex** for chest X-rays if the dataset exposes it; otherwise (and always for fundus) an **image-brightness proxy** for acquisition conditions. The app states which one was used. ## Step 0 (required for every option): build the feature cache once Run `python prepare_features.py --task heart --n 150` and `--task diabetes` in free Google Colab (or your laptop). This creates `cache/heart.npz` and `cache/diabetes.npz`. With these files the app needs no torch and no dataset download. ## Where to run it (free options, best first for a next-day deadline) 1. **Google Colab public link (guaranteed, no account tier needed):** upload this folder, `pip install gradio` and run `GRADIO_SHARE=1 python app.py`. Share the printed temporary public URL. Keep the notebook running during the demo. 2. **Your laptop (offline fallback):** `pip install gradio numpy pandas scikit-learn matplotlib pillow`, put the cache files in `cache/`, run `python app.py`, open http://127.0.0.1:7860. 3. **Hugging Face ZeroGPU Space (permanent link):** per current HF docs, Gradio Spaces need a paid plan EXCEPT up to 2 ZeroGPU Spaces for free personal accounts in good standing (verified email, account older than 30 days). New Space -> SDK Gradio -> hardware **ZeroGPU** (if it is not offered, your account is not eligible) -> upload all files including `cache/*.npz`. The app never uses the GPU; a no-op `@spaces.GPU` function in `app.py` only satisfies ZeroGPU's startup check. ### Local test (no torch needed) `pip install numpy pandas scikit-learn matplotlib` then `python tests/smoke_test.py` (runs the whole federated core on synthetic features). ## Honest limitations (say these before your guide does) - Backbone is frozen; only the classifier head is federated. Fine for a prototype, and it is what makes training instant. - Clients are simulated on one machine by partitioning one dataset. No real network or secure aggregation. - Epsilon covers the DP-SGD updates only; the noisy fairness counts are not included in the budget. - Small samples (hundreds of images) => wide confidence intervals. Report multiple seeds. - Fundus fairness groups are a proxy, not demographics. Sex is only used where the dataset provides it. - Research prototype, not a medical device. ## Files `app.py` UI | `fl/fed.py` FedAvg, DP-SGD, fairness | `fl/dp.py` clipping and RDP accountant | `fl/data.py` data and splits | `fl/vision.py` OpenCV and MobileNetV2 | `fl/experiment.py` tables and plots | `prepare_features.py` cache builder | `tests/smoke_test.py` | `docs/`