SecureLens / README.md
paulamartya25
README: remove Security Audit table
4aace6e
|
Raw
History Blame Contribute Delete
11.9 kB
---
title: SecureLens
emoji: πŸ”
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
license: mit
short_description: TRUE FHE Privacy-Preserving Pneumonia Detection (CKKS)
---
<div align="center">
# πŸ” SecureLens
### TRUE Fully Homomorphic Encryption for Medical AI
[![HF Space](https://img.shields.io/badge/πŸ€—%20HuggingFace-Live%20Demo-blue?style=for-the-badge)](https://huggingface.co/spaces/paulamartya25/SecureLens)
[![FHE Server](https://img.shields.io/badge/Render-FHE%20Server%20Live-46E3B7?style=for-the-badge&logo=render)](https://securelens-1-m5kt.onrender.com/health)
[![Python](https://img.shields.io/badge/Python-3.10-3776AB?style=for-the-badge&logo=python)](https://python.org)
[![PyTorch](https://img.shields.io/badge/PyTorch-2.0.1-EE4C2C?style=for-the-badge&logo=pytorch)](https://pytorch.org)
[![TenSEAL](https://img.shields.io/badge/TenSEAL-CKKS%20128bit-7B2FBE?style=for-the-badge)](https://github.com/OpenMined/TenSEAL)
[![License](https://img.shields.io/badge/License-MIT-green?style=for-the-badge)](LICENSE)
**Privacy-Preserving Pneumonia Detection β€” the server computes on encrypted data and is mathematically unable to decrypt it.**
</div>
---
## πŸ—οΈ TRUE FHE Architecture β€” Two Physical Machines
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” HTTPS β€” ciphertext only β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ CLIENT β€” HuggingFace Spaces β”‚ ─────────────────────────► β”‚ SERVER β€” Render.com β”‚
β”‚ paulamartya25/SecureLens β”‚ β”‚ securelens-1-m5kt.onrender.com β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ βœ… ResNet-18 extracts features β”‚ β”‚ βœ… W1 @ enc(x) + b1 [HE math] β”‚
β”‚ βœ… CKKS encrypt β†’ 326 KB cipher β”‚ ◄───────────────────────── β”‚ βœ… W2 @ enc(h) + b2 [HE math] β”‚
β”‚ βœ… Secret key β€” HERE ONLY β”‚ encrypted logits β”‚ ❌ NO secret key β”‚
β”‚ βœ… Decrypt result locally β”‚ β”‚ ❌ NEVER decrypts β”‚
β”‚ β”‚ β”‚ ❌ Cannot see plaintext β”‚
β”‚ Machine A (USA) β”‚ β”‚ Machine B (Singapore) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Two physically separate machines β€” different companies β€” TRUE FHE privacy guarantee
```
### Why This Matters
| Approach | Server sees patient data? | Privacy guaranteed by? |
|----------|--------------------------|------------------------|
| Traditional ML | βœ… Yes β€” raw image | ❌ Nothing |
| Encrypted Transport (HTTPS) | βœ… Yes β€” after decryption | ❌ Trust only |
| **SecureLens TRUE FHE** | **❌ Never β€” only ciphertext** | **βœ… Mathematics** |
---
## ✨ Features
### 5-Tab Gradio Interface
| Tab | Description |
|-----|-------------|
| πŸ”’ **TRUE FHE Classification** | Upload X-ray β†’ encrypt locally β†’ server computes on ciphertext β†’ local decrypt β†’ diagnosis |
| βš”οΈ **Attack Demo** | Adversarial robustness testing β€” noise, blur, brightness, contrast, FGSM, combined |
| πŸ“Š **FHE vs Traditional** | Side-by-side performance & timing comparison |
| 🧠 **GradCAM** | Explainable AI β€” gradient-weighted class activation maps |
| πŸ“Š **Model Evaluation** | Accuracy, Precision, Recall, F1, ROC-AUC, Confusion Matrix |
### Security & Reliability
- πŸ” **128-bit CKKS encryption** via TenSEAL
- πŸ”‘ **Secret key physically isolated** on client machine
- ⚑ **Wake Server button** β€” shows live server status before demo
- πŸ”„ **Background keep-alive** β€” pings server every 10 min
- πŸ€– **UptimeRobot** β€” external monitor pings every 5 min
- βœ… **Auto fallback** β€” gracefully falls back to in-process if server unreachable
---
## πŸ”¬ Technical Architecture
### Model Pipeline
```
Input X-Ray Image (224Γ—224Γ—3)
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ ResNet-18 Backbone β”‚ ← Pretrained on ImageNet
β”‚ (feature extractor) β”‚
β”‚ Global Average Pooling β”‚
β”‚ Output: 512-dim feature vector β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ Plaintext features (client only)
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ CKKS Encryption (CLIENT SIDE) β”‚ ← Secret key generated here
β”‚ enc(features) = 326 KB cipher β”‚ ← Never leaves this machine
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ Ciphertext sent over HTTPS
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FHE Server (Render.com) β”‚ ← Different physical machine
β”‚ β”‚
β”‚ Layer 1: W1 @ enc(x) + b1 β”‚ ← Pure homomorphic math
β”‚ Layer 2: W2 @ enc(h) + b2 β”‚ ← Still on ciphertext
β”‚ β”‚
β”‚ Returns: enc(logits) β”‚ ← Never decrypted
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ Encrypted logits returned
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Client Decryption β”‚ ← Uses secret key (client only)
β”‚ Softmax β†’ Prediction β”‚
β”‚ Display: Normal / Pneumonia β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
### Why No ReLU in the FHE Head?
ReLU requires value comparison on ciphertext β€” computationally intractable in CKKS. The linear head (no non-linearity) is the standard approach for FHE-compatible neural networks. Accuracy is preserved because the ResNet-18 backbone handles all non-linear feature extraction.
### CKKS Parameters
| Parameter | Value | Reason |
|-----------|-------|--------|
| Polynomial modulus degree | 8192 | 128-bit security |
| Coefficient modulus bits | [60, 40, 40, 60] | 2 multiplication levels |
| Global scale | 2⁴⁰ | Precision balance |
| Security level | 128-bit | SEAL library standard |
| Ciphertext size | ~326 KB | 512-dim vector |
---
## πŸ“ Project Structure
```
SecureLens/
β”œβ”€β”€ app.py # HF Spaces entry point
β”œβ”€β”€ app_gradio_enhanced_FOR_HF.py # Main 5-tab Gradio interface
β”‚ # ← fhe_server_infer() calls Render over HTTP
β”‚ # ← warm_up_server() for Wake button
β”‚ # ← _keep_alive_loop() background thread
β”œβ”€β”€ requirements.txt # Client dependencies
β”œβ”€β”€ Dockerfile # Client Docker config
β”‚
β”œβ”€β”€ server/ # πŸ” TRUE FHE Server (Render.com)
β”‚ β”œβ”€β”€ server_fhe.py # Flask API β€” HE inference only, no decrypt
β”‚ β”œβ”€β”€ feature_weights.json # W1 matrix (256Γ—512)
β”‚ β”œβ”€β”€ linear_weights.json # W2 matrix (2Γ—256)
β”‚ β”œβ”€β”€ requirements.txt # flask, tenseal, numpy only (no torch!)
β”‚ └── Dockerfile # Render.com Docker config
β”‚
β”œβ”€β”€ crypto_layer/
β”‚ └── ckks_engine.py # CKKS engine β€” encrypt/decrypt (CLIENT ONLY)
β”‚
β”œβ”€β”€ cloud_server/
β”‚ β”œβ”€β”€ server.py # Flask server for local deployment
β”‚ β”œβ”€β”€ client_pipeline.py # Native Python client (true FHE locally)
β”‚ β”œβ”€β”€ train_model_fhe_compatible.py # SecureLensNetFHE β€” FHE-compatible model
β”‚ β”œβ”€β”€ models/
β”‚ β”‚ β”œβ”€β”€ best_model.pth # Trained weights (Git LFS)
β”‚ β”‚ β”œβ”€β”€ feature_weights.json # Exported W1 (Git LFS)
β”‚ β”‚ └── linear_weights.json # Exported W2
β”‚ └── encrypted_inference/
β”‚ └── he_inference.py # HE engine β€” NEVER calls .decrypt()
β”‚
└── client/
└── templates/ # HTML templates (Flask web UI version)
```
---
## πŸš€ Run It Yourself
### Option 1 β€” Live Demo (No Setup)
πŸ‘‰ **[huggingface.co/spaces/paulamartya25/SecureLens](https://huggingface.co/spaces/paulamartya25/SecureLens)**
### Option 2 β€” Local (True Physical Separation)
```bash
# Clone repo
git clone https://github.com/paulamartya25/SecureLens-.git
cd SecureLens-
# Terminal 1 β€” Run the FHE server (Machine B)
cd server/
pip install -r requirements.txt
python server_fhe.py
# β†’ Running on http://localhost:10000
# Terminal 2 β€” Run the Gradio client (Machine A)
cd ..
pip install -r requirements.txt
FHE_SERVER_URL=http://localhost:10000 python app.py
# β†’ Open http://localhost:7860
```
### Option 3 β€” Docker
```bash
# Server
docker build -t securelens-server ./server/
docker run -p 10000:10000 securelens-server
# Client (in another terminal)
docker build -t securelens-client .
docker run -p 7860:7860 -e FHE_SERVER_URL=http://host.docker.internal:10000 securelens-client
```
### Option 4 β€” Native Python Client (Full True FHE)
```bash
# Uses client_pipeline.py β€” secret key truly local
python cloud_server/client_pipeline.py cloud_server/models/best_model.pth your_xray.jpg
```
---
## πŸ› οΈ Technology Stack
| Component | Technology | Purpose |
|-----------|-----------|---------|
| FHE | [TenSEAL](https://github.com/OpenMined/TenSEAL) (CKKS) | Homomorphic encryption |
| Deep Learning | PyTorch 2.0.1, ResNet-18 | Feature extraction |
| Web Interface | Gradio 3.50.2 | 5-tab demo UI |
| FHE Server | Flask + Gunicorn | HE inference API |
| Client Hosting | HuggingFace Spaces (Docker) | Public demo |
| Server Hosting | Render.com (Docker) | Separate FHE server |
| Uptime | UptimeRobot | Keep-alive monitoring |
---
## πŸ“– How CKKS Homomorphic Encryption Works
```python
# Standard ML inference (NOT private):
features = model(xray) # plaintext
logits = W @ features + b # plaintext
prediction = softmax(logits)
# SecureLens FHE inference (PRIVATE):
features = backbone(xray) # plaintext β€” CLIENT ONLY
enc_x = ckks.encrypt(features) # 326 KB ciphertext β€” client
# Server receives enc_x β€” cannot see features
enc_h = enc_x.dot(W1) + b1 # homomorphic dot product on ciphertext
enc_logits = enc_h.dot(W2) + b2 # still encrypted
# Client receives enc_logits β€” server never saw plaintext
logits = ckks.decrypt(enc_logits) # secret key on client only
prediction = softmax(logits) # Normal / Pneumonia
```
---
## ⚠️ Disclaimer
Research prototype demonstrating FHE in medical AI. **Not intended for clinical use.**
---
<div align="center">
**SecureLens** β€” Proving that AI can be both intelligent and private.
*CKKS Β· TenSEAL Β· PyTorch Β· ResNet-18 Β· 128-bit Security*
Built by [Amartya Paul](https://github.com/paulamartya25)
</div>