Skip to content

harpertoken/xgboost-api

Repository files navigation

XGBoost API

Flask API for XGBoost ML predictions.

What is this?

This repository provides a machine learning API using XGBoost, a popular gradient boosting library. It trains a model on sample data and serves predictions via HTTP endpoints. XGBoost is used for tasks like classification or regression, making this a machine learning application.

Keywords: python, flask, xgboost, machine-learning, api, docker, kubernetes, gpu

Quick Start

  1. Install dependencies:

    pip install -r requirements.txt
  2. Train the model:

    python train.py
  3. Run tests:

    pip install -r requirements.txt
    pytest
  4. Run the API:

    python app.py

Conventional Commits

This project follows conventional commit standards for commit messages.

Setup

To enable commit message validation:

cp scripts/commit-msg .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg

Commit Message Format

Commit messages must:

  • Start with a type: feat:, fix:, docs:, style:, refactor:, test:, chore:, perf:, ci:, build:, revert:
  • Be entirely lowercase
  • Have first line ≤60 characters

Example: feat: add user authentication endpoint

Rewriting History

To clean up existing commit messages:

git filter-branch --msg-filter 'bash scripts/rewrite_msg.sh' -- --all
git push --force-with-lease

API Endpoints

  • GET /health: Health check
  • POST /predict: Make predictions
    • Body: {"features": [array_of_features]}
    • Response: {"prediction": [values], "shape": [dims]}

Usage Examples

Health check:

curl http://localhost:8000/health

Prediction:

curl -X POST http://localhost:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"features": [1.0, 2.0, 3.0, 4.0]}'

Response: {"id":"uuid","prediction":[0.99...],"shape":[1,4]}

Requirements

  • Python 3.9+ (due to scikit-learn 1.5.0 requirements)
  • Optional: NVIDIA GPU with CUDA support
  • For GPU: Docker with NVIDIA Container Toolkit, Kubernetes with NVIDIA device plugin

Mac Setup

Install OpenMP:

brew install libomp
export LDFLAGS="-L/opt/homebrew/opt/libomp/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libomp/include"

Deployment

Docker:

docker build -t bniladridas/flask-xgboost-api .
docker run --gpus all -p 8000:8000 bniladridas/flask-xgboost-api

Test:

  1. docker run -p 8000:8000 bniladridas/flask-xgboost-api
  2. curl http://localhost:8000/health{"status":"healthy"}
  3. curl -X POST http://localhost:8000/predict -H "Content-Type: application/json" -d '{"features": [1.0, 2.0, 3.0, 4.0]}' → prediction JSON

Kubernetes:

# Install minikube: https://minikube.sigs.k8s.io/docs/start/
minikube start
kubectl apply -f kubernetes/

Requires a Kubernetes cluster (group of nodes for container management).

About

flask API for XGBoost ML predictions.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •