Table of contents

Manage a deployed script as a web service

In the Watson Machine Learning, you can deploy an R or Python script as a web service to allow other users to use your code.

After you deploy a script, a container is created that exclusively hosts your script and exposes it for consumption. You can consume web services programmatically by using a REST API or directly in Watson Machine Learning by using the UI.

Each web service is uniquely identified by the release alias, the asset type, and the deployment name.

To manage a script as a web service, you can:

You can also:

  • Monitor the health of the deployed scripts' containers.
  • View metrics about access of scripts.

Deploy scripts as web services

You need to create a release that contains R scripts or Python scripts and deploy the scripts as web services, which exposes them with a URL Deploy the scripts by following the steps in Deploy an asset as a web service You can deploy:
  • Python 2.7/3.5 scripts
  • R scripts

Initialize environment variables or load data into memory at container startup

Latency is one of the critical metrics when you execute scripts as web services. If you keep some of your data in memory, you can decrease latency and avoid I/O operations as each request comes in. You might also want to set up global and environment variables upfront to allow for decision making downstream.

You can provide an init method in your script to set environment variables and load data into memory. The init method gets run as part of the invoke sequence of the script deployment.

R example
## This function will be executed at the
container's startup
init() <- function() {

    # Set up a global variable called global_version
    global_model_version <<- 3;

    # Set up an environment variable called R_TEST 
    Sys.setenv(R_TEST = "test")

    # Read and restore an R model from a file located in your
dataset directory 

        # Obtain project name from DSX_PROJECT_NAME and DSX_USER_ID
environment variables 
        projectName <- Sys.getenv("DSX_PROJECT_NAME")
        userUID <- Sys.getenv("DSX_USER_ID")

        # Construct file path to your model file
        modelPath <- file.path("/user-home", userUID,
"DSX_Projects", projectName)

        # Load model from file system into memory
        model <<- readRDS(modelPath)
}
Python 2.7/3.5 example
def init():
  # Intialization code goes here

Invoke a user script

This example assumes that your model was loaded into memory by the init() function at the start of the container that hosts the script as a web service. In addition, it assumes a classify-flow.R script was deployed as a web service.

The classify-flower.R web service receives a data sample about a flower. The function that you can invoke is called classify and it aims to classify the sample among three species (setosa, versicolor, or virginica) from measurements of length and width of sepals and petals:

classify-flower.R:

    ....

    classify <- function(args) {
        inputData <- args$data

        # model variable is available in memory and can be accessed
directly
        if(model$modelType == "Classification") {
            predictions <- predict(model, inputData)
            probabilities <- predict(model, inputData,
type="prob")
            classes <- colnames(probabilities)
        }

        output <- list(classes, unname(probabilities,
force=FALSE), predictions)
        names(output) <- list("classes", "probabilities",
"predictions")

        ...
        # Classify flower based on probabilities 
        ...

        return(flowerType)
    }

Functions can receive input data from HTTP clients through a single variable named args. There are no restrictions on the data structure of the object.

Consume the scripts programmatically with REST APIs

After you deploy a script as a web service, you can consume functions in the script with HTTP on various operating systems. The example uses the curl client, but all general HTTP clients are supported.

The last URL segment in the URL is the function name that you want to invoke. In this case classify is the function that you want to invoke.

Input:

curl -k -X POST  https://demo-cluster.com/dsvc/v1/demo/rscript/flower-prediction/classify
  -H 'Authorization: Bearer ${deploymentToken}' 
  -H 'Content-Type: application/json' 
  -d '{
        "args": {
            "data": { 
                "sepal_length": 2,
                "sepal_width":3,
                "petal_length": 6,
                "petal_width": 12
            }
        }
    }'

Output:

{
  "result": {
    "flower_type": "setosa"
   },
  "stderr": [],
  "stdout": []
}

You successfully invoked a function in your script that is exposed with REST API.