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.
This project predicts the likelihood of heart disease based on 13 clinical features and provides explainable predictions with SHAP values.
| 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 |
| 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
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) β
βββββββββββββββββββββββββββββββββββββββββββ
- Python 3.11+
- pip or conda
- Docker (optional)
# 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.txtuvicorn api.main:app --host 0.0.0.0 --port 8000π API Docs: http://localhost:8000/docs
streamlit run app/streamlit_app.pyπ Dashboard: http://localhost:8501
# Build and start all services
docker-compose up --build
# Run in detached mode
docker-compose up -d --build
# Stop services
docker-compose down# 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.0This 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 |
- β
On push to
main - β On pull requests
- β Manual dispatch
# Format code
black src/ api/ tests/
# Lint
flake8 src/ api/ tests/
# Run tests
pytest --cov=src --cov=api --cov-report=html# Run all tests
pytest
# With coverage
pytest --cov=src --cov=api --cov-report=html
# Run specific test file
pytest tests/test_api.py -vTest Coverage:
- β Model loading & prediction
- β Feature engineering
- β API endpoints
- β Data validation
- β Property-based tests (Hypothesis)
GET /health{
"status": "healthy",
"model_loaded": true
}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
}
}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
}
}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
# 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| 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 |
MIT License β see LICENSE file.
- LinkedIn: linkedin.com/in/nwangumaemmanuel
- Email: nwangumaemmanuel29@gmail.com
- GitHub Issues: Open an issue
If you found this project helpful, please give it a star! It helps others discover the project.