Compare commits

...
Author SHA1 Message Date
gericdongandGitHub 26afe67e9c Merge branch 'main' into batch_image_model 2022-09-01 12:48:47 -04:00
6cac60f74a Sdk feature store ver1 (#790)
* Added condition to create Featurestore if it doesn't exist

* Ran Linter Test

* Made changes mentioned in review

* Ran Linter Test

* Attached uuid to featurestore_id to avoid error while creating featurestore with existing name

* Ran linter test

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-09-01 08:57:16 -07:00
Andrew FerlitschandGitHub c6ddaa183b Merge branch 'main' into batch_image_model 2022-09-01 07:59:02 -07:00
Andrew Ferlitsch 066c13369e feat: extend image batch notebook 2022-08-31 22:49:04 +00:00
Andrew Ferlitsch 7aae7a9d20 feat: extend image batch notebook 2022-08-31 22:48:11 +00:00
Andrew FerlitschandGitHub 55e37f795c feat: notebook for batch prediction with custom image model (#915)
* feat: add notebook for custom image model batch prediction

* feat: add notebook for custom image model batch prediction

* fix: review comments

* fix: review comments
2022-08-31 10:38:31 -07:00
Andrew Ferlitsch 0998b895f6 fix: review comments 2022-08-31 17:26:14 +00:00
Andrew Ferlitsch 28011f3894 fix: review comments 2022-08-31 17:24:58 +00:00
Andrew Ferlitsch 81822159bf feat: add notebook for custom image model batch prediction 2022-08-31 16:00:00 +00:00
Andrew Ferlitsch 8d59de715e feat: add notebook for custom image model batch prediction 2022-08-31 15:58:33 +00:00
c030d7ef74 use dataset instead of datasets (#892)
less chance for an error and confusion in name clashing with the `datasets` pypi package also used in the notebook.

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-08-31 07:44:46 -07:00
Andrew FerlitschandGitHub 96be449c69 feat: add notebook for MM autoML (#912)
* feat: model monitoring for AutoML

* feat: model monitoring for AutoML

* fix: correction on AutoML

* fix: correction on AutoML
2022-08-30 12:45:45 -07:00
e40ddab4d5 Upgrade to DataprocPySparkBatch v1 component and add Vertex AI placeholder features (#910)
* Fix typo in notebook heading.

* Fix linting issues.

* Use gcpc v1 components and use Vertex AI for model upload and serving.

* Notebook cleanup

* Minor heading cleanup

* Fix linting issues

* Fix linting issues

* Fix linting issues

* Fix linting issues

Co-authored-by: Win Woo <wwoo@google.com>
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-08-30 10:46:21 -07:00
Michael HuandGitHub d02bc2d56b pin arima notebook dependencies (#905)
Pins the versions of packages installed in the notebook in anticipation for a breaking change to the GCPC package.
2022-08-29 19:35:54 -04:00
76b641b23d fix: private endpoint example (#909)
* fix: remove gcloud usage

* fix: remove gcloud usage

* fix: review comments

* fix: review comments

Co-authored-by: nayaknishant <nishantnayak@google.com>
2022-08-29 12:16:18 -07:00
9 changed files with 3505 additions and 165 deletions
@@ -658,8 +658,8 @@
},
"outputs": [],
"source": [
"datasets = load_dataset(\"imdb\")\n",
"datasets"
"dataset = load_dataset(\"imdb\")\n",
"dataset"
]
},
{
@@ -668,7 +668,7 @@
"id": "RzfPtOMoIrIu"
},
"source": [
"The `datasets` object itself is [`DatasetDict`](https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasetdict), which contains one key for the training, validation and test set."
"The `dataset` object itself is [`DatasetDict`](https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasetdict), which contains one key for the training, validation and test set."
]
},
{
@@ -681,12 +681,12 @@
"source": [
"print(\n",
" \"Total # of rows in training dataset {} and size {:5.2f} MB\".format(\n",
" datasets[\"train\"].shape[0], datasets[\"train\"].size_in_bytes / (1024 * 1024)\n",
" dataset[\"train\"].shape[0], dataset[\"train\"].size_in_bytes / (1024 * 1024)\n",
" )\n",
")\n",
"print(\n",
" \"Total # of rows in test dataset {} and size {:5.2f} MB\".format(\n",
" datasets[\"test\"].shape[0], datasets[\"test\"].size_in_bytes / (1024 * 1024)\n",
" dataset[\"test\"].shape[0], dataset[\"test\"].size_in_bytes / (1024 * 1024)\n",
" )\n",
")"
]
@@ -708,7 +708,7 @@
},
"outputs": [],
"source": [
"datasets[\"train\"][0]"
"dataset[\"train\"][0]"
]
},
{
@@ -728,7 +728,7 @@
},
"outputs": [],
"source": [
"label_list = datasets[\"train\"].unique(\"label\")\n",
"label_list = dataset[\"train\"].unique(\"label\")\n",
"label_list"
]
},
@@ -779,7 +779,7 @@
},
"outputs": [],
"source": [
"show_random_elements(datasets[\"train\"])"
"show_random_elements(dataset[\"train\"])"
]
},
{
@@ -883,7 +883,7 @@
},
"outputs": [],
"source": [
"example = datasets[\"train\"][4]\n",
"example = dataset[\"train\"][4]\n",
"print(example)"
]
},
@@ -920,7 +920,7 @@
"source": [
"# Dataset loading repeated here to make this cell idempotent\n",
"# Since we are over-writing datasets variable\n",
"datasets = load_dataset(\"imdb\")\n",
"dataset = load_dataset(\"imdb\")\n",
"\n",
"# Mapping labels to ids\n",
"# NOTE: We can extract this automatically but the `Unique` method of the datasets\n",
@@ -948,7 +948,7 @@
"\n",
"\n",
"# apply preprocessing function to input examples\n",
"datasets = datasets.map(preprocess_function, batched=True, load_from_cache_file=True)"
"dataset = dataset.map(preprocess_function, batched=True, load_from_cache_file=True)"
]
},
{
@@ -1091,8 +1091,8 @@
"trainer = Trainer(\n",
" model,\n",
" args,\n",
" train_dataset=datasets[\"train\"],\n",
" eval_dataset=datasets[\"test\"],\n",
" train_dataset=dataset[\"train\"],\n",
" eval_dataset=dataset[\"test\"],\n",
" data_collator=default_data_collator,\n",
" tokenizer=tokenizer,\n",
" compute_metrics=compute_metrics,\n",
@@ -385,12 +385,11 @@
"import sys\n",
"\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = False\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" IS_COLAB = True\n",
" from google.colab import auth as google_auth\n",
"\n",
" google_auth.authenticate_user()\n",
@@ -1068,7 +1067,6 @@
"- `model`: The `Model` resource.\n",
"- `deployed_model_displayed_name`: The human readable name for the deployed model instance.\n",
"- `machine_type`: The machine type for each VM instance.\n",
"- `traffic_split`: Set to `{}` to indicate no traffic split.\n",
"\n",
"Do to the requirements to provision the resource, this may take upto a few minutes."
]
@@ -1085,7 +1083,6 @@
" model=model,\n",
" deployed_model_display_name=\"example_\" + TIMESTAMP,\n",
" machine_type=DEPLOY_COMPUTE,\n",
" traffic_split={}, # no traffic split\n",
")\n",
"\n",
"print(endpoint)"
@@ -1187,62 +1184,6 @@
" f.write(json.dumps({\"instances\": [{serving_input: {\"b64\": b64str}}]}))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "23e995c35fd6"
},
"source": [
"#### Construct the `Private Endpoint` URI\n",
"\n",
"Next, you construct the URI for the `Private Endpoint`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "97b248b2efb5"
},
"outputs": [],
"source": [
"endpoint_id = endpoint.resource_name\n",
"\n",
"ENDPOINT_URL = ! gcloud beta ai endpoints describe {endpoint_id} \\\n",
" --region={REGION} \\\n",
" --format=\"value(deployedModels.privateEndpoints.predictHttpUri)\"\n",
"\n",
"private_url = ENDPOINT_URL[1]\n",
"print(private_url)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "27605b5f0c3a"
},
"source": [
"### Make the prediction request using curl\n",
"\n",
"Use `curl` to make the prediction request to the private URI."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6cb568e6bb49"
},
"outputs": [],
"source": [
"output = ! curl -X POST -d@instances.json $private_url\n",
"\n",
"predictions = output[5]\n",
"print(predictions)\n",
"\n",
"! rm test.jpg instances.json"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -1251,7 +1192,7 @@
"source": [
"### Make the prediction request using SDK\n",
"\n",
"Finally, use the `Vertex AI SDK` to make a prediction request."
"Next, use the `Vertex AI SDK` to make a prediction request."
]
},
{
File diff suppressed because it is too large Load Diff
@@ -765,7 +765,7 @@
"source": [
"## Introduction to Vertex AI Model Monitoring\n",
"\n",
"Vertex AI Model Monitoring is supported for AutoML tabular models and custom tabular model. You can monitor for skew and drift detection of the features in the inbound prediction requests or skew and drift detection of the feature attributions (Explainable AI) in the outbound prediction response -- that is, the distribution of the attributions on how they contributed to the output (predictions).\n",
"Vertex AI Model Monitoring is supported for AutoML tabular models and custom tabular models. You can monitor for skew and drift detection of the features in the inbound prediction requests or skew and drift detection of the feature attributions (Explainable AI) in the outbound prediction response -- that is, the distribution of the attributions on how they contributed to the output (predictions).\n",
"\n",
"The following are the basic steps to enable model monitoring:\n",
"\n",
@@ -780,11 +780,11 @@
"\n",
"When model monitoring is enabled, the sampled incoming prediction requests are logged into a BigQuery table. The input feature values contained in the logged requests are then analyzed for skew or drift on an specified interval basis. You set a sampling rate to monitor a subset of the production inputs to a model, and the monitoring interval.\n",
"\n",
"The model monitoring service needs to know how to parse the feature values, which is referred to as the input schema. For AutoML tabular models, the input schema is automatically provided. For custom tabular models, the service will attempt to automatically derive the input schema from the first 1000 prediction requests. Alternatively, one can upload the input schema.\n",
"The model monitoring service needs to know how to parse the feature values, which is referred to as the input schema. For AutoML tabular models, the input schema is automatically generated. For custom tabular models, the service will attempt to automatically derive the input schema from the first 1000 prediction requests. Alternatively, one can upload the input schema.\n",
"\n",
"For skew detection, the monitoring service requires a baseline for the statistical distribution of values in the training data. For AutoML tabular models this is automatically derived. For custom tabular models, you upload the training data to the service, and have the service automatically derive the distribution.\n",
"\n",
"For feature attribution skew and drift detection, requires enabling your deployed model for `Vertex AI Explainability`\n",
"For feature attribution skew and drift detection, requires enabling your deployed model for `Vertex AI Explainability` for custom tabular models. For AutoML models, `Vertex AI Explainability` is automatically enabled.\n",
"\n",
"Learn more about [Introduction to Vertex AI Model Monitoring](https://cloud.google.com/vertex-ai/docs/model-monitoring/overview)."
]
@@ -1344,7 +1344,7 @@
"\n",
"Once the monitoring service has started, the sampled prediction requests will be logged to Cloud Storage. On the next monitoring interval, the sampled predictions are then copied over to the BigQuery logging table. Once the entries are in the BigQuery table, the monitoring service will analyze the sampled data.\n",
"\n",
"Next, you wait for the first logged entres to appear in the BigQuery table used for logging prediction samples. Since you sent a 1000 prediction requests, with 50% sampling, you should see around 500 entries."
"Next, you wait for the first logged entres to appear in the BigQuery table used for logging prediction samples. Since you sent 1000 prediction requests, with 50% sampling, you should see around 500 entries."
]
},
{
@@ -1404,11 +1404,11 @@
"\n",
"Training Prediction Skew Anomalies (Raw Feature):\n",
"\n",
"Anomalies Report Path(Google Cloud Storage): gs://cloud-ai-platform-773884b1-2a32-48d6-8b83-c03cde416b68/model_monitoring/job-8672170640054157312/serving/2022-08-25T00:00/stats_and_anomalies/5653675595884658688/anomalies/training_prediction_skew_anomalies\n",
"Anomalies Report Path(Google Cloud Storage): gs://cloud-ai-platform-773884b1-2a32-48d6-8b83-c03cde416b68/model_monitoring/job-8672170640054157312/serving/2022-08-25T00:00/stats_and_anomalies/<deployed-model-id>/anomalies/training_prediction_skew_anomalies\n",
"\n",
"For more information about the alert, please visit the model monitoring alert page.\n",
"\n",
"Deployed model id: 5653675595884658688\n",
"Deployed model id: <deployed-model-id>\n",
"\n",
"Feature name\tAnomaly short description\tAnomaly long description\n",
"country\tHigh Linfty distance between training and serving\tThe Linfty distance between training and serving is 0.947563 (up to six significant digits), above the threshold 0.5. The feature value with maximum difference is: Canada\n",
@@ -1505,9 +1505,9 @@
"source": [
"### Logging sampled requests\n",
"\n",
"Once the monitoring service has started, the sampled prediction requests will be logged to Cloud Storage. On the next monitoring interval, the sampled predictions are then copied over to the BigQuery logging table. Once the entries are in the BigQuery table, the monitoring service will analyze the sampled data.\n",
"On the next monitoring interval, the sampled predictions are then copied over to the BigQuery logging table. Once the entries are in the BigQuery table, the monitoring service will analyze the sampled data.\n",
"\n",
"Next, you wait for the first logged entres to appear in the BigQuery table used for logging prediction samples. Since you sent a 1000 prediction requests, with 50% sampling, you should see around 1000 entries."
"Next, you wait for the first logged entres to appear in the BigQuery table used for logging prediction samples. Since you sent 1000 prediction requests, with 50% sampling, you should see around 1000 entries."
]
},
{
@@ -1546,13 +1546,7 @@
"\n",
"#### Wait for monitoring interval\n",
"\n",
"It can take upwards of 40 minutes from when the analyis occurred on the monitoring interval to when you receive an email alert.\n",
"\n",
"The contents will appear like\n",
"\n",
"<blockquote>\n",
" \n",
"<blockquote>"
"It can take upwards of 40 minutes from when the analyis occurred on the monitoring interval to when you receive an email alert."
]
},
{
@@ -1635,12 +1629,14 @@
},
"outputs": [],
"source": [
"delete_bucket = True\n",
"delete_bucket = False\n",
"\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -rf {BUCKET_URI}\n",
"\n",
"! rm -f schema.yaml"
"! rm -f schema.yaml\n",
"\n",
"! bq rm -f {PROJECT_ID}.model_deployment_monitoring_{ENDPOINT_ID}"
]
}
],
@@ -766,7 +766,7 @@
"source": [
"## Introduction to Vertex AI Model Monitoring\n",
"\n",
"Vertex AI Model Monitoring is supported for AutoML tabular models and custom tabular model. You can monitor for skew and drift detection of the features in the inbound prediction requests or skew and drift detection of the feature attributions (Explainable AI) in the outbound prediction response -- that is, the distribution of the attributions on how they contributed to the output (predictions).\n",
"Vertex AI Model Monitoring is supported for AutoML tabular models and custom tabular models. You can monitor for skew and drift detection of the features in the inbound prediction requests or skew and drift detection of the feature attributions (Explainable AI) in the outbound prediction response -- that is, the distribution of the attributions on how they contributed to the output (predictions).\n",
"\n",
"The following are the basic steps to enable model monitoring:\n",
"\n",
@@ -781,7 +781,7 @@
"\n",
"When model monitoring is enabled, the sampled incoming prediction requests are logged into a BigQuery table. The input feature values contained in the logged requests are then analyzed for skew or drift on an specified interval basis. You set a sampling rate to monitor a subset of the production inputs to a model, and the monitoring interval.\n",
"\n",
"The model monitoring service needs to know how to parse the feature values, which is referred to as the input schema. For AutoML tabular models, the input schema is automatically provided. For custom tabular models, the service will attempt to automatically derive the input schema from the first 1000 prediction requests. Alternatively, one can upload the input schema.\n",
"The model monitoring service needs to know how to parse the feature values, which is referred to as the input schema. For AutoML tabular models, the input schema is automatically generated. For custom tabular models, the service will attempt to automatically derive the input schema from the first 1000 prediction requests. Alternatively, one can upload the input schema.\n",
"\n",
"For skew detection, the monitoring service requires a baseline for the statistical distribution of values in the training data. For AutoML tabular models this is automatically derived. For custom tabular models, you upload the training data to the service, and have the service automatically derive the distribution.\n",
"\n",
@@ -176,7 +176,10 @@
"else:\n",
" USER_FLAG = \"\"\n",
"\n",
"! pip3 install --upgrade google-cloud-bigquery[pandas] google-cloud-aiplatform google-cloud-pipeline-components $USER_FLAG"
"! (pip3 install --upgrade $USER_FLAG \\\n",
" google-cloud-bigquery[pandas]==2.34.4 \\\n",
" google-cloud-aiplatform==1.16.1 \\\n",
" google-cloud-pipeline-components==1.0.18)"
]
},
{
@@ -29,6 +29,8 @@
"id": "JAPoU8Sm5E6e"
},
"source": [
"# Online and Batch predictions using Vertex AI Feature Store\n",
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/feature_store/sdk-feature-store.ipynb\">\n",
@@ -51,19 +53,22 @@
{
"cell_type": "markdown",
"metadata": {
"id": "tvgnzT1CKxrO"
"id": "c4aaea3bab5e"
},
"source": [
"## Overview\n",
"\n",
"This notebook introduces Vertex AI Feature Store, a managed cloud service for machine learning engineers and data scientists to store, serve, manage and share machine learning features at a large scale.\n",
"\n",
"This notebook assumes that you understand basic Google Cloud concepts such as [Project](https://cloud.google.com/storage/docs/projects), [Storage](https://cloud.google.com/storage) and [Vertex AI](https://cloud.google.com/vertex-ai/docs). Some machine learning knowledge is also helpful but not required.\n",
"\n",
"### Dataset\n",
"\n",
"This notebook uses a movie recommendation dataset as an example throughout all the sessions. The task is to train a model to predict if a user is going to watch a movie and serve this model online. \n",
"\n",
"This notebook assumes that you understand basic Google Cloud concepts such as [Project](https://cloud.google.com/storage/docs/projects), [Storage](https://cloud.google.com/storage) and [Vertex AI](https://cloud.google.com/vertex-ai/docs). Some machine learning knowledge is also helpful but not required.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "71779c8088bf"
},
"source": [
"### Objective\n",
"\n",
"In this notebook, you will learn how to use `Vertex AI Feature Store` to import feature data, and to access the feature data for both online serving and offline tasks, such as training.\n",
@@ -79,8 +84,26 @@
"- Create featurestore, entity type, and feature resources.\n",
"- Import feature data into `Vertex AI Feature Store` resource.\n",
"- Serve online prediction requests using the imported features.\n",
"- Access imported features in offline jobs, such as training jobs.\n",
"- Access imported features in offline jobs, such as training jobs."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "55e01a856f57"
},
"source": [
"### Dataset\n",
"\n",
"This notebook uses a movie recommendation dataset as an example throughout all the sessions. The task is to train a model to predict if a user is going to watch a movie and serve this model online."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tvgnzT1CKxrO"
},
"source": [
"### Costs \n",
"\n",
"This tutorial uses billable components of Google Cloud:\n",
@@ -262,7 +285,15 @@
},
"outputs": [],
"source": [
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
"import os\n",
"\n",
"PROJECT_ID = \"\"\n",
"\n",
"# Get your Google Cloud project ID from gcloud\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID: \", PROJECT_ID)"
]
},
{
@@ -275,10 +306,7 @@
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
"print(\"Project ID: \", PROJECT_ID)"
]
},
{
@@ -320,7 +348,9 @@
},
"outputs": [],
"source": [
"REGION = \"us-central1\" # @param {type: \"string\"}"
"REGION = \"[your-region]\" # @param {type:\"string\"}\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -329,9 +359,9 @@
"id": "timestamp"
},
"source": [
"#### Timestamp\n",
"#### UUID\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
@@ -342,9 +372,16 @@
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import random\n",
"import string\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
"\n",
"# Generate a uuid of a specifed length(default=8)\n",
"def generate_uuid(length: int = 8) -> str:\n",
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
"\n",
"\n",
"UUID = generate_uuid()"
]
},
{
@@ -441,7 +478,7 @@
"source": [
"from google.cloud.aiplatform import Feature, Featurestore\n",
"\n",
"FEATURESTORE_ID = \"movie_prediction\"\n",
"FEATURESTORE_ID = \"movie_prediction\" + UUID\n",
"INPUT_CSV_FILE = \"gs://cloud-samples-data-us-central1/vertex-ai/feature-store/datasets/movie_prediction.csv\"\n",
"ONLINE_STORE_FIXED_NODE_COUNT = 1"
]
@@ -29,7 +29,7 @@
"id": "JAPoU8Sm5E6e"
},
"source": [
"# Vertex AI Pipelines: Loan eligibility prediction using google-cloud-pipeline-components and Spark ML\n",
"# Vertex AI Pipelines: Loan eligibility prediction using `google-cloud-pipeline-components` and Spark ML\n",
"\n",
"<table align=\"left\">\n",
"\n",
@@ -206,7 +206,7 @@
" \n",
"!pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform==1.11.0 \\\n",
" kfp==1.8.11 \\\n",
" google-cloud-pipeline-components==1.0.1 --quiet --no-warn-conflicts"
" google-cloud-pipeline-components==1.0.18 --quiet --no-warn-conflicts"
]
},
{
@@ -733,9 +733,7 @@
"from pathlib import Path as path\n",
"from typing import NamedTuple\n",
"\n",
"# Part 1 - ML Training\n",
"from google.cloud import aiplatform as vertex_ai\n",
"from google_cloud_pipeline_components import aiplatform as vertex_ai_components\n",
"from kfp.v2 import compiler, dsl\n",
"from kfp.v2.dsl import (Artifact, ClassificationMetrics, Condition, Input,\n",
" Metrics, Output, component)"
@@ -763,14 +761,14 @@
"PIPELINE_ROOT = f\"{BUCKET_URI}/pipelines\"\n",
"PIPELINE_PACKAGE_PATH = str(BUILD_PATH / f\"pipeline_{UUID}.json\")\n",
"RUNTIME_CONTAINER_IMAGE = f\"gcr.io/{PROJECT_ID}/{RUNTIME_IMAGE}:{IMAGE_TAG}\"\n",
"ML_APPLICATION = \"spark\"\n",
"TASK = \"classifier\"\n",
"ML_APPLICATION = \"loan-eligibility\"\n",
"TASK = \"sparkml\"\n",
"MODEL_TYPE = \"rfor\"\n",
"VERSION = \"1.0.0\"\n",
"MODEL_NAME = f\"{ML_APPLICATION}-{TASK}-{MODEL_TYPE}-{VERSION}\"\n",
"ARTIFACT_URI = f\"{BUCKET_URI}/deliverables/bundle/{UUID}\"\n",
"\n",
"# Preprocessing\n",
"PREPROCESSING_BATCH_ID = f\"data-preprocessing-{UUID}\"\n",
"PREPROCESSING_PYTHON_FILE_URI = f\"{BUCKET_URI}/src/data_preprocessing.py\"\n",
"PROCESSED_DATA_URI = f\"{BUCKET_URI}/data/processed\"\n",
"PREPROCESSING_ARGS = [\n",
@@ -785,7 +783,6 @@
"GCS_PREPROCESSED_URI = f\"{PROCESSED_DATA_URI}/*/?.csv\"\n",
"\n",
"# Training\n",
"TRAINING_BATCH_ID = f\"model-training-{UUID}\"\n",
"TRAINING_PYTHON_FILE_URI = f\"{BUCKET_URI}/src/model_training.py\"\n",
"MODEL_URI = f\"{BUCKET_URI}/deliverables/model/rfor/{UUID}/train_model\"\n",
"METRICS_URI = f\"{BUCKET_URI}/deliverables/metrics/rfor/{UUID}/train_metrics.json\"\n",
@@ -800,10 +797,9 @@
"\n",
"# Condition\n",
"AUPR_THRESHOLD = 0.5\n",
"AUPR_HYPERTUNE_CONDITION = \"[AUPR_HYPERTUNE]\"\n",
"AUPR_HYPERTUNE_CONDITION = \"hypertune\"\n",
"\n",
"# Hypertuning\n",
"HPT_TRAINING_BATCH_ID = f\"hyper-tuning-{UUID}\"\n",
"HPT_PYTHON_FILE_URI = f\"{BUCKET_URI}/src/hp_tuning.py\"\n",
"HPT_MODEL_URI = f\"{BUCKET_URI}/deliverables/model/rfor/{UUID}/model\"\n",
"HPT_METRICS_URI = f\"{BUCKET_URI}/deliverables/metrics/rfor/{UUID}/metrics.json\"\n",
@@ -814,7 +810,24 @@
" HPT_MODEL_URI,\n",
" \"--metrics-path\",\n",
" HPT_METRICS_URI,\n",
"]"
"]\n",
"HPT_BUNDLE_URI = f\"{ARTIFACT_URI}/model.zip\"\n",
"HPT_ARGS = [\n",
" \"--train-path\",\n",
" PROCESSED_DATA_URI,\n",
" \"--model-path\",\n",
" HPT_MODEL_URI,\n",
" \"--metrics-path\",\n",
" HPT_METRICS_URI,\n",
" \"--bundle-path\",\n",
" HPT_BUNDLE_URI,\n",
"]\n",
"HPT_RUNTIME_PROPERTIES = {\n",
" \"spark.jars.packages\": \"ml.combust.mleap:mleap-spark-base_2.12:0.20.0,ml.combust.mleap:mleap-spark_2.12:0.20.0\"\n",
"}\n",
"\n",
"# Deploy\n",
"SERVING_IMAGE_URI = f\"{REGION}-docker.pkg.dev/{PROJECT_ID}/{REPO_NAME}/spark-ml-serving\""
]
},
{
@@ -843,7 +856,7 @@
"id": "LB2aM7VyRyZG"
},
"source": [
"## PART I - Build the Vertex Pipeline to train and deploy a Spark model\n",
"## Build the Vertex Pipeline to train and deploy a Spark model\n",
"\n",
"In this case, the ML pipeline includes the following steps:\n",
"\n",
@@ -851,10 +864,16 @@
"2. Train an `RandomForestClassifier` with `DataprocPySparkBatchOp`\n",
"3. Run a custom component in order to evaluate the model\n",
"\n",
"If the model respects the performance condition, then\n",
"If the model respects the performance condition, then:\n",
"\n",
"4. Hypertune the `RandomForestClassifier` with `DataprocPySparkBatchOp`\n",
"5. Register the model in the Vertex AI Model Registry\n"
"5. Serializes the model to MLeap format to use the model outside of Spark.\n",
"\n",
"If the `deploy_model` pipeline parameter is set to `True`:\n",
"\n",
"6. Upload the model to Vertex AI Model Registry.\n",
"7. Creates a Vertex AI endpoint.\n",
"8. Deploys the model to the Vertex AI endpoint for serving online prediction requests.\n"
]
},
{
@@ -1430,7 +1449,9 @@
"\n",
"- `--train-path`: The GCS path of the training sample.\n",
"- `--model-path`: The GCS path to store the trained model.\n",
"- `--metrics-path`: The GCS path to store the metrics of model."
"- `--metrics-path`: The GCS path to store the metrics of model.\n",
"\n",
"The hyperparameter tuning job will also serialize the best performing model to an MLeap bundle, which can be imported to Vertex AI as a model for serving predictions - see the *Serve your model in Vertex AI* section further below."
]
},
{
@@ -1467,6 +1488,9 @@
"except ImportError as e:\n",
" print('WARN: Something wrong with pyspark library. Please check configuration settings!')\n",
" print(e)\n",
" \n",
"import mleap.pyspark\n",
"from mleap.pyspark.spark_support import SimpleSparkSerializer\n",
"\n",
"from pyspark.sql.types import StructType, DoubleType, StringType\n",
"from pyspark.sql.functions import col, udf\n",
@@ -1572,6 +1596,16 @@
" ''',\n",
" type=str,\n",
" required=True)\n",
" args_parser.add_argument(\n",
" '--bundle-path',\n",
" help='''\n",
" The GCS path to store the exported MLeap bundle. \n",
" Format: \n",
" - locally: /path/to/dir\n",
" - cloud: gs://bucket/path\n",
" ''',\n",
" type=str,\n",
" required=True)\n",
" return args_parser.parse_args()\n",
"\n",
"\n",
@@ -1728,6 +1762,7 @@
" train_path = args.train_path\n",
" model_path = args.model_path\n",
" metrics_path = args.metrics_path\n",
" bundle_path = args.bundle_path\n",
"\n",
" try:\n",
" logger.info('initializing pipeline training.')\n",
@@ -1759,10 +1794,20 @@
" logger.info(f'load model pipeline in {model_path}.')\n",
" pipeline_model.write().overwrite().save(model_path)\n",
"\n",
" logger.info(f'Upload metrics under {metrics_path}.')\n",
" logger.info(f'upload metrics under {metrics_path}.')\n",
" bucket = urlparse(model_path).netloc\n",
" metrics_file_path = urlparse(metrics_path).path.strip('/')\n",
" write_metrics(bucket, metrics, metrics_file_path)\n",
" \n",
" logger.info('export MLeap bundle to temporary location')\n",
" pipeline_model.bestModel.serializeToBundle(f'jar:file:/tmp/bundle.zip', predictions)\n",
" \n",
" logger.info(f'upload MLeap bundle to {bundle_path}')\n",
" bundle_file_path = urlparse(bundle_path).path.strip('/')\n",
" bucket = urlparse(bundle_path).netloc\n",
" logger.info(f'Copying /tmp/bundle.zip to bucket {bucket} using object name {bundle_file_path} ...')\n",
" upload_file(bucket, '/tmp/bundle.zip', bundle_file_path)\n",
" \n",
" except RuntimeError as main_error:\n",
" logger.error(main_error)\n",
" else:\n",
@@ -1807,11 +1852,11 @@
"id": "68nYBB5GS9TB"
},
"source": [
"### Build a custom dataproc serverless image\n",
"### Build a custom Dataproc Serverless container image\n",
"\n",
"The `DataprocPySparkBatchOp` allows you to pass custom image that you use when the [provided Dataproc Serverless runtime versions](https://cloud.google.com/dataproc-serverless/docs/concepts/versions/spark-runtime-versions) does not respect your requirements. \n",
"Dataproc Serverless provides [default runtime images](https://cloud.google.com/dataproc-serverless/docs/concepts/versions/spark-runtime-versions). You can also use custom container images for your Dataproc Serverless workloads. \n",
"\n",
"**Note:** This step is optional and is included here for general awareness."
"The steps in this section builds a custom container image that includes additional dependencies. The custom container image can be specified when using the `DataprocPySparkBatchOp` component to launch the workload within a pipeline."
]
},
{
@@ -1820,7 +1865,7 @@
"id": "GF9_5IGYqLAX"
},
"source": [
"#### Define the Dataproc serverless custom runtime image"
"#### Define the Dataproc Serverless custom runtime image"
]
},
{
@@ -1891,7 +1936,8 @@
" python \\\n",
" scikit-image \\\n",
" scikit-learn \\\n",
" scipy \n",
" scipy \\\n",
" mleap\n",
"\n",
"# (Required) Create the 'spark' group/user.\n",
"# The GID and UID must be 1099. Home directory is required.\n",
@@ -1927,7 +1973,9 @@
"id": "ZXzI2xInqb3V"
},
"source": [
"#### Build the Dataproc serverless custom runtime using Google Cloud Build"
"#### Build the Dataproc Serverless custom runtime using Google Cloud Build\n",
"\n",
"**Note:** this step may take approximately 5 to 10 minutes to complete."
]
},
{
@@ -2104,12 +2152,12 @@
{
"cell_type": "markdown",
"metadata": {
"id": "1-Ccx4uLDz4N"
"id": "28f3d22dd97f"
},
"source": [
"#### Model registration custom component\n",
"#### Create component for passing args to hyperparameter tuning component\n",
"\n",
"Define a component to create a model resource for the trained model on Vertex AI Model registry."
"The following component passes the args `--train-path`, `--model-path` and `--metrics-path`, and `--bundle-path` in the required format for the hyperparamter tuning function defined earlier."
]
},
{
@@ -2120,22 +2168,230 @@
},
"outputs": [],
"source": [
"# TODO: Build a custom compiler using Spark docker image to compile the Mleap bundle\n",
"\n",
"\n",
"@component(base_image=\"python:3.8-slim\")\n",
"def register_model(\n",
" artifact_uri: str,\n",
" model: Output[Artifact],\n",
") -> NamedTuple(\"Outputs\", [(\"uri\", str)]):\n",
"def build_hpt_args(\n",
" dataset_uri: Input[Artifact],\n",
" train_path: str,\n",
" model_path: str,\n",
" metrics_path: str,\n",
" bundle_path: str,\n",
") -> list:\n",
" return [\n",
" \"--train-path\",\n",
" train_path,\n",
" \"--model-path\",\n",
" model_path,\n",
" \"--metrics-path\",\n",
" metrics_path,\n",
" \"--bundle-path\",\n",
" bundle_path,\n",
" ]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0db73fff95b3"
},
"source": [
"### (Optional) Serve your model using Vertex AI\n",
"\n",
" component_outputs = NamedTuple(\n",
" \"Outputs\",\n",
" [\n",
" (\"uri\", str),\n",
" ],\n",
" )\n",
" return component_outputs(artifact_uri)"
"The hyperparameter tuning task exports the best performing model as an MLeap bundle. The MLeap bundle can be imported into the Vertex AI Model Registry and used for prediction serving. See [Serving Spark ML model using Vertex AI](https://cloud.google.com/architecture/spark-ml-model-with-vertexai) for more information.\n",
"\n",
"Enable import of the MLeap bundle into the Vertex AI Model Registry and online prediction serving."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2d7e7c8fc21b"
},
"outputs": [],
"source": [
"# Set DEPLOY_MODEL to True\n",
"DEPLOY_MODEL = False"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0bb792622d9f"
},
"source": [
"### Build the model serving container image\n",
"\n",
"A *serving container image* is required to import your model into the Model Registry. The serving container image provides the model serving implementation for the model. The following replicates the instructions from [Serving Spark ML model using Vertex AI](https://cloud.google.com/architecture/spark-ml-model-with-vertexai) to build the serving container image.\n",
"\n",
"**Note:** this step may take approximately 5 to 10 minutes to complete."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "4703a0f969a3"
},
"outputs": [],
"source": [
"DEPLOY_MODEL_CONDITION = 'deploy'\n",
"\n",
"if DEPLOY_MODEL:\n",
"\n",
" import os\n",
" \n",
" CWD = os.getcwd()\n",
"\n",
" # Clone and build the scala-sbt cloud builder\n",
" ! git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git\n",
" ! cd ${CWD}/cloud-builders-community/scala-sbt && \\\n",
" gcloud builds submit .\n",
"\n",
" # Clone and build the serving container code\n",
" ! cd {CWD} && git clone https://github.com/GoogleCloudPlatform/vertex-ai-spark-ml-serving.git\n",
" ! cd {CWD}/vertex-ai-spark-ml-serving && \\\n",
" gcloud builds submit --config=cloudbuild.yaml \\\n",
" --substitutions=\"_LOCATION={REGION},_REPOSITORY={REPO_NAME},_IMAGE=spark-ml-serving\" ."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "947b51adc087"
},
"source": [
"### Create component for importing a model artifact into a pipeline\n",
"\n",
"The pipeline uses the `ModelImportOp` component to import (upload) a model to Vertex AI Model Registry.\n",
"\n",
"The `import_model_artifact` python component creates a model artifact that can be passed to the `ModelImportOp` component."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2ed96e7ad046"
},
"outputs": [],
"source": [
"@dsl.component(\n",
" base_image=\"python:3.8-slim\",\n",
" packages_to_install=[\"google-cloud-aiplatform\"],\n",
")\n",
"def import_model_artifact(\n",
" model: dsl.Output[dsl.Artifact], artifact_uri: str, serving_image_uri: str\n",
"):\n",
" model.metadata[\"containerSpec\"] = {\n",
" \"imageUri\": serving_image_uri,\n",
" \"healthRoute\": \"/health\",\n",
" \"predictRoute\": \"/predict\",\n",
" }\n",
" model.uri = artifact_uri"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0d83d9e80923"
},
"source": [
"The serving container requires the model schema in JSON format, which is read during container startup. See [Provide the model schema](https://cloud.google.com/architecture/spark-ml-model-with-vertexai#provide_the_model_schema) for more information.\n",
"\n",
"Write the model schema file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "521d2f4d7992"
},
"outputs": [],
"source": [
"%%writefile $SRC/schema.json\n",
"{\n",
" \"input\": [\n",
" {\n",
" \"name\": \"loan_amount\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"loan_term\",\n",
" \"type\": \"STRING\"\n",
" },\n",
" {\n",
" \"name\": \"property_area\",\n",
" \"type\": \"STRING\"\n",
" },\n",
" {\n",
" \"name\": \"feature_7\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_3\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_1\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_9\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_5\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_0\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_8\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_4\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_2\",\n",
" \"type\": \"DOUBLE\"\n",
" },\n",
" {\n",
" \"name\": \"feature_6\",\n",
" \"type\": \"DOUBLE\"\n",
" }\n",
" ],\n",
" \"output\": [\n",
" {\n",
" \"name\": \"prediction\",\n",
" \"type\": \"DOUBLE\"\n",
" }\n",
" ]\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d0b88e26570a"
},
"source": [
"Copy the model schema configuration file to GCS. The serving container reads the model schema file location from the `AIP_STORAGE_URI` environment at startup. See [Import the model into Vertex AI](https://cloud.google.com/architecture/spark-ml-model-with-vertexai#import-the-model-into-vertex-ai) for more information."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "b7763bb558f3"
},
"outputs": [],
"source": [
"! gsutil cp $SRC/schema.json $ARTIFACT_URI/schema.json"
]
},
{
@@ -2159,30 +2415,35 @@
"source": [
"@dsl.pipeline(name=PIPELINE_NAME, description=\"A pipeline to train a PySpark model.\")\n",
"def pipeline(\n",
" preprocessing_batch_id: str = PREPROCESSING_BATCH_ID,\n",
" preprocessing_main_python_file_uri: str = PREPROCESSING_PYTHON_FILE_URI,\n",
" train_data_path: str = FEATURES_TRAIN_URI,\n",
" preprocessed_data_path: str = PROCESSED_DATA_URI,\n",
" dataset_name: str = DATASET_NAME,\n",
" dataset_uri: str = GCS_PREPROCESSED_URI,\n",
" training_batch_id: str = TRAINING_BATCH_ID,\n",
" training_main_python_file_uri: str = TRAINING_PYTHON_FILE_URI,\n",
" train_path: str = PROCESSED_DATA_URI,\n",
" model_path: str = MODEL_URI,\n",
" metrics_path: str = METRICS_URI,\n",
" threshold: float = AUPR_THRESHOLD,\n",
" hpt_batch_id: str = HPT_TRAINING_BATCH_ID,\n",
" hpt_main_python_file_uri: str = HPT_PYTHON_FILE_URI,\n",
" hpt_model_path: str = HPT_MODEL_URI,\n",
" hpt_metrics_path: str = HPT_METRICS_URI,\n",
" hpt_bundle_path: str = HPT_BUNDLE_URI,\n",
" custom_container_image: str = RUNTIME_CONTAINER_IMAGE,\n",
" model_name: str = MODEL_NAME,\n",
" project_id: str = PROJECT_ID,\n",
" location: str = REGION,\n",
" deploy_model: bool = DEPLOY_MODEL,\n",
" artifact_uri: str = ARTIFACT_URI,\n",
" serving_image_uri: str = SERVING_IMAGE_URI,\n",
"):\n",
"\n",
" from google_cloud_pipeline_components.experimental.dataproc import \\\n",
" from google_cloud_pipeline_components.v1.dataproc import \\\n",
" DataprocPySparkBatchOp\n",
" from google_cloud_pipeline_components.v1.dataset import \\\n",
" TabularDatasetCreateOp\n",
" from google_cloud_pipeline_components.v1.endpoint import (EndpointCreateOp,\n",
" ModelDeployOp)\n",
" from google_cloud_pipeline_components.v1.model import ModelUploadOp\n",
"\n",
" # build preprocessed data args\n",
" build_preprocessing_args_op = build_preprocessing_args(\n",
@@ -2194,13 +2455,12 @@
" project=project_id,\n",
" location=location,\n",
" container_image=custom_container_image,\n",
" batch_id=preprocessing_batch_id,\n",
" main_python_file_uri=preprocessing_main_python_file_uri,\n",
" args=build_preprocessing_args_op.output,\n",
" ).after(build_preprocessing_args_op)\n",
"\n",
" # create dataset\n",
" create_dataset_op = vertex_ai_components.TabularDatasetCreateOp(\n",
" create_dataset_op = TabularDatasetCreateOp(\n",
" display_name=dataset_name,\n",
" gcs_source=dataset_uri,\n",
" project=project_id,\n",
@@ -2220,7 +2480,6 @@
" project=project_id,\n",
" location=location,\n",
" container_image=custom_container_image,\n",
" batch_id=training_batch_id,\n",
" main_python_file_uri=training_main_python_file_uri,\n",
" args=build_training_args_op.output,\n",
" ).after(build_training_args_op)\n",
@@ -2233,11 +2492,12 @@
" name=AUPR_HYPERTUNE_CONDITION,\n",
" ):\n",
"\n",
" build_hpt_args_op = build_training_args(\n",
" build_hpt_args_op = build_hpt_args(\n",
" dataset_uri=create_dataset_op.output,\n",
" train_path=train_path,\n",
" model_path=hpt_model_path,\n",
" metrics_path=hpt_metrics_path,\n",
" bundle_path=hpt_bundle_path,\n",
" ).after(evaluate_model_op)\n",
"\n",
" # hyperparameter tuning\n",
@@ -2245,13 +2505,46 @@
" project=project_id,\n",
" location=location,\n",
" container_image=custom_container_image,\n",
" batch_id=hpt_batch_id,\n",
" main_python_file_uri=hpt_main_python_file_uri,\n",
" args=build_hpt_args_op.output,\n",
" runtime_config_properties=HPT_RUNTIME_PROPERTIES,\n",
" ).after(model_traning_op)\n",
"\n",
" # upload model\n",
" register_model(artifact_uri=hpt_model_path).after(hyperparameter_tuning_op)"
" # evaluate condition to upload and deploy model to Vertex AI\n",
" with Condition(\n",
" # kfp casts `bool` parameter to `str`\n",
" deploy_model == \"True\",\n",
" name=DEPLOY_MODEL_CONDITION,\n",
" ):\n",
" # import the model into the pipeline as a kfp model artifact\n",
" import_model_artifact_op = import_model_artifact(\n",
" artifact_uri=artifact_uri,\n",
" serving_image_uri=serving_image_uri,\n",
" )\n",
"\n",
" # upload model to Vertex AI\n",
" model_upload_op = ModelUploadOp(\n",
" project=project_id,\n",
" location=location,\n",
" display_name=model_name,\n",
" unmanaged_container_model=import_model_artifact_op.outputs[\"model\"],\n",
" ).after(hyperparameter_tuning_op)\n",
"\n",
" # create a serving endpoint\n",
" endpoint_op = EndpointCreateOp(\n",
" project=project_id,\n",
" location=location,\n",
" display_name=model_name,\n",
" ).after(model_upload_op)\n",
"\n",
" # deploy model to the serving endpoint\n",
" _ = ModelDeployOp(\n",
" model=model_upload_op.outputs[\"model\"],\n",
" endpoint=endpoint_op.outputs[\"endpoint\"],\n",
" dedicated_resources_machine_type=\"n1-standard-2\",\n",
" dedicated_resources_min_replica_count=1,\n",
" dedicated_resources_max_replica_count=1,\n",
" ).after(endpoint_op)"
]
},
{
@@ -2327,6 +2620,65 @@
"pipeline.wait()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b584afa5a1b1"
},
"source": [
"### (Optional) Get online predictions from the deployed model\n",
"\n",
"You can request online predictions if the model was deployed to a Vertex AI endpoint. Use the `google-cloud-aiplatform` client library to request predictions, or use `curl` as per below:\n",
"\n",
"Create the prediction request payload with the instances that you want to predict:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "12d068e1877c"
},
"outputs": [],
"source": [
"%%writefile instances.json\n",
"{\n",
" \"instances\": [\n",
" [214.0, \"360\", \"Rural\", 2.13, 2.21, 0.0, 0.0, 2.31, 2.01, 0.0, 0.0, 0.0, 0.0],\n",
" [213.0, \"360\", \"Semiurban\", 2.03, 2.11, 0.0, 0.0, 2.13, 2.02, 0.0, 0.0, 0.0, 0.0]\n",
" ]\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b7cbfec4537d"
},
"source": [
"Use `curl` to send the prediction request to the Vertex AI endpoint. The response contains the predicted label (`0 == not eligible`, `1 == eligible`) for each instance sent in the request payload."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "b1617d6e8a3d"
},
"outputs": [],
"source": [
"ENDPOINT_ID=!(gcloud ai endpoints list \\\n",
" --region={REGION} \\\n",
" --filter=display_name={MODEL_NAME} \\\n",
" --format='value(name)')\n",
"\n",
"!curl -X POST \\\n",
" -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n",
" -H \"Content-Type: application/json\" \\\n",
" https://{REGION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/us-central1/endpoints/{ENDPOINT_ID[-1]}:predict \\\n",
" -d \"@instances.json\""
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -2352,8 +2704,14 @@
"# Delete pipeline\n",
"pipeline.delete()\n",
"\n",
"# Delete endpoints\n",
"endpoint_list = vertex_ai.Endpoint.list(filter=f'display_name=\"{MODEL_NAME}\"')\n",
"for endpoint in endpoint_list:\n",
" endpoint.undeploy_all()\n",
" endpoint.delete()\n",
"\n",
"# Delete model\n",
"model_list = vertex_ai.TabularDataset.list(filter=f'display_name=\"{MODEL_NAME}\"')\n",
"model_list = vertex_ai.Model.list(filter=f'display_name=\"{MODEL_NAME}\"')\n",
"for model in model_list:\n",
" model.delete()\n",
"\n",