Vetiver: Model Deployment

[This article was first published on The Jumping Rivers Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is part one of a two part series on {vetiver}.

Introduction

In our previous blog, we provided an overview of MLOps and the {vetiver} package, creating and deploying a simple model locally. In this post, we’ll show you how to deploy a model to production using Posit Connect, SageMaker, and Docker.

What is Docker

Docker is an open-source platform that allows developers to build, deploy, and run containers. These containers bundle application source code with the operating system libraries and dependencies needed to run that code.

Previously, we discussed deploying a Shiny Application using Docker. Similarly, we can deploy a set of APIs to access our model.


Data comes in all shapes and sizes. It can often be difficult to know where to start. Whatever your problem, Jumping Rivers can help.


Creating a Docker file

The {vetiver} package simplifies creating a Dockerfile. We simply run:

vetiver::vetiver_prepare_docker(
 pins::board_connect(),
 "colin/k-nn",
 docker_args = list(port = 8080)
)

This command accomplishes several tasks:

  • Uses the {renv} package to create a list of R package dependencies required to run your model.
  • Creates a file named plumber.R containing the necessary code to deploy an API, essentially just vetiver_api().
  • Generates the Dockerfile.

The Dockerfile includes several components. The first component sets the R version, specifies the package repository, and crucially, installs the necessary system libraries.

FROM rocker/r-ver:4.4.0
ENV RENV_CONFIG_REPOS_OVERRIDE https://packagemanager.rstudio.com/cran/latest

RUN apt-get update -qq && apt-get install -y --no-install-recommends \
 ...

The second component copies the renv.lock file and installs the required R packages:

COPY vetiver_renv.lock renv.lock
RUN Rscript -e "install.packages('renv')"
RUN Rscript -e "renv::restore()"

Finally, we have the plumber/API section

COPY plumber.R /opt/ml/plumber.R
EXPOSE 8080
ENTRYPOINT ["R", "-e", "pr <- plumber::plumb('/opt/ml/plumber.R'); pr$run(host = '0.0.0.0', port = 8080)"]

which runs the API on port 8080.

The container is built via

docker build --tag my-first-model .

The --tag flag allows you to name your Docker image. You can inspect your stored Docker images with:

docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
my-first-model latest 792af21c775a About a minute ago 1.33GB

To run the image, use

docker run --rm --publish 8080:8080 my-first-model

Posit Connect / Sage Maker

We can also trivially publish the model to Posit Connect via

vetiver::vetiver_deploy_rsconnect(board = pins::board_connect(), "colin/k-nn")

Similarly, we can publish to SageMaker using the function vetiver_deploy_sagemaker().

For updates and revisions to this article, see the original post

To leave a comment for the author, please follow the link and comment on their blog: The Jumping Rivers Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)