Deploy a model from a notebook

To deploy a model from within a notebook, you must type or paste deployment code in a code cell. Depending on the language, you can perform this task in several different ways. The following examples show you the most common ways to do this by using the Python and Scala programming languages.

Notebook deployment steps for Python

The following steps can be used in a notebook to deploy and score a model in the cloud using the Python language. In this section you will learn how to create online scoring and to score a new data record by using the Watson Machine Learning REST API. For more information about REST APIs, see the Swagger Documentation.

  1. To work with the Watson Machine Learning REST API you must generate a Bearer access token. To do that you can use the following sample code.

    import urllib3, requests, json
    
    headers = urllib3.util.make_headers(basic_auth='{}:{}'.format(username, password))
    url = '{}/v3/identity/token'.format(service_path)
    response = requests.get(url, headers=headers)
    mltoken = 'Bearer ' + json.loads(response.text).get('token')
    
  2. Create an online scoring endpoint. Get the published_models URL from the instance details.

    endpoint_instance = service_path + "/v3/wml_instances/" + instance_id
    header = {'Content-Type': 'application/json', 'Authorization': mltoken}
    
    response_get_instance = requests.get(endpoint_instance, headers=header)
    print response_get_instance
    print response_get_instance.text
    
    endpoint_published_models = json.loads(response_get_instance.text).get('entity').get('published_models').get('url')
    print endpoint_published_models
    
  3. Execute the following sample code that uses the published_models endpoint to get deployments URL. Get the list of published models.

     header = {'Content-Type': 'application/json', 'Authorization': mltoken}
    
     response_get = requests.get(endpoint_published_models, headers=header)
     print response_get
     print response_get.text
    
  4. Get the published model deployment URL.

    [endpoint_deployments] = [x.get('entity').get('deployments').get('url') 
    for x in json.loads(response_get.text).get('resources') if x.get('metadata').get('guid') == saved_model.uid]
    
    print endpoint_deployments
    
  5. Create an online deployment for the published model.

    payload_online = {"name": "Product Line Prediction", "description": "My Cool Deployment", "type": "online"}
    response_online = requests.post(endpoint_deployments, json=payload_online, headers=header)
    
    print response_online
    print response_online.text
    
  6. Load the scoring URL.

    scoring_url = json.loads(response_online.text).get('entity').get('scoring_url')
    print scoring_url
    
  7. By using the postData method, you can send new scoring records (new data) for which you would like to get predictions.

Notebook deployment steps for Scala

The following steps can be used in a notebook to deploy and score a model in the cloud using the Scala language. In this section you will learn how to create online scoring and to score a new data record by using the Watson Machine Learning REST API. For more information about REST APIs, see the Swagger Documentation.

  1. To work with the Watson Machine Learning REST API you must generate a Bearer access token. To do that you can use the following sample code:

    // Get WML service instance token
    
    val wml_auth_header = "Basic " + Base64.getEncoder.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8))
    val wml_url = service_path + "/v3/identity/token"
    val wml_response = Http(wml_url).header("Authorization", wml_auth_header).asString
    val wmltoken_json: JsValue = Json.parse(wml_response.body)
    
    val wmltoken = (wmltoken_json \ "token").asOpt[String] match {
        case Some(x) => x
        case None => ""
    }
    
    wmltoken
    
  2. Create the online scoring endpoint. Now you can create an online scoring endpoint. Execute the following sample code that uses the publishedModelId value to create the scoring endpoint to the IBM Cloud repository. Get the published_models URL from instance details.

    val endpoint_instance = service_path + "/v3/wml_instances/" + instance_id
    val wml_response_instance = Http(endpoint_instance).header("Content-Type", "application/json")
    .header("Authorization", "Bearer" +  wmltoken).option(HttpOptions.connTimeout(10000)).option(HttpOptions.readTimeout(50000)).asString
    
    wml_response_instance
    
    val published_models_json: JsValue = Json.parse(wml_response_instance.body)
    val published_models_url = (((published_models_json \ "entity") \\ "published_models")(0) \ "url").as[JsString].value
    published_models_url
    
  3. Get a list of the published models.

    val wml_models = Http(published_models_url).header("Content-Type", "application/json").header("Authorization", wmltoken).option(HttpOptions.connTimeout(10000)).option(HttpOptions.readTimeout(50000)).asString
    wml_models
    
    var deployment_endpoint: String = _
    wml_models.body.split("\"").map{ s => {if ((s contains "deployments") & (s contains saved_model.uid.mkString)) {deployment_endpoint = s}}}
    deployment_endpoint
    
  4. Create an online deployment for the published model.

    val payload_name = "Online scoring"
    val payload_data_online = Json.stringify(Json.toJson(Map("type" -> "online", "name" -> payload_name)))
    
    val response_online = Http(deployment_endpoint).postData(payload_data_online).header("Content-Type", "application/json").header("Authorization", "Bearer" +  wmltoken).option(HttpOptions.connTimeout(50000)).option(HttpOptions.readTimeout(50000)).asString
    
    val scoring_url_json: JsValue = Json.parse(response_online.body)
    val scoring_url = (scoring_url_json \ "entity" \ "scoring_url").asOpt[String] match {
        case Some(x) => x
        case None => ""
    }
    
    print(scoring_url)
    
    val payload_scoring = Json.stringify(Json.toJson(Map("fields" -> Json.toJson(List(Json.toJson("GENDER"), Json.toJson("AGE"), Json.toJson("MARITAL_STATUS"), Json.toJson("PROFESSION"))),
                                                    "values" -> Json.toJson(List(List(Json.toJson("M"), Json.toJson(55), Json.toJson("Single"), Json.toJson("Executive")))))))
    
    payload_scoring
    
  5. By using the 'postData' method, you can send new scoring records (new data) for which you would like to get predictions.

Next steps

For more information about continuous learning and model evaluation, see Continuous learning and model evaluation