[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.

Continue reading: Vetiver: Model Deployment

Understanding MLOps, Vetiver, and Docker for Model Deployments: Future Implications and Advice

MLOps and vetiver are useful tools for machine learning programmers. Vetiver, specifically, is an R package used to streamline model deployment through Docker, an open-source platform that lets developers bundle application source code with operating system libraries and dependencies necessary for running that code. A comprehensive understanding of these tools will enhance developers’ effectiveness in model deployment processes, leading to improved machine learning implementations.

Key Features of Vetiver and Docker

Vetiver simplifies the creation of Dockerfiles and is used for generating an API to access a model, defining R package dependencies, and generating a Dockerfile. Docker manages the deployment process and provides a way to bundle the necessary components for running the model. Understanding these features is vital for future machine learning deployments.

Potential Future Developments

With increased use of machine learning, software tools like Vetiver and Docker will likely evolve to meet deployment needs. Consequently, it’s essential to stay updated with advancements in these tools.

Enhanced Integration Possibilities

As the amount of data generated increases, the demand for seamless integration of tools like Vetiver and Docker with other software will rise. A future with more streamlined and comprehensive machine learning operations is foreseeable.

Practical Advice for Developers

Become Proficient in Vetiver and Docker

Developers specializing in machine learning should become proficient in vetiver and Docker. This skill will provide them with an essential toolkit for efficient and successful model deployments.

Stay Informed

As with any rapidly evolving technology, staying informed about updates and improvements to vetiver, Docker, and other MLOps tools is crucial. Subscribing to technology blogs and participating in active coding communities can help developers stay up-to-date.

Regularly Practice Deployments

To enhance proficiency in using vetiver and Docker, developers should practice deploying models regularly. Over time, this practice will speed up the deployment process and lead to fewer errors.

Collaborate with Peers

The challenges of model deployments are not for solitary conquest. Collaboration with peers to create, troubleshoot, and deploy machine learning models can significantly improve developers’ proficiency and performance.

Read the original article