How to deploy an R XGboost model in UbiOps?

Siwan Tunc
UbiOps-tech
Published in
4 min readAug 26, 2021

Imagine this, you have just created a trained machine learning model or written an algorithm in R, and now you are looking for ways to put that code into practice. Well, you have come to the right place. UbiOps is a SaaS service that lets you make your data science application a reality and supports both R and Python code.

This article will focus on how to build an R deployment based on the use case described below. If you want to see how to create a Python deployment, you can find examples in the UbiOps cookbook

How does UbiOps work?

UbiOps works with deployments, pipelines, and objects.

Deployments are objects within UbiOps that serve a user’s code. The platform will build a container around the user’s code, running as a microservice inside UbiOps, that can receive requests to transform input data into output data by making requests. UbiOps also creates an API endpoint for the deployment.

UbiOps also makes it possible to chain several deployments together to create a pipeline. In a pipeline, the output of one deployment is the input of another deployment. This could be useful when your application depends on a series of separate data transformations that need to operate in sequence.

You can use a pipeline just like you would use a deployment. All deployments can be used in multiple pipelines at once, or multiple times in a single pipeline, which provides a lot of flexibility. All objects, which is the name for a deployment that is part of a pipeline, in a pipeline will scale independently of each other, efficiently distributing the compute load for your process. Each object can use its dependencies and programming language independent of the other objects in the pipeline. Making requests to a pipeline works the same as making requests to a deployment.

Within UbiOps, it is possible to maintain different versions of both deployments and pipelines. All versions in a deployment/pipeline share the same in- and output fields such that they can be used interchangeably, but the deployed code package, programming language, memory allocation, and other settings can differ.

If you want to read more about the features of UbiOps, click on this link.

1. Use case

The use case that will be used for this article is as follows: because the house prices continue to rise, a model that would predict house prices may come in handy. The dataset used for the prediction is the House Sales in King County, USA

The model is written in R and built using the R XGboost extension.

2. Script walkthrough and instructions

Below I will show a few steps on how to create a deployment using the client library of UbiOps. The steps shown in this article are as follows: (1) connect to the UbiOps environment, (2) create the R deployment, and (3) upload the deployment package to UbiOps. If you want to see the full R script, please check out this link.

Deployment overview

The deployment is defined as follows:

Table 1: deployment overview

The code shown below is the code required to create the deployment.

Connect to the UbiOps environment (1)

Sys.setenv("UBIOPS_PROJECT" = "INSERT_YOUR_PROJECT_NAME_HERE")

Sys.setenv("UBIOPS_API_TOKEN" = "INSERT_YOUR_TOKEN_HERE")

Sys.setenv(UBIOPS_API_URL = "https://api.ubiops.com/v2.1")

DEPLOYMENT_NAME <- "r-xgboost-deployment"
DEPLOYMENT_VERSION <- "v1"

result <- service_status()
result

Create the deployment (2)

deployment <- list(

name = DEPLOYMENT_NAME,

description = "r-xgboost-deployment.",

input_type = "structured",

output_type = "structured",

input_fields = list(

list(name = "input_data", data_type = "blob")

),
output_fields = list(

list(name = "prediction", data_type = "blob")

),

labels = list(demo = "r-xgboost-deployment")
)
result <- deployments_create(data = deployment)
result

Upload the deployment to UbiOps (3)

result <- revisions_file_upload(

deployment.name = DEPLOYMENT_NAME,

version = DEPLOYMENT_VERSION,

file = r_xgboost_recipe_zip

)

build.id <- result$build

result

status <- "queued"

while(status != "success" && status != "failed") {

result <- builds_get(

deployment.name = DEPLOYMENT_NAME,

version = DEPLOYMENT_VERSION,

build.id = build.id

)

status <- result$status

Sys.sleep(2)

}

print(status)

result <- deployment_versions_get(

deployment.name = DEPLOYMENT_NAME,

version = DEPLOYMENT_VERSION

)

result$status

After everything has finished building, requests can be made to the deployment.

Figure 1: request result

Looks like the deployment is working perfectly!

3. Wrap up

And that is all it takes to create an R XGboost deployment on UbiOps! We hope that this helps you in your day-to-day projects. If you want to try out UbiOps, please visit app.ubiops.com/sign-up , where you can make an account and start deploying your model for free.

For any questions or suggestions please join the UbiOps community slack channel or contact our customer support.

Download the Rscript here.

--

--