Chapter 3.3 - Build and publish the model with BentoML and Docker locally¶
Introduction¶
Serving the model locally is great for testing purposes, but it is not sufficient for production. In this chapter, you will learn how to build and publish the model with BentoML and Docker.
This will allow to share the model with others and deploy it later in a production environment.
In this chapter, you will learn how to:
- Create a BentoML model artifact
- Containerize the model artifact with BentoML and Docker
- Test the containerized model artifact by serving it locally with Docker
The following diagram illustrates the control flow of the experiment at the end of this chapter:
flowchart TB
workspaceGraph <-....-> dot_git
data[data/raw]
subgraph cacheGraph[CACHE]
dot_dvc[(.dvc)]
dot_git[(.git)]
bento_artifact[(Containerized
artifact)]
end
subgraph workspaceGraph[WORKSPACE]
data --> code[*.py]
subgraph dvcGraph["dvc.yaml"]
code
end
params[params.yaml] -.- code
code <--> bento_model[classifier.bentomodel]
subgraph bentoGraph[bentofile.yaml]
bento_model
serve[serve.py] <--> bento_model
fastapi[FastAPI] <--> |bento serve|serve
end
bentoGraph -->|bento build
bento containerize| bento_artifact
bento_model <-.-> dot_dvc
end
subgraph browserGraph[BROWSER]
localhost <--> |docker run|bento_artifact
localhost <--> |bento serve| fastapi
end
style workspaceGraph opacity:0.4,color:#7f7f7f80
style dvcGraph opacity:0.4,color:#7f7f7f80
style cacheGraph opacity:0.4,color:#7f7f7f80
style data opacity:0.4,color:#7f7f7f80
style dot_git opacity:0.4,color:#7f7f7f80
style dot_dvc opacity:0.4,color:#7f7f7f80
style code opacity:0.4,color:#7f7f7f80
style serve opacity:0.4,color:#7f7f7f80
style bento_model opacity:0.4,color:#7f7f7f80
style fastapi opacity:0.4,color:#7f7f7f80
style params opacity:0.4,color:#7f7f7f80
linkStyle 0 opacity:0.4,color:#7f7f7f80
linkStyle 1 opacity:0.4,color:#7f7f7f80
linkStyle 2 opacity:0.4,color:#7f7f7f80
linkStyle 3 opacity:0.4,color:#7f7f7f80
linkStyle 4 opacity:0.4,color:#7f7f7f80
linkStyle 5 opacity:0.4,color:#7f7f7f80
linkStyle 6 opacity:0.4,color:#7f7f7f80
linkStyle 7 opacity:0.4,color:#7f7f7f80
linkStyle 8 opacity:0.4,color:#7f7f7f80
Steps¶
Create a BentoML model artifact¶
A BentoML model artifact (called "Bento" in the documentation) packages your model, code, and environment dependencies into a single file. It is the standard format for saving and sharing ML models.
The BentoML model artifact is described in a bentofile.yaml
file. It contains the following information:
- The service filename and class name
- The Python packages required to run the service
- The Docker configuration, such as the Python version to use
Create a new bentofile.yaml
file in the src
directory with the following content:
src/bentofile.yaml | |
---|---|
Do not forget to include the serve.py
file in the BentoML model artifact. This file contains the code to serve the model with FastAPI as you have seen in the previous chapter.
The python
section contains the Python packages required to run the service. It does not contain DVC and other packages to build the model, as they are not required to run the service.
The docker
section contains the Python version to use. It is important to specify the Python version to ensure the service runs correctly.
Now that the bentofile.yaml
file is created, you can serve the model with the following command:
Build the BentoML model artifact¶
Before containerizing the BentoML model artifact with Docker, you need to build it.
A BentoML model artifact can be built with the following command:
Execute the following command(s) in a terminal | |
---|---|
The output should be similar to this:
All Bentos can be listed with the following command:
The output should be similar to this:
Containerize the BentoML model artifact with Docker¶
Now that the BentoML model artifact is built, you can containerize it with the following command:
Execute the following command(s) in a terminal | |
---|---|
The first :latest
is the tag of the BentoML model artifact. It is a symlink to the latest version of the BentoML model artifact.
The output should be similar to this:
Test the containerized BentoML model artifact locally¶
The BentoML model artifact is now containerized. To verify its behavior, serve the model artifact locally by running the Docker image:
Execute the following command(s) in a terminal | |
---|---|
Congrats! You have successfully containerized the BentoML model artifact using Docker. You have also tested the container by running it locally. The model is now ready to be shared on a container registry.
Check the changes¶
Check the changes with Git to ensure that all the necessary files are tracked:
Execute the following command(s) in a terminal | |
---|---|
The output should look similar to this:
Commit the changes to Git¶
Commit the changes to Git.
Execute the following command(s) in a terminal | |
---|---|
Summary¶
Congratulations! You have successfully prepared the model for deployment in a production environment.
In this chapter, you have successfully:
- Created and containerized a BentoML model artifact
State of the MLOps process¶
- Notebook has been transformed into scripts for production
- Codebase and dataset are versioned
- Steps used to create the model are documented and can be re-executed
- Changes done to a model can be visualized with parameters, metrics and plots to identify differences between iterations
- Model can be saved and loaded with all required artifacts for future usage
- Model can be easily used outside of the experiment context
Sources¶
Highly inspired by: