--- 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 [![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.**
--- ## ๐Ÿ—๏ธ 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)