Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cpd4.5/notebooks/rest_api/curl/experiments/autoai/Use AutoAI and batch deployment to predict credit risk.ipynb
6405 views
Kernel: Python 3 (ipykernel)

Use AutoAI and batch deployment to predict credit risk with Watson Machine Learning REST API

This notebook contains steps and code to demonstrate support of AutoAI experiments in Watson Machine Learning Service. It introduces commands for getting data, training experiments, persisting pipelines, publishing models, deploying models and scoring.

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

Learning goals

The learning goals of this notebook are:

  • Working with Watson Machine Learning experiments to train AutoAI models.

  • Downloading computed models to local storage.

  • Batch deployment and scoring of trained model.

Contents

This notebook contains the following parts:

  1. Setup

  2. Experiment definition

  3. Experiment Run

  4. Historical runs

  5. Deploy and Score

  6. Cleaning

  7. Summary and next steps

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.eyJ1c2VybmFtZSI6ImFkbWluIiwicm9sZSI6IkFkbWluIiwicGVybWlzc2lvbnMiOlsiYWRtaW5pc3RyYXRvciIsImNhbl9wcm92aXNpb24iLCJtYW5hZ2VfY2F0YWxvZyIsImNyZWF0ZV9wcm9qZWN0IiwiY3JlYXRlX3NwYWNlIiwiYWNjZXNzX2NhdGFsb2ciXSwiZ3JvdXBzIjpbMTAwMDBdLCJzdWIiOiJhZG1pbiIsImlzcyI6IktOT1hTU08iLCJhdWQiOiJEU1giLCJ1aWQiOiIxMDAwMzMwOTk5IiwiYXV0aGVudGljYXRvciI6ImRlZmF1bHQiLCJkaXNwbGF5X25hbWUiOiJhZG1pbiIsImlhdCI6MTY0OTkyNzQ2NCwiZXhwIjoxNjQ5OTcwNjI4fQ.v7u1n30Sfa5vDnvYIqo2D_9usWUMgMrznuU583nrChV9OEaHPe5ijMczXJLD6eBHJyiGLmChAahFCHlEH2sC4ojd00u6HN6sIDXSUEBdQgR--LxBXlohMelTrAhrbwX2x1u4XUJTNRo1PtA5D7ePEfIcOmnzMijk7UQJSM0fXh3s0znmOkHWjWVmGIALha4oAQ-LWgeOO2kQuYs6d7acWNVKVB2lwRgvIigc3kgMkoIv3FvG48yInO8vIVJEVK1CoHNeU3UmC3zB8D1r1luMRyWQLqwZD6cDU66UuHImAHGZxFMRqbCrCPVP5s-C8kHcalVrh3zPbRX5iwOKb2AR5w

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. Experiment / optimizer configuration

Provide input information for AutoAI experiment / optimizer:

  • name - experiment name

  • learning_type - type of the problem

  • label - target column name

  • scorer_for_ranking - optimization metric

  • holdout_param - percentage of training data to use as a holdout [0 - 1]

  • daub_include_only_estimators - list of estimators to use

You can modify parameters section of the following cURL call to change AutoAI experiment settings.

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Pipelines/pipelines_create" target="_blank" rel="noopener no referrer">Define AutoAI experiment.

%%bash --out pipeline_payload PIPELINE_PAYLOAD='{"space_id": "'"$SPACE_ID"'", "name": "Credit Risk Prediction - AutoAI", "description": "", "document": {"doc_type": "pipeline", "version": "2.0", "pipelines": [{"id": "autoai", "runtime_ref": "hybrid", "nodes": [{"id": "automl", "type": "execution_node", "parameters": {"stage_flag": true, "output_logs": true, "input_file_separator": ",", "optimization": {"learning_type": "binary", "label": "Risk", "max_num_daub_ensembles": 1, "daub_include_only_estimators": ["ExtraTreesClassifierEstimator", "GradientBoostingClassifierEstimator", "LGBMClassifierEstimator", "LogisticRegressionEstimator", "RandomForestClassifierEstimator", "XGBClassifierEstimator", "DecisionTreeClassifierEstimator"], "scorer_for_ranking": "roc_auc", "compute_pipeline_notebooks_flag": true, "run_cognito_flag": true, "holdout_param": 0.1}}, "runtime_ref": "autoai", "op": "kube"}]}], "runtimes": [{"id": "autoai", "name": "auto_ai.kb", "app_data": {"wml_data": {"hardware_spec": { "name": "M"}}}, "version": "3.0.2"}],"primary_pipeline": "autoai"}}' echo $PIPELINE_PAYLOAD
JSON(loads(pipeline_payload))
<IPython.core.display.JSON object>
%env PIPELINE_PAYLOAD=$pipeline_payload
%%bash --out pipeline_id curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$PIPELINE_PAYLOAD" \ "$DATAPLATFORM_URL/ml/v4/pipelines?version=2020-08-01" \ | grep '"id": ' | awk -F '"' '{ print $4 }' | sed -n 5p
%env PIPELINE_ID=$pipeline_id
env: PIPELINE_ID=8b3c84d0-4702-48d6-b7c7-66cf9cbf3c50

Get experiment details

To retrieve AutoAI experiment / optimizer configuration you can follow below cURL GET call.

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Pipelines/pipelines_get" target="_blank" rel="noopener no referrer">Get experiment / optimizer information

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$DATAPLATFORM_URL/ml/v4/pipelines/$PIPELINE_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "entity": { "document": { "doc_type": "pipeline", "pipelines": [ { "id": "autoai", "nodes": [ { "id": "automl", "op": "kube", "parameters": { "input_file_separator": ",", "optimization": { "compute_pipeline_notebooks_flag": true, "daub_include_only_estimators": [ "ExtraTreesClassifierEstimator", "GradientBoostingClassifierEstimator", "LGBMClassifierEstimator", "LogisticRegressionEstimator", "RandomForestClassifierEstimator", "XGBClassifierEstimator", "DecisionTreeClassifierEstimator" ], "holdout_param": 0.1, "label": "Risk", "learning_type": "binary", "max_num_daub_ensembles": 1.0, "run_cognito_flag": true, "scorer_for_ranking": "roc_auc" }, "output_logs": true, "stage_flag": true }, "runtime_ref": "autoai", "type": "execution_node" } ], "runtime_ref": "hybrid" } ], "primary_pipeline": "autoai", "runtimes": [ { "app_data": { "wml_data": { "hardware_spec": { "id": "c076e82c-b2a7-4d20-9c0f-1f0c2fdf5a24", "name": "M" } } }, "id": "autoai", "name": "auto_ai.kb", "version": "3.0.2" } ], "version": "2.0" } }, "metadata": { "created_at": "2022-04-14T09:12:36.362Z", "id": "8b3c84d0-4702-48d6-b7c7-66cf9cbf3c50", "modified_at": "2022-04-14T09:12:36.362Z", "name": "Credit Risk Prediction - AutoAI", "owner": "1000330999", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" }, "system": { "warnings": [] } }

Training data connection

Define connection information to COS bucket and training data CSV file. This example uses the German Credit Risk dataset.

The dataset can be downloaded from here. You can also download it to local filesystem by running the cell below.

Action: Upload training data to COS bucket and enter location information in the next cURL examples.

%%bash wget -q https://github.com/IBM/watson-machine-learning-samples/raw/master/cpd4.0/data/credit_risk/credit_risk_training_light.csv \ -O credit_risk_training_light.csv

Create Data Asset

Upload your local dataset into Data Asset Storage

<a href="https://cloud.ibm.com/apidocs/watson-data-api#createdataassetv2" target="_blank" rel="noopener no referrer">Upload file as Data Asset

%%bash --out data_asset_metadata DATA_ASSET_METADATA='{"metadata": {"name": "autoai_training_data","description": "desc","asset_type": "data_asset","origin_country": "us","asset_category": "USER"},"entity": {"data_asset": {"mime_type": "text/csv"}}}' echo $DATA_ASSET_METADATA
JSON(loads(data_asset_metadata))
<IPython.core.display.JSON object>
%env DATA_ASSET_METADATA=$data_asset_metadata
env: DATA_ASSET_METADATA={"metadata": {"name": "autoai_training_data","description": "desc","asset_type": "data_asset","origin_country": "us","asset_category": "USER"},"entity": {"data_asset": {"mime_type": "text/csv"}}}
%%bash --out asset_id asset_id=$(curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --data "$DATA_ASSET_METADATA" \ "$DATAPLATFORM_URL/v2/assets?space_id=$SPACE_ID&version=2020-08-01") asset_id=${asset_id#*asset_id\":\"} asset_id=${asset_id%%\"*} echo $asset_id
%env ASSET_ID=$asset_id
env: ASSET_ID=6ea1f40e-07a8-4c31-bfc9-6c4caf35cafb
%%bash --out attachment ATTACHMENT='{"asset_type": "data_asset", "name": "credit_risk_training_light.csv", "mime": "text/csv"}' echo $ATTACHMENT
%env ATTACHMENT=$attachment
env: ATTACHMENT={"asset_type": "data_asset", "name": "credit_risk_training_light.csv", "mime": "text/csv"}
%%bash --out attachment_response curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --data "$ATTACHMENT" \ "$DATAPLATFORM_URL/v2/assets/$ASSET_ID/attachments?space_id=$SPACE_ID&version=2020-08-01"
%env ATTACHMENT_RESPONSE=$attachment_response
env: ATTACHMENT_RESPONSE={"attachment_id":"00fd607c-4d2f-4b0d-a7bb-31d163352ac8","asset_type":"data_asset","attachment_type":"local","upload_id":"7956c19a-d412-45d0-b1f1-7328a0a648a8","data_partitions":1,"start_part_num":1,"url1":"/v2/asset_files/8d2ebb07-8c1c-471e-8efc-2613941b092b/6ea1f40e-07a8-4c31-bfc9-6c4caf35cafb/00fd607c-4d2f-4b0d-a7bb-31d163352ac8?space_id=12b155e5-fc94-42f9-bef3-835e1603d814&signature=4PBMTFP%2BguEV02y8m1%2FF2A%3D%3D%3Aso2Lo00aXZ5NEZhQpXDrO6VM5FWcIs7KCMyVZJ7BPQBhpecx2XRe97iDCmQIjQ0juFjPOmtvwSM85k%2Frx5WTYFbTm8%2FiDkRcqDwqIHaZS7wKQZUB8MvKvIBcIkwuKUprzAvuqglw8EOHJ9ioeSL%2FrNMHNGeaPloeQG%2FandYq1yDkrTGRT96xrymYqFpWrVXqVbx0XQUkSOGae2LSp88qpRdK9HTNYFBZzwoaLSB5kBiTOYtOG%2BiFmiJaz2Gzz294CYXuGyaa3irejem5B4IROEyU6Kv8SlfeVt8NGQHLye8fp4UPfTemE9E%3D","datasource_type":"81bafdbd-b7c6-45c5-a4fd-6ec135f66f4e","is_partitioned":false,"name":"credit_risk_training_light.csv","mime":"text/csv","user_data":{},"href":"/v2/assets/6ea1f40e-07a8-4c31-bfc9-6c4caf35cafb/attachments/00fd607c-4d2f-4b0d-a7bb-31d163352ac8?space_id=12b155e5-fc94-42f9-bef3-835e1603d814","asset_category":"USER"}
%%bash --out attachment_id echo $ATTACHMENT_RESPONSE | cut -d '"' -f 4 | tr -d '\n'
%env ATTACHMENT_ID=$attachment_id
env: ATTACHMENT_ID=00fd607c-4d2f-4b0d-a7bb-31d163352ac8
%%bash --out attachment_url ATTACHMENT_URL=${ATTACHMENT_RESPONSE#*url1\":\"} ATTACHMENT_URL=${ATTACHMENT_URL%%\"*} echo $ATTACHMENT_URL
%env ATTACHMENT_URL=$attachment_url
env: ATTACHMENT_URL=/v2/asset_files/8d2ebb07-8c1c-471e-8efc-2613941b092b/6ea1f40e-07a8-4c31-bfc9-6c4caf35cafb/00fd607c-4d2f-4b0d-a7bb-31d163352ac8?space_id=12b155e5-fc94-42f9-bef3-835e1603d814&signature=4PBMTFP%2BguEV02y8m1%2FF2A%3D%3D%3Aso2Lo00aXZ5NEZhQpXDrO6VM5FWcIs7KCMyVZJ7BPQBhpecx2XRe97iDCmQIjQ0juFjPOmtvwSM85k%2Frx5WTYFbTm8%2FiDkRcqDwqIHaZS7wKQZUB8MvKvIBcIkwuKUprzAvuqglw8EOHJ9ioeSL%2FrNMHNGeaPloeQG%2FandYq1yDkrTGRT96xrymYqFpWrVXqVbx0XQUkSOGae2LSp88qpRdK9HTNYFBZzwoaLSB5kBiTOYtOG%2BiFmiJaz2Gzz294CYXuGyaa3irejem5B4IROEyU6Kv8SlfeVt8NGQHLye8fp4UPfTemE9E%3D
%%bash curl -sk -X PUT \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: multipart/form-data" \ -F 'file=@credit_risk_training_light.csv' \ "$DATAPLATFORM_URL$ATTACHMENT_URL"
{"status":"Asset created: The asset was successfully uploaded."}

The response should looks like this: {"status":"Asset created: The asset was successfully uploaded."}

%%bash curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ "$DATAPLATFORM_URL/v2/assets/$ASSET_ID/attachments/$ATTACHMENT_ID/complete?space_id=$SPACE_ID&version=2020-08-01"
{"attachment_id":"00fd607c-4d2f-4b0d-a7bb-31d163352ac8","asset_type":"data_asset","name":"credit_risk_training_light.csv","mime":"text/csv","size":34787}

3. Experiment run

This section provides samples about how to trigger AutoAI experiment via cURL calls.

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Trainings/trainings_create" target="_blank" rel="noopener no referrer">Schedule a training job for AutoAI experiment

%%bash --out random_id openssl rand -hex 20
%env RANDOM_ID=$random_id
env: RANDOM_ID=e42413bc96a6c32fc6e770e0ea5a5ff491cdaa00
%%bash --out training_payload TRAINING_PAYLOAD='{"space_id": "'"$SPACE_ID"'", "training_data_references": [{"type": "data_asset", "id": "credit_risk_training_light.csv", "connection": {}, "location": {"href": "/v2/assets/'"$ASSET_ID"'?space_id='"$SPACE_ID"'"}}], "results_reference": {"type": "fs", "id": "autoai_results", "connection": {}, "location": {"path": "/spaces/'$SPACE_ID'/assets/auto_ml/auto_ml_curl.'$RANDOM_ID'/wml_data"}}, "tags": [{"value": "autoai"}], "pipeline": {"id": "'"$PIPELINE_ID"'"}}' echo $TRAINING_PAYLOAD
JSON(loads(training_payload))
<IPython.core.display.JSON object>
%env TRAINING_PAYLOAD=$training_payload
env: TRAINING_PAYLOAD={"space_id": "12b155e5-fc94-42f9-bef3-835e1603d814", "training_data_references": [{"type": "data_asset", "id": "credit_risk_training_light.csv", "connection": {}, "location": {"href": "/v2/assets/6ea1f40e-07a8-4c31-bfc9-6c4caf35cafb?space_id=12b155e5-fc94-42f9-bef3-835e1603d814"}}], "results_reference": {"type": "fs", "id": "autoai_results", "connection": {}, "location": {"path": "/spaces/12b155e5-fc94-42f9-bef3-835e1603d814/assets/auto_ml/auto_ml_curl.e42413bc96a6c32fc6e770e0ea5a5ff491cdaa00/wml_data"}}, "tags": [{"value": "autoai"}], "pipeline": {"id": "8b3c84d0-4702-48d6-b7c7-66cf9cbf3c50"}}
%%bash --out training_id curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$TRAINING_PAYLOAD" \ "$DATAPLATFORM_URL/ml/v4/trainings?version=2020-08-01" \ | awk -F'"id":' '{print $2}' | cut -c2-37
%env TRAINING_ID=$training_id
env: TRAINING_ID=27ea20b9-0e7c-4153-a6e3-c44491463d25

Get training details

Training is an asynchronous endpoint. In case you want to monitor training status and details, you need to use a GET method and specify which training you want to monitor by usage of training ID.

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Trainings/trainings_get" target="_blank" rel="noopener no referrer">Get information about training job

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

Get training status

%%bash STATUS=$(curl -sk -X GET\ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$DATAPLATFORM_URL/ml/v4/trainings/$TRAINING_ID?space_id=$SPACE_ID&version=2020-08-01") STATUS=${STATUS#*state\":\"} STATUS=${STATUS%%\"*} echo $STATUS
completed

Please make sure that training is completed before you go to the next sections. Monitor state of your training by running above cell couple of times.

4. Historical runs

In this section you will see cURL examples describing how to get historical training runs information.

Output should be similar to the output from training creation but you should see more trainings entries. Listing trainings:

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Trainings/trainings_list" target="_blank" rel="noopener no referrer">Get list of historical training jobs information

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

Cancel training run

Tip: If you want to cancel your training, please convert below cell to code, specify training ID and run.

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Trainings/trainings_delete" target="_blank" rel="noopener no referrer">Canceling training

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

5. Deploy and Score

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

Before deployment creation, you need store your model in WML repository. Please see below cURL call example how to do it. Remember that you need to specify where your chosen model is stored in COS.

Store AutoAI model

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=$(curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$DATAPLATFORM_URL/v2/asset_files/auto_ml/auto_ml_curl.$RANDOM_ID/wml_data/$TRAINING_ID/assets/$TRAINING_ID""_P1_global_output/resources/wml_model/request.json?space_id=$SPACE_ID") echo $MODEL_PAYLOAD
JSON(loads(model_payload))
<IPython.core.display.JSON object>
%env MODEL_PAYLOAD=$model_payload
%%bash --out model_details MODEL_DETAILS=$(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&space_id=$SPACE_ID") echo $MODEL_DETAILS
JSON(loads(model_details))
<IPython.core.display.JSON object>
%env MODEL_DETAILS=$model_details
%%bash --out model_id echo $MODEL_DETAILS | awk -F '"id": ' '{ print $8 }' | cut -d '"' -f 2
%env MODEL_ID=$model_id
env: MODEL_ID=8a2ab35b-1fc6-4fda-9678-f32b794b90c3

Download model content

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

Deployment creation

An AutoAI Batch deployment creation is presented below.

<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": "AutoAI deployment","description": "This is description","batch": {}, "hybrid_pipeline_hardware_specs": [{"node_runtime_id": "auto_ai.kb", "hardware_spec": {"name": "M"}}],"asset": {"id": "'"$MODEL_ID"'"}}' echo $DEPLOYMENT_PAYLOAD
JSON(loads(deployment_payload))
<IPython.core.display.JSON object>
%env DEPLOYMENT_PAYLOAD=$deployment_payload
%%bash --out deployment_details DETAILS=$(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") echo $DETAILS
%env DEPLOYMENT_DETAILS=$deployment_details
env: DEPLOYMENT_DETAILS={ "entity": { "asset": { "id": "8a2ab35b-1fc6-4fda-9678-f32b794b90c3" }, "batch": { }, "custom": { }, "deployed_asset_type": "model", "description": "This is description", "hybrid_pipeline_hardware_specs": [{ "hardware_spec": { "name": "M" }, "node_runtime_id": "auto_ai.kb" }], "name": "AutoAI deployment", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814", "status": { "state": "ready" } }, "metadata": { "created_at": "2022-04-14T09:27:39.734Z", "description": "This is description", "id": "8a0a9650-d349-4373-8c0c-194e4abc7d55", "modified_at": "2022-04-14T09:27:39.734Z", "name": "AutoAI deployment", "owner": "1000330999", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" } }
%%bash --out deployment_id echo $DEPLOYMENT_DETAILS | awk -F '"id": ' '{ print $3 }' | cut -d '"' -f 2
%env DEPLOYMENT_ID=$deployment_id
env: DEPLOYMENT_ID=8a0a9650-d349-4373-8c0c-194e4abc7d55

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
{ "entity": { "asset": { "id": "8a2ab35b-1fc6-4fda-9678-f32b794b90c3" }, "batch": {}, "custom": {}, "deployed_asset_type": "model", "description": "This is description", "hybrid_pipeline_hardware_specs": [ { "hardware_spec": { "name": "M" }, "node_runtime_id": "auto_ai.kb" } ], "name": "AutoAI deployment", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814", "status": { "state": "ready" } }, "metadata": { "created_at": "2022-04-14T09:27:39.734Z", "description": "This is description", "id": "8a0a9650-d349-4373-8c0c-194e4abc7d55", "modified_at": "2022-04-14T09:27:39.734Z", "name": "AutoAI deployment", "owner": "1000330999", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" } }

Score your Batch deployment

Scoring for Batch deployment is done by creating jobs. User can specify job payload as a json or as data connection to eg. COS.

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Deployment Jobs/deployment_jobs_create" target="_blank" rel="noopener no referrer">Create deployment job

%%bash --out job_payload JOB_PAYLOAD='{"name": "AutoAI job", "space_id": "'"$SPACE_ID"'","deployment": {"id": "'"$DEPLOYMENT_ID"'"}, "hybrid_pipeline_hardware_specs": [{"node_runtime_id": "auto_ai.kb", "hardware_spec": {"name": "M"}}], "scoring": {"input_data": [{"fields": ["CheckingStatus", "LoanDuration", "CreditHistory", "LoanPurpose", "LoanAmount", "ExistingSavings", "EmploymentDuration", "InstallmentPercent", "Sex", "OthersOnLoan", "CurrentResidenceDuration", "OwnsProperty", "Age", "InstallmentPlans", "Housing", "ExistingCreditsCount", "Job", "Dependents", "Telephone", "ForeignWorker"], "values": [["less_0", 6, "all_credits_paid_back", "car_used", 250, "less_100", "1_to_4", 2, "male", "none", 2, "savings_insurance", 28, "stores", "rent", 1, "skilled", 1, "none", "yes"]]}]}}' echo $JOB_PAYLOAD
JSON(loads(job_payload))
<IPython.core.display.JSON object>
%env JOB_PAYLOAD=$job_payload
env: JOB_PAYLOAD={"name": "AutoAI job", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814","deployment": {"id": "8a0a9650-d349-4373-8c0c-194e4abc7d55"}, "hybrid_pipeline_hardware_specs": [{"node_runtime_id": "auto_ai.kb", "hardware_spec": {"name": "M"}}], "scoring": {"input_data": [{"fields": ["CheckingStatus", "LoanDuration", "CreditHistory", "LoanPurpose", "LoanAmount", "ExistingSavings", "EmploymentDuration", "InstallmentPercent", "Sex", "OthersOnLoan", "CurrentResidenceDuration", "OwnsProperty", "Age", "InstallmentPlans", "Housing", "ExistingCreditsCount", "Job", "Dependents", "Telephone", "ForeignWorker"], "values": [["less_0", 6, "all_credits_paid_back", "car_used", 250, "less_100", "1_to_4", 2, "male", "none", 2, "savings_insurance", 28, "stores", "rent", 1, "skilled", 1, "none", "yes"]]}]}}
%%bash --out job_id curl -sk -X POST \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ --data "$JOB_PAYLOAD" \ "$DATAPLATFORM_URL/ml/v4/deployment_jobs?version=2020-08-01" \ | grep '"id": ' | awk -F '"' '{ print $4 }' | sed -n 2p
%env JOB_ID=$job_id
env: JOB_ID=8af8f408-23ab-46c2-b322-fd5b9cba32bf

Listing all Batch jobs

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

Get particular job details

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Deployment Jobs/deployment_jobs_get" target="_blank" rel="noopener no referrer">Get job details

%%bash curl -sk -X GET \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$DATAPLATFORM_URL/ml/v4/deployment_jobs/$JOB_ID?space_id=$SPACE_ID&version=2020-08-01" \ | python -m json.tool
{ "entity": { "deployment": { "id": "8a0a9650-d349-4373-8c0c-194e4abc7d55" }, "platform_job": { "job_id": "9dfa2750-906d-46ca-a592-b246877fd583", "run_id": "d0b841e9-8263-4425-94d0-07184d60dbbc" }, "scoring": { "input_data": [ { "fields": [ "CheckingStatus", "LoanDuration", "CreditHistory", "LoanPurpose", "LoanAmount", "ExistingSavings", "EmploymentDuration", "InstallmentPercent", "Sex", "OthersOnLoan", "CurrentResidenceDuration", "OwnsProperty", "Age", "InstallmentPlans", "Housing", "ExistingCreditsCount", "Job", "Dependents", "Telephone", "ForeignWorker" ], "values": [ [ "less_0", 6, "all_credits_paid_back", "car_used", 250, "less_100", "1_to_4", 2, "male", "none", 2, "savings_insurance", 28, "stores", "rent", 1, "skilled", 1, "none", "yes" ] ] } ], "predictions": [ { "fields": [ "prediction", "probability" ], "values": [ [ "No Risk", [ 0.9936711491879138, 0.0063288508120862424 ] ] ] } ], "status": { "completed_at": "2022-04-14T09:29:20.604Z", "running_at": "2022-04-14T09:29:14.901Z", "state": "completed" } } }, "metadata": { "created_at": "2022-04-14T09:28:32.493Z", "id": "8af8f408-23ab-46c2-b322-fd5b9cba32bf", "modified_at": "2022-04-14T09:29:21.752Z", "name": "AutoAI job", "space_id": "12b155e5-fc94-42f9-bef3-835e1603d814" } }

Cancel job

Tip: You can cancel running job by calling delete method. Just convert below cell to code and run it.

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

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

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.

Delete training run

Tip: You can completely delete a training run with its metadata.

<a href="https://watson-ml-v4-api.mybluemix.net/wml-restapi-cloud.html#/Trainings/trainings_delete" target="_blank" rel="noopener no referrer">Deleting training

%%bash TRAINING_ID_TO_DELETE=... curl -sk -X DELETE \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/json" \ --header "Accept: application/json" \ "$DATAPLATFORM_URL/ml/v4/trainings/$TRAINING_ID_TO_DELETE?space_id=$SPACE_ID&version=2020-08-01&hard_delete=true"

Delete job

Tip: If you want remove job completely (with metadata), just specify hard_delete to True.

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

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 AutoAI 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.