license: mit
title: Pneumonia Detection with ResNet50
sdk: docker
Pneumonia Detection with ResNet50
This is a Flask application for detecting pneumonia from chest X-ray images using a pre-trained ResNet50 model.
title: AI-Powered Pneumonia Detection System emoji: π« colorFrom: '#667eea' colorTo: '#764ba2' sdk: docker app_file: app.py license: apache-2.0 tags: - medical - healthcare - pneumonia - deep-learning - computer-vision - image-classification - flask - docker - tensorflow - generative-ai - llm - resnet
π« AI-Powered Pneumonia Detection System
This Hugging Face Space hosts a sophisticated, multi-stage AI system designed for the analysis of chest X-ray images to detect signs of pneumonia. The application combines a powerful ResNet50 Convolutional Neural Network (CNN) for initial classification with a Large Language Model (LLM) to generate a comprehensive, human-readable radiology report.
β¨ Key Features
- Deep Learning Classification: Utilizes a pre-trained ResNet50 model to accurately classify chest X-rays as either
NORMALorPNEUMONIA. - Generative AI Report Synthesis: Employs state-of-the-art LLMs via the OpenRouter API to analyze the image and the CNN's findings, generating a detailed, structured report that mimics a radiologist's summary.
- User-Friendly Interface: A clean, intuitive web interface built with Flask allows for easy image upload and clear presentation of results.
- Downloadable PDF Reports: Users can download the complete, formatted AI-generated medical report as a PDF for offline viewing or sharing.
- Robust & Scalable: Containerized with Docker and served with Gunicorn, ensuring a reliable and efficient user experience.
π How to Use the Application
- Upload Image: Click the "Choose X-Ray Image" button and select a chest X-ray file (
.jpg,.jpeg, or.png). - Analyze: Click the "Analyze Image" button to start the multi-stage analysis.
- Review Results:
- The ResNet50 model's prediction (
PNEUMONIAorNORMAL) and confidence score will be displayed. - A detailed, structured report generated by an LLM will appear below the prediction.
- The ResNet50 model's prediction (
- Download Report: Click the "Download Complete PDF Report" button to save a professionally formatted PDF of the analysis.
π¨ Critical Medical Disclaimer
This application is an educational tool and a demonstration of AI capabilities in medical imaging. It is NOT a certified medical device.
- The analysis provided does NOT constitute a clinical diagnosis or medical advice.
- All findings require confirmation by a qualified healthcare professional.
- This tool should be used for informational and research purposes only. Never disregard professional medical advice or delay in seeking it because of something you have read or seen on this application.
π οΈ Technical Breakdown
This application operates in two main stages:
Stage 1: CNN Image Classification
- Model: A
ResNet50Convolutional Neural Network, fine-tuned for binary classification of chest X-rays. - Framework:
TensorFlow/Keras. - Input: The uploaded image is preprocessed by resizing it to
128x128pixels and normalizing its values. - Output: The model outputs a prediction score, which is translated into a class label (
PNEUMONIAorNORMAL) and a confidence percentage.
Stage 2: Generative AI Report Synthesis
- Service: The application uses the OpenRouter API to access a variety of powerful Large Language Models (LLMs) with vision capabilities.
- Process: The base64-encoded image, the CNN's prediction, and the confidence score are sent to the LLM within a highly detailed prompt. This prompt instructs the AI to act as an experienced radiologist and generate a comprehensive report.
- Output: A structured and detailed medical analysis in Markdown format, covering aspects like technical assessment, cardiac and pulmonary analysis, clinical recommendations, and more.
Technology Stack
- Backend: Python with Flask Web Framework.
- ML/DL: TensorFlow, OpenCV, NumPy.
- Generative AI: OpenRouter API for LLM access.
- PDF Generation: WeasyPrint library.
- Deployment: Docker container served by Gunicorn.
- Frontend: HTML, CSS, and vanilla JavaScript.
π» How to Run Locally
To run this application on your local machine, follow these steps:
Clone the Repository:
git clone [https://huggingface.co/spaces/Arihant0008/Pneumonia-Detector-App](https://huggingface.co/spaces/Arihant0008/Pneumonia-Detector-App) cd Pneumonia-Detector-AppSet Up Environment: It's recommended to use a virtual environment.
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`Install Dependencies:
pip install -r requirements.txtSet Environment Variables: You need an API key from OpenRouter.ai. Create a
.envfile in the root directory and add your key:OPENROUTER_API_KEY="your-openrouter-api-key-here"The application will load this variable.
Run the Application:
python app.pyThe application will be available at
http://127.0.0.1:7860.
π Repository Structure
βββ model_weights/ β βββ ResNet50_Pneumonia_model.keras # The pre-trained CNN model βββ static/ β βββ uploads/ # For storing user-uploaded images βββ templates/ β βββ index.html # The main HTML file for the UI βββ app.py # The core Flask application logic βββ Dockerfile # Instructions to build the Docker image βββ requirements.txt # Python dependencies βββ README.md