Skip to main content

AI-powered chest X-ray diagnostic assistant with CheXNet, Grad-CAM++ heatmaps, Gemini AI explanations, and nearby doctor recommendations.

Project description

๐Ÿซ Chest X-Ray Diagnostic Assistant

An AI-powered medical diagnostic system for chest X-ray analysis with explainable AI, Grad-CAM++ heatmaps, and nearby doctor recommendations.

๐Ÿš€ Features

  • 3-Stage Binary Pipeline: Validates images through garbage detection โ†’ chest confirmation โ†’ normal/abnormal assessment
  • 14-Pathology Detection: (DenseNet-121) detects conditions like Pneumonia, Cardiomegaly, Effusion, Mass, Nodule, and more
  • Grad-CAM++ Heatmaps: Visual overlays showing which regions the AI focused on
  • Gemini AI Explainability: gemini-3.1-flash-lite generates professional clinical explanations in natural language
  • Nearby Doctor Recommendations: Geoapify-powered specialist finder based on detected pathologies and the user's real location
  • Modular React Frontend: Clean, component-based UI with a premium medical design

๐Ÿ—๏ธ Architecture

Frontend (React)  โ†’  Backend (FastAPI)  โ†’  AI Models         โ†’  External APIs
     โ†“                     โ†“                    โ†“                      โ†“
  7 Components        REST API :8000       Binary Pipeline       Gemini 3.1 Flash Lite
  Medical UI          Image Processing      (DenseNet)           Geoapify Places
  Responsive UX       Heatmap Generation   Grad-CAM++ Maps

๐Ÿ“ Project Structure

chexnet-proto/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ app.py                    # FastAPI application (main entry point)
โ”‚   โ”œโ”€โ”€ requirements.txt          # Python dependencies
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ binary_pipeline.py    # 3-stage X-ray validation pipeline
โ”‚       โ”œโ”€โ”€ HeatmapGenerator.py   # Grad-CAM++ heatmap generation
โ”‚       โ”œโ”€โ”€ DensenetModels.py     # CheXNet model definition
โ”‚       โ”œโ”€โ”€ explainability_ai.py  # Gemini AI clinical explanation
โ”‚       โ””โ”€โ”€ geoapify_service.py   # Nearby doctor search (Geoapify)
โ”œโ”€โ”€ frontend/
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ App.js                # Root component & analysis orchestrator
โ”‚       โ”œโ”€โ”€ App.css               # Global styles
โ”‚       โ””โ”€โ”€ components/
โ”‚           โ”œโ”€โ”€ Header.js             # App header
โ”‚           โ”œโ”€โ”€ UploadSection.js      # X-ray upload & drag-drop
โ”‚           โ”œโ”€โ”€ BinaryPipelineStatus.js  # Pipeline validation display
โ”‚           โ”œโ”€โ”€ ImagePanel.js         # Original + heatmap side-by-side
โ”‚           โ”œโ”€โ”€ PathologyResults.js   # 14-pathology confidence grid
โ”‚           โ”œโ”€โ”€ AIExplanationCard.js  # Gemini AI explanation renderer
โ”‚           โ””โ”€โ”€ NearbyDoctors.js      # Geoapify-powered doctor finder
โ”œโ”€โ”€ chexnet/                      # Original reference repo
โ”œโ”€โ”€ data/                         # Dataset directory
โ”œโ”€โ”€ scripts/                      # Utility & training scripts
โ””โ”€โ”€ .env                          # API keys (not committed)

๐Ÿ“‹ Prerequisites

  • Python 3.8+
  • Node.js 16+
  • CUDA-compatible GPU (recommended for faster inference)
  • Gemini API Key โ€” Get one here
  • Geoapify API Key โ€” Get one here

๐Ÿ”ง Setup

1. Backend

cd backend
pip install -r requirements.txt

2. Frontend

cd frontend
npm install

3. Environment Variables

Create a .env file in the root directory:

GEMINI_API_KEY=your_gemini_api_key_here
GEOAPIFY_API_KEY=your_geoapify_api_key_here

Note: OpenAI/ChatGPT is no longer used. The project exclusively uses gemini-2.0-flash for AI explanations.

4. Run the Application

# Terminal 1 โ€” Backend
cd backend
uvicorn app:app --reload --host 0.0.0.0 --port 8000

# Terminal 2 โ€” Frontend
cd frontend
npm start

Visit: http://localhost:3000

๐Ÿ” How It Works

Stage 1 โ€” Binary Pipeline (Validation)

Model Task Result
Model 1 Garbage vs. X-ray Rejects non-medical images
Model 2 Chest vs. Other X-ray Confirms it's a chest X-ray
Model 3 Normal vs. Abnormal Initial pathology flag

Stage 2 โ€” CheXNet Analysis

  • Runs DenseNet-121 trained on ChestX-ray14 dataset
  • Detects 14 pathologies with confidence scores and clinical thresholds
  • Generates Grad-CAM++ attention heatmaps

Stage 3 โ€” AI Explainability (Gemini)

  • Combines Model 3 + CheXNet results into a structured prompt
  • Sends to Gemini 3.1 Flash Lite for clinical explanation
  • Returns formatted explanation with sections: Summary, Findings, Recommendations

Stage 4 โ€” Nearby Doctor Finder (Geoapify)

  • Detects dominant pathology from results
  • Maps pathology โ†’ specialist type (e.g., Pneumonia โ†’ Pulmonologist)
  • Uses browser geolocation to search for real nearby clinics
  • Falls back to General Physician if no specialist found

๐Ÿ“Š API Endpoints

Method Endpoint Description
GET /health Health check
POST /analyze Upload X-ray image for full analysis

Sample Response (/analyze)

{
  "binary_pipeline": [
    { "model": "Image Validation", "is_valid": true, "confidence": 0.98, "message": "Valid chest X-ray" }
  ],
  "valid_for_analysis": true,
  "clinical_summary": "โœ… NORMAL - No significant abnormalities detected",
  "assessment_level": "NORMAL",
  "pathologies": [
    { "name": "Pneumonia", "confidence": 0.12, "is_detected": false }
  ],
  "heatmap_b64": "data:image/png;base64,...",
  "ai_explanation": "## Summary\n...",
  "doctor_recommendations": [...],
  "processing_time_ms": 245.3
}

๐Ÿงฐ Tech Stack

Layer Technology
Frontend React 18, Vanilla CSS
Backend FastAPI, Uvicorn
ML Models PyTorch, DenseNet-121 (CheXNet)
AI Explainability Google Gemini 3.1 Flash Lite (google-genai)
Doctor Finder Geoapify Places API
Heatmaps Grad-CAM++, OpenCV

โšก Performance

  • Binary pipeline: ~50ms
  • CheXNet + heatmap: ~150โ€“250ms
  • Gemini AI explanation: ~1โ€“3s (network dependent)
  • GPU acceleration supported (CUDA)

๐Ÿ”’ Privacy & Security

  • All image processing is local โ€” no images sent to external servers
  • Only text summaries are sent to Gemini for explanation
  • API keys stored in .env (excluded from version control via .gitignore)
  • No patient data is logged or persisted

๐Ÿ› Troubleshooting

Issue Fix
Model files not found Ensure .pth files are in backend/models/
CUDA errors Set device = "cpu" in pipeline files
Gemini API errors Verify GEMINI_API_KEY in .env
Geoapify returns no results Check GEOAPIFY_API_KEY and confirm valid category names
Frontend can't reach backend Confirm backend is running on port 8000
CORS errors Check allow_origins in app.py FastAPI config

โš ๏ธ Medical Disclaimer

This system is designed for research and educational purposes only. It must not be used as a substitute for professional medical diagnosis. Always consult a qualified healthcare provider for clinical decisions.

๐Ÿ“„ License

For educational and research use only. See individual model licenses for CheXNet model weights.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chest_xray_diagnosis-1.0.0.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chest_xray_diagnosis-1.0.0-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

Details for the file chest_xray_diagnosis-1.0.0.tar.gz.

File metadata

  • Download URL: chest_xray_diagnosis-1.0.0.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for chest_xray_diagnosis-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9aeb1f4f62fe58c8b38fb95b0aafce4afc5870af64076a78b23cbfca54a0d4ce
MD5 eff25261f2218b0ebeca857f0ca7b1f2
BLAKE2b-256 6baa916f526ee39bb7352aaa12513cdfa829946d2953a5ff6972e26d74c42fea

See more details on using hashes here.

File details

Details for the file chest_xray_diagnosis-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for chest_xray_diagnosis-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d920c43838343432601d7ba4227396ab36b9cb90892c8c9e48eb3b4bbaa45436
MD5 45cd72154899098c66685a0f8e669238
BLAKE2b-256 b2cfcc38ca56be3c9ce580eeee2c9119a20fc6eaaf8f1d423c25fcb0f9bff369

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page