Skip to content

Ganesh-a0576/Health-Insight-Using-Machine-Learning-And-Flask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฅ Health-Insight โ€” ML-Powered Disease Prediction

Python Flask Scikit-learn Pandas NumPy License

An end-to-end ML-powered web application that predicts risk for multiple diseases in real time.

Live Demo ยท Report Bug ยท Request Feature


๐Ÿ“Œ Table of Contents


๐Ÿ“– About the Project

Health-Insight is a full-stack web application that integrates multiple machine learning models with a Flask backend to deliver real-time disease risk predictions directly in the browser.

Designed with production-style considerations in mind, this project addresses challenges like feature consistency between training and inference, model serialization, dynamic form generation, and robust error handling โ€” making it more than just a demo, but a template for real-world ML deployment.

โš ๏ธ Disclaimer: This application is intended for educational and research purposes only. It is not a substitute for professional medical advice, diagnosis, or treatment. Always consult a qualified healthcare provider.


๐Ÿฆ  Supported Diseases

Disease Model File Dataset
๐Ÿฉธ Diabetes diabetes.pkl diabetes.csv
โค๏ธ Heart Disease heart.pkl heart.csv
๐Ÿซ˜ Kidney Disease kidney.pkl kidney.csv
๐Ÿซ€ Liver Disease liver.pkl liver.csv
๐Ÿ”ฌ Cancer cancer.pkl cancer.csv

โœจ Features

  • ๐Ÿค– Individual ML model per disease โ€” each condition has its own dedicated RandomForestClassifier
  • ๐Ÿ“‹ Dynamic input forms โ€” automatically generated based on disease-specific feature sets
  • โšก Real-time predictions โ€” instant inference via Flask REST backend
  • ๐Ÿ’พ Model persistence โ€” serialized and loaded using Pickle for fast startup
  • โœ… Input validation โ€” client and server-side checks before inference
  • ๐Ÿ”ก Categorical encoding โ€” handles mixed data types in production
  • ๐Ÿงฑ Modular architecture โ€” clean separation of training, inference, and UI layers
  • ๐Ÿ“ฑ Responsive UI โ€” works across desktop and mobile browsers

๐Ÿ›  Tech Stack

Layer Technology
Backend Python 3.x, Flask
ML Framework Scikit-learn (RandomForestClassifier)
Data Handling Pandas, NumPy
Serialization Pickle
Frontend HTML5, CSS3, JavaScript
Templating Jinja2

โš™๏ธ How It Works

User selects a disease
        โ”‚
        โ–ผ
Dynamic form rendered with disease-specific input fields
        โ”‚
        โ–ผ
User submits health parameters
        โ”‚
        โ–ผ
Flask validates & encodes inputs
        โ”‚
        โ–ผ
Correct .pkl model loaded for the selected disease
        โ”‚
        โ–ผ
RandomForestClassifier runs inference
        โ”‚
        โ–ผ
Prediction result displayed on result.html
  1. Training Phase โ€” Each disease has a standalone training script (training/<disease>.py) that preprocesses the dataset, trains a RandomForestClassifier, and saves the model as a .pkl file.
  2. Inference Phase โ€” When a user submits a form, Flask loads the corresponding .pkl model, applies the same preprocessing pipeline, and returns the prediction.
  3. Feature Consistency โ€” Feature names and encoding schemes are kept consistent between training and inference to prevent silent prediction errors.

๐Ÿš€ Getting Started

Prerequisites

Ensure the following are installed on your system:

  • Python 3.x โ€” Download
  • pip โ€” comes with Python
  • Git โ€” Download

Installation

  1. Clone the repository:
git clone https://github.com/your-username/Health-Insight.git
cd Health-Insight
  1. Create and activate a virtual environment (recommended):
python -m venv venv
source venv/bin/activate        # macOS/Linux
venv\Scripts\activate           # Windows
  1. Install all dependencies:
pip install -r requirements.txt
  1. (Optional) Retrain the models:
python training/diabetes.py
python training/heart.py
python training/kidney.py
python training/liver.py
python training/cancer.py

Pre-trained .pkl files are included in the models/ directory so retraining is optional.


๐Ÿ’ป Usage

  1. Start the Flask development server:
python app.py
  1. Open your browser and visit:
http://127.0.0.1:5000/
  1. Select a disease, fill in the health parameters, and click Predict to receive your risk assessment instantly.

๐Ÿ“ Project Structure

Health-Insight/
โ”‚
โ”œโ”€โ”€ app.py                    # Main Flask app โ€” routes & inference logic
โ”œโ”€โ”€ requirements.txt          # All Python dependencies
โ”‚
โ”œโ”€โ”€ models/                   # Serialized trained ML models
โ”‚   โ”œโ”€โ”€ diabetes.pkl
โ”‚   โ”œโ”€โ”€ heart.pkl
โ”‚   โ”œโ”€โ”€ kidney.pkl
โ”‚   โ”œโ”€โ”€ liver.pkl
โ”‚   โ””โ”€โ”€ cancer.pkl
โ”‚
โ”œโ”€โ”€ training/                 # Standalone training scripts per disease
โ”‚   โ”œโ”€โ”€ diabetes.py
โ”‚   โ”œโ”€โ”€ heart.py
โ”‚   โ”œโ”€โ”€ kidney.py
โ”‚   โ”œโ”€โ”€ liver.py
โ”‚   โ””โ”€โ”€ cancer.py
โ”‚
โ”œโ”€โ”€ datasets/                 # Raw CSV datasets used for training
โ”‚   โ”œโ”€โ”€ diabetes.csv
โ”‚   โ”œโ”€โ”€ heart.csv
โ”‚   โ”œโ”€โ”€ kidney.csv
โ”‚   โ”œโ”€โ”€ liver.csv
โ”‚   โ””โ”€โ”€ cancer.csv
โ”‚
โ”œโ”€โ”€ templates/                # Jinja2 HTML templates
โ”‚   โ”œโ”€โ”€ index.html            # Landing page โ€” disease selector
โ”‚   โ”œโ”€โ”€ form.html             # Dynamic input form
โ”‚   โ””โ”€โ”€ result.html           # Prediction result display
โ”‚
โ”œโ”€โ”€ static/                   # Static assets
โ”‚   โ”œโ”€โ”€ css/                  # Stylesheets
โ”‚   โ””โ”€โ”€ js/                   # JavaScript files
โ”‚
โ””โ”€โ”€ README.md

๐Ÿ”Œ API Endpoints

Method Endpoint Description
GET / Home page โ€” disease selection
GET /predict/<disease> Load input form for selected disease
POST /predict/<disease> Submit form and return prediction result

Example POST body for diabetes prediction:

{
  "pregnancies": 2,
  "glucose": 138,
  "blood_pressure": 62,
  "skin_thickness": 35,
  "insulin": 0,
  "bmi": 33.6,
  "diabetes_pedigree": 0.627,
  "age": 47
}

๐Ÿ“Š Model Performance

Results from training on the provided datasets. Metrics may vary with different train/test splits.

Disease Algorithm Accuracy
Diabetes RandomForestClassifier ~76โ€“80%
Heart Disease RandomForestClassifier ~82โ€“86%
Kidney Disease RandomForestClassifier ~96โ€“99%
Liver Disease RandomForestClassifier ~72โ€“76%
Cancer RandomForestClassifier ~94โ€“97%

๐Ÿง  Key Learnings

  • Feature consistency โ€” Ensuring training feature names/order exactly match inference inputs to prevent silent errors
  • Categorical encoding in production โ€” Handling label encoding and one-hot encoding at inference time without refitting
  • Real-world ML deployment โ€” Debugging shape mismatches, missing values, and dtype inconsistencies
  • Modular backend design โ€” Separating training logic from inference for clean, maintainable code
  • Flask routing patterns โ€” Building dynamic, parameterized routes for multi-model applications

๐Ÿ”ฎ Future Improvements

  • REST API endpoints with JSON responses for mobile/external integration
  • User authentication and prediction history dashboard
  • Model monitoring, drift detection & automated retraining pipeline
  • Dockerized deployment with docker-compose
  • SHAP-based explainability โ€” show which features drove the prediction
  • Confidence scores alongside binary predictions
  • CI/CD pipeline with GitHub Actions

๐Ÿค Contributing

Contributions are welcome and appreciated!

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Commit your changes: git commit -m "Add: your feature description"
  4. Push to your branch: git push origin feature/your-feature
  5. Open a Pull Request

Please follow PEP 8 coding standards and include docstrings for any new functions.


๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for more information.


๐Ÿ“ฌ Contact

Your Name โ€” ganesh1a0576@gmail.com

GitHub: Ganesh-a0576

Project Link: https://github.com/your-username/Health-Insight


โญ If this project helped you, please consider giving it a star โ€” it means a lot!


Made with โค๏ธ and Python

About

Health-Insight is a full-stack web application that integrates multiple machine learning models with a Flask backend to provide real-time disease risk predictions. The system is designed with production-style considerations such as feature consistency, model serialization, dynamic form generation, and robust error handling.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors