# authenticates via environment variables:vetiver_deploy_rsconnect(model_board, "user.name/cars_mpg")
from rsconnect.api import RSConnectServerconnect_server = RSConnectServer( url=server_url, # load from an .env file api_key=api_key # load from an .env file )vetiver.deploy_rsconnect( connect_server = connect_server, board = model_board, pin_name ="user.name/cars_mpg",)
In this case, you probably want model_board to be a Connect pins board (board_connect()). For more on deploying to Connect, see the Connect documentation for using vetiver.
Prepare a Dockerfile
For deploying a vetiver API to infrastructure other than Posit Connect, such as Google Cloud Run, AWS, or Azure, you likely will want to build a Docker container.
Note
You can use any pins board with Docker, like board_folder() or board_connect(), as long as your Docker container can authenticate to your pins board.
The following required packages are not installed:
- cpp11 [required by lobstr, readr, tidyr, and 2 others]
- progress [required by vroom]
Consider reinstalling these packages before snapshotting the lockfile.
# Generated by the vetiver package; edit with care
FROM rocker/r-ver:4.4.1
ENV RENV_CONFIG_REPOS_OVERRIDE https://packagemanager.rstudio.com/cran/latest
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
libcurl4-openssl-dev \
libicu-dev \
libsodium-dev \
libssl-dev \
make \
zlib1g-dev \
&& apt-get clean
COPY vetiver_renv.lock renv.lock
RUN Rscript -e "install.packages('renv')"
RUN Rscript -e "renv::restore()"
COPY plumber.R /opt/ml/plumber.R
EXPOSE 8000
ENTRYPOINT ["R", "-e", "pr <- plumber::plumb('/opt/ml/plumber.R'); pr$run(host = '0.0.0.0', port = 8000)"]
When you run vetiver_prepare_docker(), you generate three files needed to build a Docker image: the Dockerfile itself, a Plumber file serving your REST API, and the vetiver_renv.lock file to capture your model dependencies.
vetiver.prepare_docker(model_board, "cars_mpg")
/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vetiver/attach_pkgs.py:77: UserWarning:
required packages unknown for board protocol: ('file', 'local'), add to model's metadata to export
# # Generated by the vetiver package; edit with care
# start with python base image
FROM python:3.10
# create directory in container for vetiver files
WORKDIR /vetiver
# copy and install requirements
COPY vetiver_requirements.txt /vetiver/requirements.txt
#
RUN pip install --no-cache-dir --upgrade -r /vetiver/requirements.txt
# copy app file
COPY app.py /vetiver/app/app.py
# expose port
EXPOSE 8080
# run vetiver API
CMD ["uvicorn", "app.app:api", "--host", "0.0.0.0", "--port", "8080"]
When you run vetiver.prepare_docker(), you generate three files needed to build a Docker image: the Dockerfile itself, a FastAPI app file serving your REST API, and a requirements.txt file to capture your model dependencies.
Tip
When you build such a Docker container with docker build, all the packages needed to make a prediction with your model are installed into the container.
When you run the Docker container, you can pass in environment variables (for authentication to your pins board, for example) with docker run --env-file .Renviron.
── A model API endpoint for prediction:
http://127.0.0.1:8080/predict
from vetiver.server import predict, vetiver_endpointendpoint = vetiver_endpoint("http://127.0.0.1:8080/predict")endpoint
'http://127.0.0.1:8080/predict'
If such a deployed model endpoint is running via one process (either remotely on a server or locally, perhaps via Docker or a background job in the RStudio IDE), you can make predictions with that deployed model and new data in another, separate process1.
Being able to predict with a vetiver model endpoint takes advantage of the model’s input data prototype and other metadata that is stored with the model.
Footnotes
Keep in mind that the R and Python models have different values for the decision tree hyperparameters.↩︎