Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cpd4.0/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:

  • Contact with your Cloud Pack for Data administrator and ask him for your account credentials

Connection to WML

Authenticate the Watson Machine Learning service on IBM Cloud Pack for Data. You need to provide platform url, your username and api_key.

from json import loads from IPython.display import JSON
%env USERNAME= %env API_KEY= %env DATAPLATFORM_URL= %env SPACE_ID=

Getting WML authorization token for further cURL calls

%%bash --out token token=$(curl -sk -X POST \ --header "Content-type: application/json" \ -d "{\"username\":\"${USERNAME}\",\"api_key\":\"${API_KEY}\"}" \ "$DATAPLATFORM_URL/icp4d-api/v1/authorize") token=${token#*token\":\"} token=${token%%\"*} echo $token
%env TOKEN=$token
env: TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImlLdnhaRWpzbER3U01fcmVvWi1WOGxSLUVWbHRDenUwWUxvY2pxczBNeFEifQ.eyJ1c2VybmFtZSI6ImFkbWluIiwicm9sZSI6IkFkbWluIiwicGVybWlzc2lvbnMiOlsiYWRtaW5pc3RyYXRvciIsImNhbl9wcm92aXNpb24iLCJtYW5hZ2VfY2F0YWxvZyIsImNyZWF0ZV9wcm9qZWN0IiwiY3JlYXRlX3NwYWNlIiwiYWNjZXNzX2NhdGFsb2ciXSwiZ3JvdXBzIjpbMTAwMDBdLCJzdWIiOiJhZG1pbiIsImlzcyI6IktOT1hTU08iLCJhdWQiOiJEU1giLCJ1aWQiOiIxMDAwMzMwOTk5IiwiYXV0aGVudGljYXRvciI6ImRlZmF1bHQiLCJkaXNwbGF5X25hbWUiOiJhZG1pbiIsImlhdCI6MTY0OTkyOTU0MCwiZXhwIjoxNjQ5OTcyNzA0fQ.NOiHL6YOtH_G_HpFDNb_8hbQkHXgfPIEyp-T4ZxOiHo-qUymPPDqZEJqAy5tdiXRqnwZgLPbLteoJOVRaZR-B1tgZGp6sfvtAGPRbuDDgHDNXXFF6V-3mpXkniuLpD90owQhoGPoP3TdYL_VBXmHaqQ77zKHdX5_linxqFj8cPsfmoIJDzZqSFxecDHw1rUuQtWr_ME01TePN9syi3f4cTpyuk8lxlG9959Tv4PWooUa3RPsEPYmggjdj8gsILHkimQRS5c7q_kkbghnA3b82S10vrIlEKV7kauUKtRi-H1M7V_hTJefhvJNUtj82QeLBkAQCNUxyPgrGI-xmUnalw

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://cpd-spaces-api.eu-gb.cf.appdomain.cloud/#/Spaces/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": "curl_DL"}' \ "$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://cpd-spaces-api.eu-gb.cf.appdomain.cloud/#/Spaces/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://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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
env: MODEL_PAYLOAD={"space_id": "12b155e5-fc94-42f9-bef3-835e1603d814","name": "scikit_diabetes_model","description": "This is description","type": "scikit-learn_1.0", "software_spec": {"name": "runtime-22.1-py3.9"}}
%%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" \ "$DATAPLATFORM_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=d5d62413-b0d5-4721-8133-15201c875fc7

Create model revision for further update

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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
%env REVISION_PAYLOAD=$revision_payload
env: REVISION_PAYLOAD={"space_id": "12b155e5-fc94-42f9-bef3-835e1603d814", "commit_message": "Initial model."}
%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$REVISION_PAYLOAD" \ "$DATAPLATFORM_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-14T09:46:43.002Z" }, "created_at": "2022-04-14T09:46:27.446Z", "description": "This is description", "id": "d5d62413-b0d5-4721-8133-15201c875fc7", "modified_at": "2022-04-14T09:46:27.446Z", "name": "scikit_diabetes_model", "owner": "1000330999", "resource_key": "2280ef8c-abe9-49f9-92c3-b2de8718eda3", "rev": "1", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" }, "system": { "warnings": [] } }

Model content upload

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

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

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/models_upload_content" target="_blank" rel="noopener no referrer">Upload model content

%%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" \ "$DATAPLATFORM_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=f82de80e-78a8-46b6-9411-0fa81e12a824

Download model

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

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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" \ "$DATAPLATFORM_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://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Deployments/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": "12b155e5-fc94-42f9-bef3-835e1603d814","name": "Diabetes deployment", "description": "This is description","online": {},"hardware_spec": {"name": "S"},"asset": {"id": "d5d62413-b0d5-4721-8133-15201c875fc7"}}
%%bash --out deployment_id curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$DEPLOYMENT_PAYLOAD" \ "$DATAPLATFORM_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=7731eb49-d288-4460-8127-c0fdf345522a

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://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Deployments/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" \ "$DATAPLATFORM_URL/ml/v4/deployments/$DEPLOYMENT_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool

Scoring of a webservice

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

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Deployments/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
env: 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]]}]}
%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$SCORING_PAYLOAD"\ "$DATAPLATFORM_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://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Deployments/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" \ "$DATAPLATFORM_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://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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" \ "$DATAPLATFORM_URL/ml/v4/models/$MODEL_ID/revisions?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "first": { "href": "/ml/v4/models/d5d62413-b0d5-4721-8133-15201c875fc7/revisions?space_id=12b155e5-fc94-42f9-bef3-835e1603d814&limit=100" }, "limit": 100, "resources": [ { "metadata": { "commit_info": { "commit_message": "Initial model.", "committed_at": "2022-04-14T09:46:43.002Z" }, "created_at": "2022-04-14T09:46:27.446Z", "description": "This is description", "id": "d5d62413-b0d5-4721-8133-15201c875fc7", "modified_at": "2022-04-14T09:46:27.446Z", "name": "scikit_diabetes_model", "owner": "1000330999", "resource_key": "2280ef8c-abe9-49f9-92c3-b2de8718eda3", "rev": "1", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" } } ] }

Create second model revision

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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": "12b155e5-fc94-42f9-bef3-835e1603d814", "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" \ "$DATAPLATFORM_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": "Updated model.", "committed_at": "2022-04-14T09:49:55.002Z" }, "created_at": "2022-04-14T09:46:27.446Z", "description": "This is description", "id": "d5d62413-b0d5-4721-8133-15201c875fc7", "modified_at": "2022-04-14T09:47:09.866Z", "name": "scikit_diabetes_model", "owner": "1000330999", "resource_key": "ba1e34ba-3bf8-4fc9-963d-4e3f5c5ade01", "rev": "2", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" }, "system": { "warnings": [] } }

Update model metadata

For example update model name or description

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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" \ "$DATAPLATFORM_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-14T09:50:08.250Z" }, "created_at": "2022-04-14T09:46:27.446Z", "description": "This is description", "id": "d5d62413-b0d5-4721-8133-15201c875fc7", "modified_at": "2022-04-14T09:50:08.231Z", "name": "updated scikit model", "owner": "1000330999", "resource_key": "d3464e47-c86e-4929-b2f4-5b38d2aa03d1", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" }, "system": { "warnings": [] } }

Upload new model content

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

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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" \ "$DATAPLATFORM_URL/ml/v4/models/$MODEL_ID/content?space_id=$SPACE_ID&version=2020-08-01&content_format=native" \ | python -m json.tool
{ "attachment_id": "f7d49958-894e-48e4-a343-e06ca0406905", "content_format": "native", "persisted": true }

List model revisions to see a new one just created

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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" \ "$DATAPLATFORM_URL/ml/v4/models/$MODEL_ID/revisions?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "first": { "href": "/ml/v4/models/d5d62413-b0d5-4721-8133-15201c875fc7/revisions?space_id=12b155e5-fc94-42f9-bef3-835e1603d814&limit=100" }, "limit": 100, "resources": [ { "metadata": { "commit_info": { "commit_message": "Updated model.", "committed_at": "2022-04-14T09:49:55.002Z" }, "created_at": "2022-04-14T09:46:27.446Z", "description": "This is description", "id": "d5d62413-b0d5-4721-8133-15201c875fc7", "modified_at": "2022-04-14T09:47:09.866Z", "name": "scikit_diabetes_model", "owner": "1000330999", "resource_key": "ba1e34ba-3bf8-4fc9-963d-4e3f5c5ade01", "rev": "2", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" } }, { "metadata": { "commit_info": { "commit_message": "Initial model.", "committed_at": "2022-04-14T09:46:43.002Z" }, "created_at": "2022-04-14T09:46:27.446Z", "description": "This is description", "id": "d5d62413-b0d5-4721-8133-15201c875fc7", "modified_at": "2022-04-14T09:46:27.446Z", "name": "scikit_diabetes_model", "owner": "1000330999", "resource_key": "2280ef8c-abe9-49f9-92c3-b2de8718eda3", "rev": "1", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" } } ] }

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

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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" \ "$DATAPLATFORM_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-14T09:50:37.134Z" }, "created_at": "2022-04-14T09:46:27.446Z", "description": "This is description", "id": "d5d62413-b0d5-4721-8133-15201c875fc7", "modified_at": "2022-04-14T09:50:34.789Z", "name": "updated scikit model", "owner": "1000330999", "resource_key": "0444f87e-c700-4d45-b94e-8ca6f46f5a69", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" }, "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://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Deployments/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": "d5d62413-b0d5-4721-8133-15201c875fc7"}}]
%%bash curl -sk -X PATCH \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$REDEPLOY_PAYLOAD" \ "$DATAPLATFORM_URL/ml/v4/deployments/$DEPLOYMENT_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool

Score updated webservice

%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$SCORING_PAYLOAD" \ "$DATAPLATFORM_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" \ "$DATAPLATFORM_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://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Models/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" \ "$DATAPLATFORM_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-2025 IBM. This notebook and its source code are released under the terms of the MIT License.