Table of Contents
In this article, we will see how to install google cloud AI Python client library to interact with Vertex AI programmatically. If you are looking to interact with Vertex AI using python programming language then Google Cloud provides an AI Platform Python client library that you can install and use for this purpose. This client library would allow you to train machine learning models using custom training jobs or pre-built algorithms.
It will also allow you to deploy trained models as REST or gRPC endpoints. Using this client library, you can leverage Vertex AI's pre-trained models to perform various tasks like image-recognition, natural language processing etc. Similarly, you can interact and do whole lot of things using this client library. You can check more about this on official website.
How to Install Google Cloud AI Python client library to interact with Vertex AI
Also Read: How to Install Google Cloud BigQuery Python client library on Linux
Step 1: Prerequisites
a) You should have python 3.7
or higher installed.
b) You should have pip
utility installed in your System.
c) You should have Vertex AI
enabled for your project.
d) You should have proper authentication setup in Google cloud to interact with Vertex AI. In case if you would like set up authentication credentials through service account key then you can follow steps mentioned on How to Add a Service Accounts Key in Google Cloud in 7 Easy Steps article.
e) You should have proper roles and permissions to access cloud buckets and files in it.
Step 2: Install Google Cloud AI Platform client library
In the next step, you can install google-cloud-aiplatform client library by using pip install google-cloud-aiplatform
command as shown below. It will install client library along with all its dependencies.
cyberithub@ubuntu:~$ pip install google-cloud-aiplatform Defaulting to user installation because normal site-packages is not writeable Collecting google-cloud-aiplatform Downloading google_cloud_aiplatform-1.72.0-py2.py3-none-any.whl (6.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.2/6.2 MB 3.2 MB/s eta 0:00:00 Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.2 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform) (5.28.3) Collecting google-cloud-storage<3.0.0dev,>=1.32.0 Downloading google_cloud_storage-2.18.2-py2.py3-none-any.whl (130 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 130.5/130.5 KB 615.2 kB/s eta 0:00:00 Requirement already satisfied: google-cloud-bigquery!=3.20.0,<4.0.0dev,>=1.15.0 in ./.local/lib/python3.10/site-packages (from google-cloud-aiplatform) (3.27.0) Collecting google-cloud-resource-manager<3.0.0dev,>=1.3.3 Downloading google_cloud_resource_manager-1.13.1-py2.py3-none-any.whl (358 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 358.6/358.6 KB 2.3 MB/s eta 0:00:00 Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform) (1.25.0) Requirement already satisfied: packaging>=14.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform) (24.2) .....................................................
Step 3: Verify Installation
After successful installation, you can verify the library by running pip show google-cloud-aiplatform
command. If it is already installed, it should show like below where you will see detailed information about the client library package.
cyberithub@ubuntu:~$ pip show google-cloud-aiplatform Name: google-cloud-aiplatform Version: 1.72.0 Summary: Vertex AI API client library Home-page: https://github.com/googleapis/python-aiplatform Author: Google LLC Author-email: googleapis-packages@google.com License: Apache 2.0 Location: /home/cyberithub/.local/lib/python3.10/site-packages Requires: docstring-parser, google-api-core, google-auth, google-cloud-bigquery, google-cloud-resource-manager, google-cloud-storage, packaging, proto-plus, protobuf, pydantic, shapely Required-by:
Step 4: Create a Vertex AI Dataset
Now that AI Platform python client library is installed, let's use it to create a Vertex AI Dataset. Here we are using below python code to create a tabular dataset in Vertex AI.
from google.cloud import aiplatform aiplatform.init(project="cyberithub-project", location="us-west2") dataset = aiplatform.TabularDataset.create( display_name="cyberithub-ds", gcs_source=["gs://cyberithub-bucket/sample.csv"] ) print(f"Dataset created: {dataset.resource_name}")
Let's break above example and understand its different components. Firstly, we are importing Vertex AI client library in our python code to interact with Vertex AI services programmatically.
from google.cloud import aiplatform
In the next step, we are initializing the Vertex AI and providing the name of project and location where AI resources will be kept. In our case, we are creating all resources under cyberithub-project
and keeping it in us-west2
location.
aiplatform.init(project="cyberithub-project", location="us-west2")
After initializing project name and location, we are creating a tabular dataset in Vertex AI using TabularDataset.create
. It takes few parameters like display name and location of file to load data into dataset. In our case, we are using display name as cyberithub-ds
and loading data into dataset from sample.csv
stored in cyberithub-bucket
.
dataset = aiplatform.TabularDataset.create(
display_name="cyberithub-ds",
gcs_source=["gs://cyberithub-bucket/sample.csv"]
)
Finally we are displaying dataset created on output as shown below.
print(f"Dataset created: {dataset.resource_name}")
When you run above program, you should see an output like below. This confirms that client library is working as expected.
Dataset created: projects/9736183502/locations/us-west1/datasets/9428616403
Step 5: Uninstall Google Cloud AI Platform client library
Once you are done using AI Platform client library, you can choose to uninstall it from your python environment by using pip uninstall google-cloud-aiplatform
command as shown below.
cyberithub@ubuntu:~$ pip uninstall google-cloud-aiplatform Found existing installation: google-cloud-aiplatform 1.72.0 Uninstalling google-cloud-aiplatform-1.72.0: Would remove: /home/cyberithub/.local/bin/tb-gcp-uploader /home/cyberithub/.local/lib/python3.10/site-packages/google/cloud/aiplatform/* /home/cyberithub/.local/lib/python3.10/site-packages/google/cloud/aiplatform_v1/* /home/cyberithub/.local/lib/python3.10/site-packages/google/cloud/aiplatform_v1beta1/* /home/cyberithub/.local/lib/python3.10/site-packages/google_cloud_aiplatform-1.72.0-py3.9-nspkg.pth /home/cyberithub/.local/lib/python3.10/site-packages/google_cloud_aiplatform-1.72.0.dist-info/* /home/cyberithub/.local/lib/python3.10/site-packages/vertex_ray/* /home/cyberithub/.local/lib/python3.10/site-packages/vertexai/* Proceed (Y/n)? Y Successfully uninstalled google-cloud-aiplatform-1.72.0