GemmaSight / README.md
TouseefAhmad's picture
Upload folder using huggingface_hub
4057e44 verified
|
Raw
History Blame Contribute Delete
4.87 kB

A newer version of the Gradio SDK is available: 6.24.0

Upgrade
metadata
title: GemmaSight
emoji: πŸ”¬
colorFrom: green
colorTo: blue
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false

GemmaSight πŸ”¬ β€” Multimodal Colorectal Cancer Pathology Assistant

GemmaSight is a state-of-the-art multimodal AI pathology assistant designed to analyze H&E-stained colorectal cancer tissue patches to rapidly classify MSI-High (Microsatellite Instability-High) vs. MSS (Microsatellite Stable) status, bypassing the typical 2-4 week turnaround of traditional genomic sequencing.


πŸš€ Key Features (Five-Phase Pipeline)

  1. Dual-Encoder Fusion: Features google/path-foundation (384-dimensional) and google/medsiglip-448 (1152-dimensional) frozen vision models, concatenated into a high-fidelity 1536-dimensional clinical embedding.
  2. Classification MLP: A highly regularized, 3-layer dense neural network trained to optimize AUROC and output the final binary phenotype status and probability score.
  3. FAISS Retrieval: Normalized cosine-similarity indexing (faiss.IndexFlatL2) to locate and retrieve the Top-3 matching reference cases from the clinical training registry for evidence-based decisions.
  4. Spatial Explainability: Sliding-window occlusion (40x40 stride=20) computing exact confidence drop deltas smoothed with a Gaussian kernel to isolate and highlight the Top 20% most influential diagnostic regions.
  5. Report Generation (MedGemma): Automatically prompts google/medgemma-1.5-4b-it in bfloat16 to fuse score, visual overlays, and historical matches into a structured clinical pathology brief under 200 words.

πŸ› οΈ Project File Structure

gemmasight/
β”œβ”€β”€ requirements.txt           # Dependency requirements
β”œβ”€β”€ config.py                 # Hyperparameters, paths, and mode configs
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ feature_extractor.py  # Dual-encoder feature extractor (gated fallback)
β”‚   β”œβ”€β”€ classifier.py         # 3-layer classification MLP PyTorch module
β”‚   β”œβ”€β”€ retriever.py          # FAISS Flat index case retriever
β”‚   └── report_generator.py   # MedGemma clinical report generator (simulator fallback)
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ occlusion.py          # Sliding-window occlusion visualizer
β”‚   β”œβ”€β”€ preprocess.py         # PIL preprocessing transformations
β”‚   └── visualization.py      # Premium custom Gradio layouts & CSS
β”œβ”€β”€ train.py                  # MLP classifier training pipeline
β”œβ”€β”€ inference.py              # End-to-end diagnosis flow orchestration
β”œβ”€β”€ app.py                    # Entrypoint for the Gradio Blocks dashboard
└── README.md                 # Deployment & setup documentation

πŸ“¦ Setup & Local Installation

Prerequisites

  • Python 3.9+
  • CUDA-enabled GPU (Highly recommended for live model weights, though CPU simulation mode is supported)

Step 1: Clone and install dependencies

git clone https://github.com/msaleem-aisci/gemmasight.git
cd gemmasight
pip install -r requirements.txt

Step 2: Configure Environment Variables & Gated Access

Ensure your HF_TOKEN has read permissions and your Hugging Face account has accepted the license agreements for:

Export token:

export HF_TOKEN="your_huggingface_write_token_here"

If you are running in resource-constrained environments (e.g. standard CPUs or free spaces), ensure FORCE_SIMULATION = True is set in config.py to activate the high-fidelity simulator fallbacks.


πŸ”¬ Running Training & Inference

1. Optimize Classifier (MLP)

Run training on pre-extracted embeddings. If no embeddings are found in data/, the pipeline will automatically generate a randomized synthetic dataset baseline to facilitate quick local testing:

python train.py

This builds the FAISS case indices and saves the trained classification model weights into weights/best_classifier.pt.

2. Launch Gradio Clinical Dashboard

Launch the dashboard on localhost (defaults to port 7860):

python app.py

πŸš€ Deployment to Hugging Face Spaces

  1. Create a new Space on Hugging Face (Choose the Gradio SDK, preferably selecting a GPU hardware tier like T4 Small for fast inference).
  2. Set the HF_TOKEN as a Secret under your Hugging Face Space settings.
  3. Push your repository to the Hugging Face Space Git remote:
git remote add hf https://huggingface.co/spaces/YOUR_USER_OR_ORG/YOUR_SPACE_NAME
git push -u hf main
  1. The space will automatically install dependencies from requirements.txt and launch app.py.