XGBoosting Home | About | Contact | Examples

Deploy XGBoost Model As Service with FastAPI

Deploying your trained XGBoost model as a web service allows you to make predictions on new data through a simple API, making it easier to integrate your model into applications.

This example demonstrates how to save a trained XGBoost model and create a FastAPI web service that loads the model and serves predictions via a REST API.

First, let’s train an XGBoost model on a synthetic dataset and save it to a file:

import joblib
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier

# Generate a synthetic dataset
X, y = make_classification(n_samples=1000, n_classes=2, n_features=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train an XGBClassifier
model = XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
model.fit(X_train, y_train)

# Save the trained model to a file
joblib.dump(model, 'xgb_model.pkl')

Now that we have a trained model saved to a file, let’s create a FastAPI application that loads the model and serves predictions.

First, install the necessary dependencies using pip:

pip install fastapi uvicorn joblib

Next, develop the FastAPI application that provides a web service for making predictions via HTTP requests:

import joblib
from fastapi import FastAPI
from pydantic import BaseModel
import numpy as np

app = FastAPI()

# Load the trained model
model = joblib.load('xgb_model.pkl')

class Features(BaseModel):
    features: list

def predict(data: Features):
    X = np.array(data.features)

    # Make predictions using the loaded model
    y_pred_proba = model.predict_proba(X)
    y_pred = model.predict(X)

    # Return the predictions as a JSON response
    response = {
        'probabilities': y_pred_proba.tolist(),
        'predictions': y_pred.tolist()
    return response

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host='', port=8000)

In this FastAPI application, we load the saved XGBoost model using joblib.load().

We define a Pydantic model called Features to validate and parse the incoming JSON data containing the input features.

The /predict endpoint accepts POST requests with JSON data containing the input features. The predict() function extracts the feature data from the validated JSON payload, uses the loaded model to make predictions, and returns the predicted class probabilities and labels as a JSON response.

To make predictions, send a POST request to the /predict endpoint with the input features as a JSON payload.

For example, using curl:

curl -X POST -H "Content-Type: application/json" -d '{"features": [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]]}' http://localhost:8000/predict

This request sends a single data point with 10 features to the /predict endpoint.

The server will respond with a JSON object containing the predicted class probabilities and labels:

	"probabilities":[[0.17284148931503296,0.827158510684967]], "predictions":[1]

By following this example, you can easily deploy your trained XGBoost model as a web service using FastAPI, allowing you to make predictions on new data through a simple API. This approach can be extended to handle more complex input data and can be integrated with other tools and frameworks to build end-to-end machine learning applications.

See Also