Skip to content

Latest commit

Β 

History

History
Β 
Β 

xgboost

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

BentoML XGBoost Demo

This is a sample project demonstrating basic usage of BentoML with XGBoost.

Install Dependencies

Install python packages required for running this project:

pip install -r ./requirements.txt

Model Training

First step, train a classification model with the UCI Machine Learning Repository Agaricus mushroom dataset and save the model with BentoML:

python train.py

This should save a new model in the BentoML local model store:

bentoml models list agaricus

Verify that the model can be loaded as runner from Python shell:

import bentoml

runner = bentoml.xgboost.get("agaricus:latest").to_runner()

runner.run([0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1])  # => array(0.01241208, dtype=float32)

Create ML Service

The ML Service code is defined in the agaricus.py file:

# agaricus.py
import typing

import bentoml
import xgboost
from bentoml.io import NumpyNdarray, File

if typing.TYPE_CHECKING:
    import numpy as np

agaricus_runner = bentoml.xgboost.get("agaricus:latest").to_runner()

svc = bentoml.Service("agaricus", runners=[agaricus_runner])


@svc.api(input=NumpyNdarray(), output=NumpyNdarray())
def classify(input_data: "np.ndarray") -> "np.ndarray":
    return agaricus_runner.run(input_data)

Start an API server locally to test the service code above:

bentoml serve agaricus:svc --reload

With the --reload flag, the API server will automatically restart when the source file agaricus.py is being edited, to boost your development productivity.

Verify the endpoint can be accessed locally:

curl -X POST -H "content-type: application/json" --data "[[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1]]" http://127.0.0.1:3000/classify

Build Bento for deployment

A bentofile for the agaricus service is also contained in this directory:

service: "agaricus:svc"
description: "file: ./README.md"
labels:
  owner: bentoml-team
  stage: demo
include:
  - "*.py"
exclude:
  - "locustfile.py"
python:
  packages:
    - xgboost

Simply run bentoml build from this directory to build a Bento with the latest version of the agaricus model. This may take a while when running for the first time, as BentoML needs to resolve all dependency versions:

> bentoml build

03/07/2022 12:25:16 PM INFO     [cli] Building BentoML service "agaricus:uvkv7d46cgnvgeb5" from build context "/home/user/devel/gallery/xgboost"
03/07/2022 12:25:16 PM INFO     [cli] Packing model "agaricus:3t4533c6ufi6zcz2ca6rzl235" from "/home/user/bentoml/models/agaricus/3t4533c6ufi6zcz2ca6rzl235"
03/07/2022 12:25:16 PM INFO     [cli] Successfully saved Model(tag="agaricus:3t4533c6ufi6zcz2ca6rzl235",
                                path="/run/user/1000/tmpw8lyba_sbentoml_bento_agaricus/models/agaricus/3t4533c6ufi6zcz2ca6rzl235/")
03/07/2022 12:25:16 PM INFO     [cli] Locking PyPI package versions..
03/07/2022 12:25:17 PM INFO     [cli]
                                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ•—β–‘β–‘β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ•—β–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•—β–‘β–‘β–‘β–‘β–‘
                                β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ•‘β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–‘β–‘
                                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•¦β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–‘β–ˆβ–ˆβ•”β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β–ˆβ–ˆβ–ˆβ–ˆβ•”β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–‘β–‘
                                β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β–‘β–‘β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–‘β–‘
                                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•¦β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–‘β•šβ–ˆβ–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–‘β•šβ•β•β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
                                β•šβ•β•β•β•β•β•β–‘β•šβ•β•β•β•β•β•β•β•šβ•β•β–‘β–‘β•šβ•β•β•β–‘β–‘β–‘β•šβ•β•β–‘β–‘β–‘β–‘β•šβ•β•β•β•β•β–‘β•šβ•β•β–‘β–‘β–‘β–‘β–‘β•šβ•β•β•šβ•β•β•β•β•β•β•

03/07/2022 12:25:17 PM INFO     [cli] Successfully built Bento(tag="agaricus:uvkv7d46cgnvgeb5") at "/home/user/bentoml/bentos/agaricus/uvkv7d46cgnvgeb5/"

This Bento can now be served:

bentoml serve agaricus:latest --production

The Bento directory contains all code, files, models and configuration required to run this service. BentoML standarizes this file structure, enabling serving runtimes and deployment tools to be built on top of it. By default, Bentos are managed under the ~/bentoml/bentos directory:

> cd $(bentoml get agaricus:latest -o path)

> tree
.
β”œβ”€β”€ apis
β”‚Β Β  └── openapi.yaml
β”œβ”€β”€ bento.yaml
β”œβ”€β”€ env
β”‚Β Β  β”œβ”€β”€ conda
β”‚Β Β  β”œβ”€β”€ docker
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ Dockerfile
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ entrypoint.sh
β”‚Β Β  β”‚Β Β  └── init.sh
β”‚Β Β  └── python
β”‚Β Β      β”œβ”€β”€ requirements.lock.txt
β”‚Β Β      β”œβ”€β”€ requirements.txt
β”‚Β Β      └── version.txt
β”œβ”€β”€ models
β”‚Β Β  └── agaricus
β”‚Β Β      β”œβ”€β”€ 3t4533c6ufi6zcz2ca6rzl235
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ model.yaml
β”‚Β Β      β”‚Β Β  └── saved_model.json
β”‚Β Β      └── latest
β”œβ”€β”€ README.md
└── src
    β”œβ”€β”€ agaricus.py
    └── train.py

9 directories, 14 files

Containerize Bento for deployment

Make sure you have docker installed and the docker daemon is running. The following command will use your local docker environment to build a new docker image containing the Bento:

bentoml containerize agaricus:latest

To test out the newly created docker image:

docker run agaricus:<bento tag output> -p 3000:3000