Arihant0008's picture
Update README.md
abc1368 verified
metadata
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 NORMAL or PNEUMONIA.
  • 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

  1. Upload Image: Click the "Choose X-Ray Image" button and select a chest X-ray file (.jpg, .jpeg, or .png).
  2. Analyze: Click the "Analyze Image" button to start the multi-stage analysis.
  3. Review Results:
    • The ResNet50 model's prediction (PNEUMONIA or NORMAL) and confidence score will be displayed.
    • A detailed, structured report generated by an LLM will appear below the prediction.
  4. 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 ResNet50 Convolutional Neural Network, fine-tuned for binary classification of chest X-rays.
  • Framework: TensorFlow / Keras.
  • Input: The uploaded image is preprocessed by resizing it to 128x128 pixels and normalizing its values.
  • Output: The model outputs a prediction score, which is translated into a class label (PNEUMONIA or NORMAL) 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:

  1. Clone the Repository:

    git clone [https://huggingface.co/spaces/Arihant0008/Pneumonia-Detector-App](https://huggingface.co/spaces/Arihant0008/Pneumonia-Detector-App)
    cd Pneumonia-Detector-App
    
  2. Set Up Environment: It's recommended to use a virtual environment.

    python -m venv venv
    source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
    
  3. Install Dependencies:

    pip install -r requirements.txt
    
  4. Set Environment Variables: You need an API key from OpenRouter.ai. Create a .env file in the root directory and add your key:

    OPENROUTER_API_KEY="your-openrouter-api-key-here"
    

    The application will load this variable.

  5. Run the Application:

    python app.py
    

    The 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