Flask API for XGBoost ML predictions.
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
-
Install dependencies:
pip install -r requirements.txt
-
Train the model:
python train.py
-
Run tests:
pip install -r requirements.txt pytest
-
Run the API:
python app.py
This project follows conventional commit standards for commit messages.
To enable commit message validation:
cp scripts/commit-msg .git/hooks/commit-msg
chmod +x .git/hooks/commit-msgCommit 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
To clean up existing commit messages:
git filter-branch --msg-filter 'bash scripts/rewrite_msg.sh' -- --all
git push --force-with-leaseGET /health: Health checkPOST /predict: Make predictions- Body:
{"features": [array_of_features]} - Response:
{"prediction": [values], "shape": [dims]}
- Body:
Health check:
curl http://localhost:8000/healthPrediction:
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]}
- 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
Install OpenMP:
brew install libomp
export LDFLAGS="-L/opt/homebrew/opt/libomp/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libomp/include"Docker:
docker build -t bniladridas/flask-xgboost-api .
docker run --gpus all -p 8000:8000 bniladridas/flask-xgboost-apiTest:
docker run -p 8000:8000 bniladridas/flask-xgboost-apicurl http://localhost:8000/health→{"status":"healthy"}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).