Path: blob/master/cpd5.2/notebooks/python_sdk/lifecycle-management/Use Python API to automate AutoAI deployment lifecycle.ipynb
6397 views
Use Python API to automate AutoAI deployment lifecycle
This notebook contains the steps and code to demonstrate support of AI Lifecycle features of the AutoAI model in watsonx.ai service. It contains steps and code to work with ibm-watsonx-ai SDK available in PyPI repository. It also introduces commands for training, persisting and deploying model, scoring it, updating the model and redeploying it.
Some familiarity with Python is helpful. This notebook uses Python 3.12.
Learning goals
The learning goals of this notebook are:
List all deprecated and unsupported deployments.
Identify AutoAI models that need to be retrained.
Work with watsonx.ai experiments to re-train AutoAI models.
Persist an updated AutoAI model in watsonx.ai repository.
Redeploy model in-place.
Score sample records using client library.
Contents
This notebook contains the following parts:
Install dependencies
Note: ibm-watsonx-ai
documentation can be found here.
Successfully installed wget-3.2
Requirement already satisfied: threadpoolctl>=3.1.0 in /opt/user-env/pyt6/lib64/python3.12/site-packages (from scikit-learn==1.6.1) (3.6.0)
Successfully installed autoai-libs-3.0.3
Successfully installed ibm-watsonx-ai-1.3.20
Define credentials
Authenticate the watsonx.ai Runtime service on IBM Cloud Pak for Data. You need to provide the admin's username
and the platform url
.
Use the admin's api_key
to authenticate watsonx.ai Runtime services:
Alternatively you can use the admin's password
:
Create APIClient
instance
Working with spaces
First of all, you need to create a space that will be used for your work. If you do not have space already created, you can use {PLATFORM_URL}/ml-runtime/spaces?context=icp4data
to create one.
Click New Deployment Space
Create an empty space
Go to space
Settings
tabCopy
space_id
and paste it below
Tip: You can also use SDK to prepare the space for your work. More information can be found here.
You can use the list
method to print all existing spaces.
Extract all space IDs
You can also list deployments under particular space. The output contains SPEC_STATE
and SPEC_REPLACEMENT
. Set the working space.
List deployments under this space.
3. Identification of model requiring retraining
Pick up deployment of the AutoAI model you wish to retrain.
Hint: You can also do that programatically in the loop sequence over spaces check (Check the state of your deployments
cell). Hint: You can also use software_specification information (model details) to identify models and deployments that are not yet deprecated but can be retrained (updated software specification is available).
Extract the deployed model's details (including the pipeline information).
Find the AutoAI experiment runs matching the extracted pipeline
Extract the project ID where the training took place.
Tip: For more information about using AutoAI with projects, see this sample notebook.
Note: If the training took place in a space, please update accordingly.
Extract AutoAI experiment training_id
The training_id
is available in model's details.
Explore experiment's results
Connect to finished experiment and preview the results.
Evaluate the best model locally
Load the model for test purposes.
Hint: The best model is returned automatically if no pipeline_name
provided.
This cell constructs the cell scorer based on the experiment metadata.
Read the train and holdout data.
Hint: You can also use external test dataset.
Calculate the score
List stored models.
6. Redeploy and score new version of the model
In this section, you'll learn how to redeploy new version of the model by using the watsonx.ai Client.
Hint: As a best practice please consider using the test space before moving to production.
Promote model to deployment space
Check current deployment details before update.
Update the deployment with new model
Note: The update is asynchronous.
Wait for the deployment update:
Get updated deployment details
Score updated model
Create sample payload and score the deployed model.
Use client.deployments.score() method to run scoring.
If you want to clean up all created assets:
experiments
trainings
pipelines
models
deployments
please follow up this sample notebook.
You successfully completed this notebook! You learned how to use scikit-learn machine learning as well as watsonx.ai for model creation and deployment.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors
Lukasz Cmielowski, PhD, is a Senior Technical Staff Member at IBM with a track record of developing enterprise-level applications that substantially increases clients' ability to turn data into actionable knowledge.
Dorota Lączak, Python Software Developer in Watson Machine Learning AutoAI at IBM
Copyright © 2023-2025 IBM. This notebook and its source code are released under the terms of the MIT License.