---
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)
---
# ๐ SecureLens
### TRUE Fully Homomorphic Encryption for Medical AI
[](https://huggingface.co/spaces/paulamartya25/SecureLens)
[](https://securelens-1-m5kt.onrender.com/health)
[](https://python.org)
[](https://pytorch.org)
[](https://github.com/OpenMined/TenSEAL)
[](LICENSE)
**Privacy-Preserving Pneumonia Detection โ the server computes on encrypted data and is mathematically unable to decrypt it.**
---
## ๐๏ธ 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.**
---
**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)