Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cloud/notebooks/rest_api/curl/deployments/scikit/Use scikit-learn to predict diabetes progression.ipynb
6408 views
Kernel: Python 3 (ipykernel)

Use scikit-learn to predict diabetes progression with Watson Machine Learning REST API

This notebook contains steps and code to demonstrate support of external machine learning models in Watson Machine Learning Service. This notebook introduces cURL calls for publishing, deploying (Web Service) and scoring ML scikit model.

Some familiarity with cURL is helpful. This notebook uses cURL examples.

This example was made based on diabetes dataset and trained scikit ML model that could be found inside the same repository under as follows:

  • /data/diabetes/

  • /models/scikit/diabetes/

Learning goals

The learning goals of this notebook are:

  • Working with Watson Machine Learning repository, deployment and scoring.

Contents

This notebook contains the following parts:

  1. Setup

  2. WML Repository

  3. Model Deployment and Scoring

  4. Persist new version of the model

  5. Redeploy and score new version of the model

  6. Cleaning

  7. Summary

1. Set up the environment

Before you use the sample code in this notebook, you must perform the following setup tasks:

You can find your COS credentials in COS instance dashboard under the Service credentials tab. Go to the Endpoint tab in the COS instance's dashboard to get the endpoint information.

Authenticate the Watson Machine Learning service on IBM Cloud.

Your Cloud API key can be generated by going to the Users section of the Cloud console. From that page, click your name, scroll down to the API Keys section, and click Create an IBM Cloud API key. Give your key a name and click Create, then copy the created key and paste it below.

NOTE: You can also get service specific apikey by going to the Service IDs section of the Cloud Console. From that page, click Create, then copy the created key and paste it below.

from json import loads from IPython.display import JSON
%env API_KEY=... %env WML_ENDPOINT_URL=... %env WML_INSTANCE_CRN="fill out only if you want to create a new space" %env WML_INSTANCE_NAME=... %env COS_CRN="fill out only if you want to create a new space" %env COS_ENDPOINT=... %env COS_BUCKET=... %env COS_ACCESS_KEY_ID=... %env COS_SECRET_ACCESS_KEY=... %env COS_API_KEY=... %env SPACE_ID="fill out only if you have space already created" %env DATAPLATFORM_URL=https://api.dataplatform.cloud.ibm.com %env AUTH_ENDPOINT=https://iam.cloud.ibm.com/oidc/token

Getting WML authorization token for further cURL calls

%%bash --out token curl -sk -X POST \ --header "Content-Type: application/x-www-form-urlencoded" \ --header "Accept: application/json" \ --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \ --data-urlencode "apikey=$API_KEY" \ "$AUTH_ENDPOINT" \ | cut -d '"' -f 4
%env TOKEN=$token
env: TOKEN=eyJraWQiOiIyMDIyMDMxNzA4MjMiLCJhbGciOiJSUzI1NiJ9.eyJpYW1faWQiOiJJQk1pZC01NTAwMDA5MVZDIiwiaWQiOiJJQk1pZC01NTAwMDA5MVZDIiwicmVhbG1pZCI6IklCTWlkIiwianRpIjoiYmYwZTNjMjEtMmZmYy00ZWZkLWFkOWQtMzRjYTAwMjJjZTM4IiwiaWRlbnRpZmllciI6IjU1MDAwMDkxVkMiLCJnaXZlbl9uYW1lIjoiV01MIiwiZmFtaWx5X25hbWUiOiJXTUwtQmV0YSIsIm5hbWUiOiJXTUwgV01MLUJldGEiLCJlbWFpbCI6IldNTC1CZXRhQHBsLmlibS5jb20iLCJzdWIiOiJXTUwtQmV0YUBwbC5pYm0uY29tIiwiYXV0aG4iOnsic3ViIjoiV01MLUJldGFAcGwuaWJtLmNvbSIsImlhbV9pZCI6IklCTWlkLTU1MDAwMDkxVkMiLCJuYW1lIjoiV01MIFdNTC1CZXRhIiwiZ2l2ZW5fbmFtZSI6IldNTCIsImZhbWlseV9uYW1lIjoiV01MLUJldGEiLCJlbWFpbCI6IldNTC1CZXRhQHBsLmlibS5jb20ifSwiYWNjb3VudCI6eyJib3VuZGFyeSI6Imdsb2JhbCIsInZhbGlkIjp0cnVlLCJic3MiOiJlMGY3ZWMzYWMxYjI0ZWM5YWU3NzFlZmQ3NzI1MzhhMiIsImltc191c2VyX2lkIjoiNzE3Njk0MyIsImZyb3plbiI6dHJ1ZSwiaW1zIjoiMTY4NDc4MyJ9LCJpYXQiOjE2NDk3Njg4MDIsImV4cCI6MTY0OTc3MjQwMiwiaXNzIjoiaHR0cHM6Ly9pYW0uY2xvdWQuaWJtLmNvbS9pZGVudGl0eSIsImdyYW50X3R5cGUiOiJ1cm46aWJtOnBhcmFtczpvYXV0aDpncmFudC10eXBlOmFwaWtleSIsInNjb3BlIjoiaWJtIG9wZW5pZCIsImNsaWVudF9pZCI6ImRlZmF1bHQiLCJhY3IiOjEsImFtciI6WyJwd2QiXX0.TeHnosVgvUd_cz-5Y398WFQGe379xd6AFRx7PVfnfkxXNI1OUfRIfRFhO8J2cITtsvSgC06CrdRWMoHqnvgpHUtBYTSi906yzyyOiif-qmu0v77RIkGo0rLx_BDUsX_h2eaLXn7SPrClBHKaavLXIjCl4BdqN5dG64Y2ErNeEGbDYVHqWeGwuCtGvmbenGW2dEtCTuHa1yDpx4Xlz7uc8nJZ41KT2ugpOZgo-IJ7y-UAPQ5_pUDhd9qxEmwi0fBmdszIzuBFnhTbwsMSl25p-Z16weGWPGjauNhEd4g4Yagkg9rlD-Jf2Oy2St8iD7DIRbHuAdQoDndTZ52CoaCn-w

Space creation

Tip: If you do not have space already created, please convert below three cells to code and run them.

First of all, you need to create a space that will be used in all of your further cURL calls. If you do not have space already created, below is the cURL call to create one.

<a href="https://cloud.ibm.com/apidocs/data-ai-common-core#spaces-create" target="_blank" rel="noopener no referrer">Space creation

%%bash --out space_id curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data '{"name": "my_space4", "storage": {"type": "bmcos_object_storage", "resource_crn": "'"$COS_CRN"'"}, "compute": [{"name": "'"$WML_INSTANCE_NAME"'", "crn": "'"$WML_INSTANCE_CRN"'", "type": "machine_learning"}]}' \ "$DATAPLATFORM_URL/v2/spaces" \ | grep '"id": ' | awk -F '"' '{ print $4 }'
space_id = space_id.split('\n')[1] %env SPACE_ID=$space_id

Space creation is asynchronous. This means that you need to check space creation status after creation call. Make sure that your newly created space is active.

<a href="https://cloud.ibm.com/apidocs/data-ai-common-core#spaces-get" target="_blank" rel="noopener no referrer">Get space information

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$DATAPLATFORM_URL/v2/spaces/$SPACE_ID"

2. Manage WML Repository

In this section you will learn how to upload your ML model, list your models stored in repository, delete your model and update it.

Model storing

Store information about your model to WML repository.

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-create" target="_blank" rel="noopener no referrer">Model storing

%%bash --out model_payload MODEL_PAYLOAD='{"space_id": "'"$SPACE_ID"'","name": "scikit_diabetes_model","description": "This is description","type": "scikit-learn_1.0", "software_spec": {"name": "runtime-22.1-py3.9"}}' echo $MODEL_PAYLOAD
JSON(loads(model_payload))
<IPython.core.display.JSON object>
%env MODEL_PAYLOAD=$model_payload
%%bash --out model_id -s "$model_payload" curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$MODEL_PAYLOAD" \ "$WML_ENDPOINT_URL/ml/v4/models?version=2020-08-01" | grep '"id": ' | awk -F '"' '{ print $4 }' | sed -n 2p
%env MODEL_ID=$model_id
env: MODEL_ID=091c518d-3f56-4a24-8b36-b8a1e8d98371

Create model revision for further update

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-create-revision" target="_blank" rel="noopener no referrer">Model revision

%%bash --out revision_payload REVISION_PAYLOAD='{"space_id": "'"$SPACE_ID"'", "commit_message": "Initial model."}' echo $REVISION_PAYLOAD
JSON(loads(revision_payload))
<IPython.core.display.JSON object>
%env REVISION_PAYLOAD=$revision_payload
%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$REVISION_PAYLOAD" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID/revisions?version=2020-08-01" \ | python -m json.tool
{ "entity": { "hybrid_pipeline_software_specs": [], "software_spec": { "id": "12b83a17-24d8-5082-900f-0ab31fbfd3cb", "name": "runtime-22.1-py3.9" }, "type": "scikit-learn_1.0" }, "metadata": { "commit_info": { "commit_message": "Initial model.", "committed_at": "2022-04-12T13:09:45.002Z" }, "created_at": "2022-04-12T13:08:52.237Z", "description": "This is description", "id": "091c518d-3f56-4a24-8b36-b8a1e8d98371", "modified_at": "2022-04-12T13:08:57.416Z", "name": "scikit_diabetes_model", "owner": "IBMid-55000091VC", "resource_key": "844bae18-ff37-495f-8d33-0e29b95bf6f4", "rev": "1", "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47" }, "system": { "warnings": [] } }

Model content upload

Now you need to upload your model content into the WML repository.

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-upload-content" target="_blank" rel="noopener no referrer">Upload model content

!wget -q https://github.com/IBM/watson-machine-learning-samples/raw/master/cloud/models/scikit/diabetes/model/diabetes_model.tar.gz \ -O diabetes_model.tar.gz
%%bash --out attachment_id curl -sk -X PUT \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/gzip" \ --header "Accept: application/json" \ --data-binary "@diabetes_model.tar.gz" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID/content?space_id=$SPACE_ID&version=2020-08-01&content_format=native" \ | grep "attachment_id" | awk -F '"' '{ print $4 }'
%env ATTACHMENT_ID=$attachment_id
env: ATTACHMENT_ID=abe3eaf0-4f3b-4913-a0b2-afa41823c7af

Download model

If you want to download your saved model, please make the following call.

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-filtered-download" target="_blank" rel="noopener no referrer">Download model content

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --output "model.tar.gz" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID/download?space_id=$SPACE_ID&version=2020-08-01"
!ls -l model.tar.gz

3. Deploy and Score

In this section you will learn how to deploy and score pipeline model as webservice using WML instance.

Deployment creation

This example uses scikit-learn model deployment and S hardware specification.

<a href="https://cloud.ibm.com/apidocs/machine-learning#deployments-create" target="_blank" rel="noopener no referrer">Create deployment

%%bash --out deployment_payload DEPLOYMENT_PAYLOAD='{"space_id": "'"$SPACE_ID"'","name": "Diabetes deployment", "description": "This is description","online": {},"hardware_spec": {"name": "S"},"asset": {"id": "'"$MODEL_ID"'"}}' echo $DEPLOYMENT_PAYLOAD
JSON(loads(deployment_payload))
<IPython.core.display.JSON object>
%env DEPLOYMENT_PAYLOAD=$deployment_payload
env: DEPLOYMENT_PAYLOAD={"space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47","name": "Diabetes deployment", "description": "This is description","online": {},"hardware_spec": {"name": "S"},"asset": {"id": "091c518d-3f56-4a24-8b36-b8a1e8d98371"}}
%%bash --out deployment_id curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$DEPLOYMENT_PAYLOAD" \ "$WML_ENDPOINT_URL/ml/v4/deployments?version=2020-08-01" | grep '"id": ' | awk -F '"' '{ print $4 }' | sed -n 3p
%env DEPLOYMENT_ID=$deployment_id
env: DEPLOYMENT_ID=d8fb127e-83e5-4b6b-af68-82fe901a875d

Get deployment details

As deployment API is asynchronous, please make sure your deployment is in ready state before going to the next points.

<a href="https://cloud.ibm.com/apidocs/machine-learning#deployments-get" target="_blank" rel="noopener no referrer">Get deployment details

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ "$WML_ENDPOINT_URL/ml/v4/deployments/$DEPLOYMENT_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "entity": { "asset": { "id": "091c518d-3f56-4a24-8b36-b8a1e8d98371" }, "custom": {}, "deployed_asset_type": "model", "description": "This is description", "hardware_spec": { "id": "e7ed1d6c-2e89-42d7-aed5-863b972c1d2b", "name": "S", "num_nodes": 1 }, "name": "Diabetes deployment", "online": {}, "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47", "status": { "online_url": { "url": "https://yp-qa.ml.cloud.ibm.com/ml/v4/deployments/d8fb127e-83e5-4b6b-af68-82fe901a875d/predictions" }, "serving_urls": [ "https://yp-qa.ml.cloud.ibm.com/ml/v4/deployments/d8fb127e-83e5-4b6b-af68-82fe901a875d/predictions" ], "state": "ready" } }, "metadata": { "created_at": "2022-04-12T13:10:55.346Z", "description": "This is description", "id": "d8fb127e-83e5-4b6b-af68-82fe901a875d", "modified_at": "2022-04-12T13:10:55.346Z", "name": "Diabetes deployment", "owner": "IBMid-55000091VC", "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47" }, "system": { "warnings": [ { "id": "Deprecated", "message": "online_url is deprecated and will be removed in a future release. Use serving_urls instead." } ] } }

Scoring of a webservice

If you want to make a score call on your deployment, please follow a below method:

<a href="https://cloud.ibm.com/apidocs/machine-learning#deployments-compute-predictions" target="_blank" rel="noopener no referrer">Score your deployment

%%bash --out scoring_payload SCORING_PAYLOAD='{"space_id": "$SPACE_ID","input_data": [{"fields": ["age", "sex", "bmi", "bp", "s1", "s2", "s3", "s4", "s5", "s6"], "values": [[-0.00188201652779104, -0.044641636506989, -0.0514740612388061, -0.0263278347173518, -0.00844872411121698, -0.019163339748222, 0.0744115640787594, -0.0394933828740919, -0.0683297436244215, -0.09220404962683], [0.0852989062966783, 0.0506801187398187, 0.0444512133365941, -0.00567061055493425, -0.0455994512826475, -0.0341944659141195, -0.0323559322397657, -0.00259226199818282, 0.00286377051894013, -0.0259303389894746]]}]}' echo $SCORING_PAYLOAD
JSON(loads(scoring_payload))
<IPython.core.display.JSON object>
%env SCORING_PAYLOAD=$scoring_payload
%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$SCORING_PAYLOAD"\ "$WML_ENDPOINT_URL/ml/v4/deployments/$DEPLOYMENT_ID/predictions?version=2020-08-01" \ | python -m json.tool
{ "predictions": [ { "fields": [ "prediction" ], "values": [ [ 82.3468335692293 ], [ 168.2544647515973 ] ] } ] }

Listing all deployments

<a href="https://cloud.ibm.com/apidocs/machine-learning#deployments-list" target="_blank" rel="noopener no referrer">List deployments details

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ "$WML_ENDPOINT_URL/ml/v4/deployments?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool

4. Persist new version of the model

In this section, you'll learn how to store new version of your model in Watson Machine Learning repository.

Model update

Below you can find how ML model can be updated with new version on WML repository.

List model revisions.

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-list-revisions" target="_blank" rel="noopener no referrer">List revisions

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID/revisions?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool

Create second model revision

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-create-revision" target="_blank" rel="noopener no referrer">Create model revision

%%bash --out revision_payload REVISION_PAYLOAD='{"space_id": "'"$SPACE_ID"'", "commit_message": "Updated model."}' echo $REVISION_PAYLOAD
JSON(loads(revision_payload))
<IPython.core.display.JSON object>
%env REVISION_PAYLOAD=$revision_payload
env: REVISION_PAYLOAD={"space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47", "commit_message": "Updated model."}
%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$REVISION_PAYLOAD" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID/revisions?version=2020-08-01" \ | python -m json.tool

Update model metadata

For example update model name or description

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-update" target="_blank" rel="noopener no referrer">Patch model

%%bash --out update_payload echo '[{"op": "add", "path": "/name", "value": "updated scikit model"}]'
%env UPDATE_PAYLOAD=$update_payload
env: UPDATE_PAYLOAD=[{"op": "add", "path": "/name", "value": "updated scikit model"}]
%%bash curl -sk -X PATCH \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$UPDATE_PAYLOAD" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "entity": { "hybrid_pipeline_software_specs": [], "software_spec": { "id": "12b83a17-24d8-5082-900f-0ab31fbfd3cb", "name": "runtime-22.1-py3.9" }, "type": "scikit-learn_1.0" }, "metadata": { "commit_info": { "committed_at": "2022-04-12T13:13:05.072Z" }, "created_at": "2022-04-12T13:08:52.237Z", "description": "This is description", "id": "091c518d-3f56-4a24-8b36-b8a1e8d98371", "modified_at": "2022-04-12T13:13:04.869Z", "name": "updated scikit model", "owner": "IBMid-55000091VC", "resource_key": "004c3bd1-4f58-4d17-a84c-f0a527ca9830", "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47" }, "system": { "warnings": [] } }

Upload new model content

!wget -q https://github.com/IBM/watson-machine-learning-samples/raw/master/cloud/models/scikit/diabetes/model/new_diabetes_model.tar.gz \ -O new_diabetes_model.tar.gz

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-upload-content" target="_blank" rel="noopener no referrer">Upload model content

%%bash curl -sk -X PUT \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/gzip" \ --header "Accept: application/json" \ --data-binary "@new_diabetes_model.tar.gz" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID/content?space_id=$SPACE_ID&version=2020-08-01&content_format=native" \ | python -m json.tool
{ "attachment_id": "a61f2746-3e3a-4ea9-acbf-5ac1d6a1ff3a", "content_format": "native", "persisted": true }

List model revisions to see a new one just created

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-list-revisions" target="_blank" rel="noopener no referrer">List revisions

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID/revisions?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "first": { "href": "/ml/v4/models/091c518d-3f56-4a24-8b36-b8a1e8d98371/revisions?space_id=680a7515-620c-461f-9c6f-1f4c535bfc47&limit=100" }, "limit": 100, "resources": [ { "metadata": { "commit_info": { "commit_message": "Updated model.", "committed_at": "2022-04-12T13:12:29.002Z" }, "created_at": "2022-04-12T13:08:52.237Z", "description": "This is description", "id": "091c518d-3f56-4a24-8b36-b8a1e8d98371", "modified_at": "2022-04-12T13:10:15.830Z", "name": "scikit_diabetes_model", "owner": "IBMid-55000091VC", "resource_key": "059d30d9-a47c-4ada-a1bc-904733099fb1", "rev": "2", "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47" } }, { "metadata": { "commit_info": { "commit_message": "Initial model.", "committed_at": "2022-04-12T13:09:45.002Z" }, "created_at": "2022-04-12T13:08:52.237Z", "description": "This is description", "id": "091c518d-3f56-4a24-8b36-b8a1e8d98371", "modified_at": "2022-04-12T13:08:57.416Z", "name": "scikit_diabetes_model", "owner": "IBMid-55000091VC", "resource_key": "844bae18-ff37-495f-8d33-0e29b95bf6f4", "rev": "1", "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47" } } ] }

Now we have updated model content and model name in repository.

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-get" target="_blank" rel="noopener no referrer">Get model details

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "entity": { "hybrid_pipeline_software_specs": [], "software_spec": { "id": "12b83a17-24d8-5082-900f-0ab31fbfd3cb", "name": "runtime-22.1-py3.9" }, "type": "scikit-learn_1.0" }, "metadata": { "commit_info": { "committed_at": "2022-04-12T13:13:32.613Z" }, "created_at": "2022-04-12T13:08:52.237Z", "description": "This is description", "id": "091c518d-3f56-4a24-8b36-b8a1e8d98371", "modified_at": "2022-04-12T13:13:25.446Z", "name": "updated scikit model", "owner": "IBMid-55000091VC", "resource_key": "0c2b2541-92a9-4645-8697-4f0949bf698d", "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47" }, "system": { "warnings": [] } }

5. Redeploy and score new version of the model

Below you can see how deployment can be updated with new version of the model without any change for scoring url.

<a href="https://cloud.ibm.com/apidocs/machine-learning#deployments-update" target="_blank" rel="noopener no referrer">Deployment update

%%bash --out redeploy_payload echo '[{"op": "replace", "path": "/asset", "value": {"id": "'"$MODEL_ID"'"}}]'
%env REDEPLOY_PAYLOAD=$redeploy_payload
env: REDEPLOY_PAYLOAD=[{"op": "replace", "path": "/asset", "value": {"id": "091c518d-3f56-4a24-8b36-b8a1e8d98371"}}]
%%bash curl -sk -X PATCH \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$REDEPLOY_PAYLOAD" \ "$WML_ENDPOINT_URL/ml/v4/deployments/$DEPLOYMENT_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "entity": { "asset": { "id": "091c518d-3f56-4a24-8b36-b8a1e8d98371" }, "custom": {}, "deployed_asset_type": "model", "description": "This is description", "hardware_spec": { "id": "e7ed1d6c-2e89-42d7-aed5-863b972c1d2b", "name": "S", "num_nodes": 1 }, "name": "Diabetes deployment", "online": {}, "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47", "status": { "online_url": { "url": "https://yp-qa.ml.cloud.ibm.com/ml/v4/deployments/d8fb127e-83e5-4b6b-af68-82fe901a875d/predictions" }, "serving_urls": [ "https://yp-qa.ml.cloud.ibm.com/ml/v4/deployments/d8fb127e-83e5-4b6b-af68-82fe901a875d/predictions" ], "state": "ready" } }, "metadata": { "created_at": "2022-04-12T13:10:55.346Z", "description": "This is description", "id": "d8fb127e-83e5-4b6b-af68-82fe901a875d", "modified_at": "2022-04-12T13:13:47.467Z", "name": "Diabetes deployment", "owner": "IBMid-55000091VC", "space_id": "680a7515-620c-461f-9c6f-1f4c535bfc47" }, "system": { "warnings": [ { "id": "Deprecated", "message": "online_url is deprecated and will be removed in a future release. Use serving_urls instead." } ] } }

Score updated webservice

%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$SCORING_PAYLOAD" \ "$WML_ENDPOINT_URL/ml/v4/deployments/$DEPLOYMENT_ID/predictions?version=2020-08-01" \ | python -m json.tool
{ "predictions": [ { "fields": [ "prediction" ], "values": [ [ 82.3468335692293 ], [ 168.2544647515973 ] ] } ] }

6. Cleaning section

Below section is useful when you want to clean all of your previous work within this notebook. Just convert below cells into the code and run them.

Deleting deployment

Tip: You can delete existing deployment by calling DELETE method.

%%bash curl -sk -X DELETE \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$WML_ENDPOINT_URL/ml/v4/deployments/$DEPLOYMENT_ID?space_id=$SPACE_ID&version=2020-08-01"

Delete model from repository

Tip: If you want to completely remove your stored model and model metadata, just use a DELETE method.

<a href="https://cloud.ibm.com/apidocs/machine-learning#models-delete" target="_blank" rel="noopener no referrer">Delete model from repository

%%bash curl -sk -X DELETE \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ "$WML_ENDPOINT_URL/ml/v4/models/$MODEL_ID?space_id=$SPACE_ID&version=2020-08-01"

7. Summary and next steps

You successfully completed this notebook!.

You learned how to use cURL calls to store, deploy and score a scikit-learn ML model in WML.

Authors

Amadeusz Masny, Python Software Developer in Watson Machine Learning at IBM Jan Sołtysik, Intern in Watson Machine Learning at IBM

Copyright © 2020, 2021, 2022 IBM. This notebook and its source code are released under the terms of the MIT License.