Skip to content

Emart29/Heart-Disease-Prediction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

❀️ Heart Disease Prediction

CI/CD Pipeline Python 3.11+ Code style: black Docker License: MIT

A production-ready machine learning system for heart disease risk assessment, featuring a FastAPI REST API, Streamlit dashboard, SHAP explainability, MLflow experiment tracking, and Dockerized deployment.

From notebook β†’ production-grade application with full CI/CD.


🎯 Project Overview

This project predicts the likelihood of heart disease based on 13 clinical features and provides explainable predictions with SHAP values.

Key Features

Feature Description
πŸš€ REST API FastAPI service with auto-generated docs
πŸ–₯️ Web Interface Interactive Streamlit dashboard
πŸ” Explainable AI SHAP-based feature importance
πŸ“Š MLOps Ready MLflow tracking, versioned models
🐳 Containerized Docker support for consistent deployment
βœ… Well-Tested pytest + property-based tests
πŸ”„ CI/CD GitHub Actions for linting, tests, and Docker build

πŸ“Š Model Performance

Metric Score
Accuracy 88.5%
ROC-AUC 0.954
Precision 86.2%
Recall 89.3%
F1 Score 87.7%
  • Model: Random Forest Classifier
  • Features: 13 clinical attributes + 22 engineered features
  • Dataset: UCI Heart Disease

πŸ—οΈ Architecture

User Interface
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Streamlit App β”‚       β”‚ Swagger UI    β”‚
 β”‚ (Port 8501)   β”‚       β”‚ (Port 8000)   β”‚
 β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                       β”‚
       β–Ό                       β–Ό
 API Layer
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ FastAPI Server                          β”‚
 β”‚ β€’ /health    β€’ /model-info   β€’ /predict β”‚
 β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
 ML Pipeline
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Feature Eng  β”‚β†’ β”‚ Model       β”‚β†’ β”‚ SHAP Explainer  β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
 MLOps Layer
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ MLflow Registry                         β”‚
 β”‚ β€’ Experiment tracking                   β”‚
 β”‚ β€’ Model versioning                      β”‚
 β”‚ β€’ Artifacts storage                     β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

 CI/CD Pipeline (GitHub Actions)
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ On Push/PR:                             β”‚
 β”‚ β€’ Lint (black, flake8)                  β”‚
 β”‚ β€’ Test (pytest)                         β”‚
 β”‚ β€’ Build (Docker)                        β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • pip or conda
  • Docker (optional)

Local Installation

# Clone repo
git clone https://github.com/Emart29/heart-disease-prediction.git
cd heart-disease-prediction

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# venv\Scripts\activate   # Windows

# Install dependencies
pip install -r requirements.txt

Run the API

uvicorn api.main:app --host 0.0.0.0 --port 8000

πŸ“ API Docs: http://localhost:8000/docs

Run Streamlit App

streamlit run app/streamlit_app.py

πŸ“ Dashboard: http://localhost:8501


🐳 Docker Deployment

Using Docker Compose (Recommended)

# Build and start all services
docker-compose up --build

# Run in detached mode
docker-compose up -d --build

# Stop services
docker-compose down

Using Docker Directly

# Build image
docker build -t heart-disease-prediction .

# Run API
docker run -p 8000:8000 heart-disease-prediction

# Run Streamlit
docker run -p 8501:8501 heart-disease-prediction \
  streamlit run app/streamlit_app.py --server.port=8501 --server.address=0.0.0.0

πŸ”„ CI/CD Pipeline

This project includes a fully automated CI/CD pipeline using GitHub Actions:

Step Tool Purpose
Linting black, flake8 Code formatting & style
Testing pytest Unit & property-based tests
Coverage pytest-cov Code coverage reporting
Build Docker Container build verification

Pipeline Triggers

  • βœ… On push to main
  • βœ… On pull requests
  • βœ… Manual dispatch

Running CI Locally

# Format code
black src/ api/ tests/

# Lint
flake8 src/ api/ tests/

# Run tests
pytest --cov=src --cov=api --cov-report=html

πŸ§ͺ Testing

# Run all tests
pytest

# With coverage
pytest --cov=src --cov=api --cov-report=html

# Run specific test file
pytest tests/test_api.py -v

Test Coverage:

  • βœ… Model loading & prediction
  • βœ… Feature engineering
  • βœ… API endpoints
  • βœ… Data validation
  • βœ… Property-based tests (Hypothesis)

πŸ“‘ API Endpoints

Health Check

GET /health
{
  "status": "healthy",
  "model_loaded": true
}

Model Info

GET /model-info
{
  "version": "1.0.0",
  "model_type": "RandomForestClassifier",
  "features": ["age", "sex", "cp", ...],
  "training_date": "2025-12-29",
  "metrics": {
    "accuracy": 0.885,
    "roc_auc": 0.954
  }
}

Predict

POST /predict
{
  "age": 55,
  "sex": 1,
  "cp": 3,
  "trestbps": 140,
  "chol": 250,
  "fbs": 0,
  "restecg": 0,
  "thalach": 150,
  "exang": 0,
  "oldpeak": 1.5,
  "slope": 2,
  "ca": 0,
  "thal": 3
}

Response:

{
  "prediction": 1,
  "probability": 0.6523,
  "risk_level": "Medium",
  "feature_importance": {
    "age": 0.0234,
    "thalach": -0.0456,
    "ca": 0.0891
  }
}

πŸ“ Project Structure

heart_disease_prediction/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci.yml          # CI/CD pipeline
β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ config.py
β”‚   β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ features/
β”‚   β”‚   └── engineering.py
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ train.py
β”‚   β”‚   └── predict.py
β”‚   └── validation/
β”‚       └── schemas.py
β”œβ”€β”€ api/                    # FastAPI
β”‚   └── main.py
β”œβ”€β”€ app/                    # Streamlit
β”‚   └── streamlit_app.py
β”œβ”€β”€ tests/                  # Unit & property-based tests
β”œβ”€β”€ models/                 # Saved model artifacts
β”œβ”€β”€ data/                   # Datasets
β”œβ”€β”€ mlruns/                 # MLflow artifacts
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ pyproject.toml
└── README.md

πŸ”¬ MLflow Tracking

# Train a new model with MLflow
python -m src.models.train --experiment-name "heart_disease_v2" --n-estimators 200

# Launch MLflow UI
mlflow ui --port 5000

πŸ› οΈ Tech Stack

Category Technologies
ML/Data scikit-learn, pandas, numpy, SHAP
API FastAPI, Pydantic, uvicorn
Web Streamlit, Plotly
MLOps MLflow
Testing pytest, hypothesis
CI/CD GitHub Actions
Container Docker, Docker Compose

πŸ“„ License

MIT License – see LICENSE file.


πŸ“¬ Contact


⭐ Star This Repo

If you found this project helpful, please give it a star! It helps others discover the project.

Star History

About

πŸ«€ Production-ready ML system for heart disease risk assessment with 88.5% accuracy. Features FastAPI REST API, Streamlit dashboard, SHAP explainability, MLflow tracking, and Docker deployment. Demonstrates end-to-end ML engineering from notebook to production-grade application.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors