An AI-powered chest X-ray analysis tool that combines a custom Convolutional Neural Network (CNN) with Grad-CAM (Gradient-weighted Class Activation Mapping) visual explanations and an interactive Google Gemini chatbot.
This project was built to address the "black box" problem in medical AI by providing verifiable, anatomically grounded explanations for its classifications (Pneumonia, Tuberculosis, or Normal).
- 3-Class Classification: Identifies Pneumonia, Tuberculosis, and Normal chest X-rays using a reverse-engineered PyTorch CNN.
- Grad-CAM Heatmaps: Visually highlights the specific regions of the X-ray (e.g., Right Upper Lobe) that most strongly influenced the model's decision.
- Interactive AI Medical Assistant: A Gemini-powered (2.5 Flash) chatbot integrated directly into the UI. You can ask questions about the analysis, the Grad-CAM methodology, or the specific anatomical findings.
- Evidence-Based Explanations: The agent operates under a strict "Grounding Protocol," ensuring explanations are based only on the visual evidence and model activations, without hallucinating clinical histories.
- Premium Interface: A beautiful, dark-themed medical UI built with Gradio and custom CSS, featuring glassmorphism and animated components.
graph LR
A["X-Ray Image"] --> B["Preprocess<br/>(224×224, Normalize)"]
B --> C["MedicalCNN"]
C --> D["Classification<br/>(3 classes)"]
C --> E["Grad-CAM<br/>(features.11 hook)"]
E --> F["Heatmap Overlay"]
D --> G["Explainer Agent"]
E --> G
G --> H["Structured<br/>Explanation"]
app.py: The main Gradio application and UI construction, including the chatbot interaction loop.model.py: The PyTorch CNN architecture definition and layer structure.gradcam.py: Custom PyTorch implementation of Grad-CAM to extract feature gradients and generate heatmaps.explainer.py: Logic for formatting the Grad-CAM outputs into structured clinical observations.best_medical_model.pth: The pre-trained model weights.
-
Clone the repository:
git clone https://github.com/IDKHowToCodeFR/GradCam-Agent-DWDM.git cd GradCam-Agent-DWDM -
Install the required dependencies: Make sure you have Python installed, then run:
pip install torch torchvision numpy Pillow opencv-python gradio google-generativeai
-
Configure the API Key: The
app.pyuses Google's Gemini API for the interactive chatbot. Ensure you have a valid API key with access to thegemini-2.5-flashmodel and update theGEMINI_API_KEYvariable inapp.py.
Run the web application locally:
python app.pyThe interface will automatically launch in your browser at http://localhost:7860.
- Upload a chest X-ray image.
- The application will instantly display the Grad-CAM heatmap, the classification result, and a detailed text explanation.
- Scroll down to the AI Medical Assistant section to ask follow-up questions about the findings!
This tool is for educational and research purposes only. It is not a substitute for professional medical diagnosis. All analysis is strictly limited to visual evidence within the provided X-ray image and the model's learned feature representations. No clinical history, symptoms, or demographic data are inferred or implied.