Skip to content

A machine learning-powered REST API for predicting the presence of heart disease based on patient clinical data. Built using FastAPI and Dockerized for easy deployment. The final model uses a tuned Support Vector Machine (SVM) pipeline trained on healthcare data.

Notifications You must be signed in to change notification settings

Amaan-developpeur/HeartPredictionAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Heart Disease Predictor API

A machine learning project to predict the presence of heart disease using clinical and demographic data, deployed using a FastAPI backend and containerized with Docker.


Overview

This project develops and deploys a machine learning model to predict the likelihood of heart disease based on patient clinical features such as age, cholesterol level, chest pain type, and others.

Multiple classification algorithms were trained and evaluated, including Logistic Regression, Support Vector Classifier (SVC), K-Nearest Neighbors (KNN), Decision Tree, Random Forest, AdaBoost, and XGBoost. After comparative model selection, SVC was chosen for its superior performance on the validation set.

The selected model is served via a RESTful API built with FastAPI. The entire application is containerized with Docker, enabling consistent and portable deployment across environments that support containers.


Project Structure

HeartPredictionAPI/
├── main.py               # FastAPI app
├── svm_pipeline.pkl      # Trained ML pipeline
├── model_training.ipynb  # Model development notebook (optional)
├── requirements.txt      # Python dependencies
├── Dockerfile            # Docker container instructions
├── README.md             # Project documentation
├── .gitignore
└── images/               # Screenshots for API demo

Dataset

The dataset is taken from Kaggle, which includes clinical features such as:

  • Age, sex, chest pain type (cp), resting blood pressure (trestbps), cholesterol (chol)
  • Fasting blood sugar (fbs), ECG (restecg), max heart rate (thalach)
  • Exercise-induced angina (exang), ST depression (oldpeak), number of vessels (ca), and thalassemia type (thal)

Final Model

  • Algorithm: Support Vector Machine (RBF Kernel)
  • Pipeline: StandardScaler + SVM using Pipeline from scikit-learn
  • Exported As: svm_pipeline.pkl using pickle
  • Tuned With: GridSearchCV
  • Evaluated Using: Accuracy, Precision, Recall, F1-Score, and SHAP for model explainability

Local Execution (via FastAPI)

1. Environment Setup

conda create -n heartapi python=3.12 -y
conda activate heartapi

2. Clone the Repository

git clone https://github.com/Amaan-developpeur/HeartPredictionAPI.git
cd HeartPredictionAPI

3. Install Dependencies

pip install -r requirements.txt

4. Run the API Server

uvicorn main:app --reload

Visit the interactive Swagger UI at:
📎 http://127.0.0.1:8000/docs


Sample API Request

{
  "age": 55,
  "sex": 1,
  "cp": 2,
  "trestbps": 130,
  "chol": 250,
  "fbs": 0,
  "restecg": 1,
  "thalach": 160,
  "exang": 0,
  "oldpeak": 1.5,
  "slope": 2,
  "ca": 0,
  "thal": 3
}

Docker Setup

You can run the entire app in a containerized environment using Docker.

1. Build the Docker Image

docker build -t heart-api .

2. Run the Container

docker run -p 8000:8000 heart-api

Then open your browser to:
📎 http://localhost:8000/docs


API Demo

🔹 Swagger UI Input Form

Users can submit patient data here:

Swagger Input


🔹 API Response with Prediction

The model returns a clear message about heart disease risk:

Prediction Output


🔹 Terminal Running FastAPI

Server running successfully with Uvicorn:

Uvicorn Terminal


Requirements

See requirements.txt for exact versions. Core dependencies include:

  • fastapi
  • uvicorn
  • pandas
  • scikit-learn
  • numpy
  • python-multipart


Author

Mohammed Amaan
📎 GitHub: Amaan-developpeur

About

A machine learning-powered REST API for predicting the presence of heart disease based on patient clinical data. Built using FastAPI and Dockerized for easy deployment. The final model uses a tuned Support Vector Machine (SVM) pipeline trained on healthcare data.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages