Note
Go to the end to download the full example code
2. Deploy with Docker#
In this example, we deploy the model with Docker. For this example you need to install Docker, see Docker and XGBoost, see XGBoost.
Firstly, import mljet
.
import mljet
Import xgboost.XGBClassifier
as classifier
and sklearn.datasets
to load the iris dataset.
from sklearn.datasets import load_iris
from xgboost import XGBClassifier
Let’s load dataset and create and train a simple model.
X, y = load_iris(return_X_y=True)
clf = XGBClassifier()
clf.fit(X, y)
Now, we can deploy the model to docker with mljet.contrib.entrypoint.cook()
.
mljet.contrib.cook(
model=clf,
strategy="docker",
tag="mljet-xgboost",
port=5000,
need_run=True,
silent=True,
verbose=False,
)
Let’s see on passed parameters.
model - model to deploy - clf (XGBoost model)
strategy - strategy to use - docker
tag - tag for the docker image - mljet-xgboost
port - port for the docker container - 8000
backend - backend to use - sanic, see Sanic
need_run - run service after build or not - True (only create container)
silent - silent mode - True, non-blocking mode
verbose - verbose mode - True, print DEBUG logs
After calling the mljet.contrib.entrypoint.cook()
function
You can see build folder in the current directory.
And you can see the docker image and container with name mljet-xgboost.
Now we can send a request to the model. For this example, we use requests, see Requests. You can use any other tool, for example Postman. Firstly, import requests.
import time
import requests
Let’s sleep for 5 seconds and check the response.
time.sleep(5)
response = requests.post(
"http://localhost:5000/predict",
json={"data": X.tolist()},
)
print(response.json())
Total running time of the script: ( 0 minutes 0.000 seconds)