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, designed to be easy for developers to integrate. This package provides an end-to-end pipeline that validates X-rays, detects 14 different pathologies, generates visual heatmaps, provides clinical AI explanations, and finds nearby doctors.
Available on both PyPI and NPM:
- Python (PyPI):
pip install chest-xray-diagnosis - Node.js (NPM):
npx chest-xray-diagnosis serve
🚀 Features & What Happens Under the Hood
When you feed an image into this package, it automatically processes through 4 stages:
-
3-Stage Binary Pipeline (Validation)
- Model 1 (MobileNetV2): Garbage vs. X-ray. Rejects non-medical images instantly.
- Model 2 (ResNet18): Chest vs. Other. Confirms the X-ray is actually of a chest.
- Model 3 (ResNet18): Normal vs. Abnormal. Performs an initial check. If Normal, it skips the heavy processing and generates health tips!
-
CheXNet Analysis (DenseNet-121)
- If the image is Abnormal, it runs through CheXNet.
- Detects 14 pathologies (Pneumonia, Cardiomegaly, Effusion, Mass, etc.) with probabilities and clinical thresholds.
- Grad-CAM++ Heatmaps: Automatically generates a visual heatmap (returned as a base64 PNG) showing exactly which region of the lungs the AI focused on.
-
Gemini AI Explainability
- Passes the clinical findings to Google Gemini 3.1 Flash Lite.
- Generates a warm, patient-friendly clinical explanation detailing symptoms to watch for and recommended precautions.
- (Works without an API key using a comprehensive rule-based fallback system).
-
Nearby Doctor Recommendations (Geoapify)
- Identifies the dominant pathology (e.g., Pneumonia).
- Maps the pathology to a specialist (e.g., Pulmonologist).
- Searches the Geoapify Places API to return real doctors and clinics near the user's provided GPS coordinates.
🧰 Tech Stack
- Core AI: PyTorch, TorchVision
- Models: DenseNet-121, ResNet-18, MobileNet-V2
- Image Processing: OpenCV, Pillow (PIL)
- API Server: FastAPI, Uvicorn
- External Integrations: Google GenAI (Gemini), Geoapify Places API
🔑 API Keys
The package is fully functional WITHOUT any API keys! The ML models (binary pipeline + CheXNet + heatmaps) are 100% local. However, for the best experience (AI explanations and real doctor search), you should provide keys.
You can configure keys in 4 ways:
- Interactive CLI Wizard:
chest-xray-diagnosis setup - Environment Variables:
GEMINI_API_KEYandGEOAPIFY_API_KEY .envfile in your current working directory- Passed explicitly to the
ChestXRayAnalyzerconstructor in Python
💻 CLI Usage
Once installed (pip install chest-xray-diagnosis), you have access to a powerful CLI.
1. Configure your keys
chest-xray-diagnosis setup
Interactive prompt that securely saves your Gemini and Geoapify keys to ~/.chexray-diagnosis/.env.
2. Pre-download models
chest-xray-diagnosis download-models
Downloads the ~230MB of model weights from HuggingFace Hub to your local cache.
3. Analyze a single image directly in your terminal
chest-xray-diagnosis analyze path/to/chest_xray.png
Output Example:
============================================================
Chest X-Ray Analysis Result
============================================================
Status : ⚠️ ABNORMAL — Significant findings: Infiltration, Effusion
Level : ABNORMAL
Normal : False
Time : 343 ms
Pathology Prob Confidence Decision
---------------------- ------ ---------- ----------
Infiltration 78.2% HIGH POSITIVE
Effusion 65.4% HIGH POSITIVE
Atelectasis 41.0% MEDIUM NEGATIVE
...
AI Explanation (Gemini 3.1 Flash Lite):
--------------------------------------------------
## 🩺 Important Findings in Your Chest X-ray
Your chest X-ray shows signs of **Infiltration**...
(You can also use --json to output the raw JSON data).
4. Start the FastAPI Server
chest-xray-diagnosis serve --port 8000
Starts a production-ready API server with CORS enabled. View the interactive Swagger docs at http://localhost:8000/docs.
🐍 Programmatic Usage (Python SDK)
If you are building your own Python backend, you can import the pipeline directly:
from chexray_diagnosis import ChestXRayAnalyzer
# Initializes the models (auto-downloads weights on first run)
analyzer = ChestXRayAnalyzer(
gemini_api_key="your-key-here", # Optional: reads from env if not provided
geoapify_api_key="your-key-here" # Optional: reads from env if not provided
)
# Run end-to-end analysis
result = analyzer.analyze("patient_xray.png")
print(result["is_normal"]) # True/False
print(result["clinical_summary"]) # "⚠️ ABNORMAL — Significant findings..."
print(result["pathologies"]) # List of 14 pathologies + probabilities
print(result["ai_explanation"]) # Markdown text from Gemini
print(result["heatmap_b64"]) # Base64 encoded PNG heatmap string
⚠️ 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
MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chest_xray_diagnosis-1.0.1.tar.gz.
File metadata
- Download URL: chest_xray_diagnosis-1.0.1.tar.gz
- Upload date:
- Size: 26.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90e78f5117bea25ad86761bec7f946636a7c81ddcd5c38b83fa5ee81efe114f0
|
|
| MD5 |
846997e61b7d572e6459b0cdb7fbc8af
|
|
| BLAKE2b-256 |
c8b677716f58c6bc9e31ba2aca8e8cb219d6226cb8ebc5dafd09cb28d884a685
|
File details
Details for the file chest_xray_diagnosis-1.0.1-py3-none-any.whl.
File metadata
- Download URL: chest_xray_diagnosis-1.0.1-py3-none-any.whl
- Upload date:
- Size: 31.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd5534f102c58a4d3e769c37480333ffadcd28489de5faa8efd6d7cab5dc045f
|
|
| MD5 |
3b9799ee7fd56cfaf9c8b1543a2fb5c7
|
|
| BLAKE2b-256 |
cfc956833bf5ad1f5d0486e47eda2c9cdff6a4cf9088324f65629bef7c1c24c5
|