<jupyter_start><jupyter_text>Training and deploying a tabular model using Vertex custom training job - Part 2![Training pipeline](../images/custom-tabular.png)<jupyter_code>import os
import pprint
import pandas as pd
import tensorflow as tf
import time

from google.cloud import aiplatform as vertex_ai
from google.cloud.aiplatform_v1beta1 import types<jupyter_output><empty_output><jupyter_text>Configure GCP settings<jupyter_code>PREFIX = 'jkwst1'

shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null
PROJECT = shell_output[0]
print("Project ID: ", PROJECT)
REGION = 'us-central1'
STAGING_BUCKET = f'gs://{PREFIX}-bucket'<jupyter_output><empty_output><jupyter_text>Deploying a model to Vertex AI  Initialize Vertex AI SDK<jupyter_code>vertex_ai.init(
    project=PROJECT,
    location=REGION,
    staging_bucket=STAGING_BUCKET
)<jupyter_output><empty_output><jupyter_text>Retrieve pre-trained model artifacts<jupyter_code>pretrained_model_path = 'gs://workshop-datasets/models/taxi_classifier'

!gsutil ls {pretrained_model_path}<jupyter_output>gs://workshop-datasets/models/taxi_classifier/saved_model.pb
gs://workshop-datasets/models/taxi_classifier/variables/<jupyter_text>Copy the pre-trained model artifacts to your staging bucket<jupyter_code>saved_model_path = f'{STAGING_BUCKET}/models/taxi_classifer'

!gsutil cp -r {pretrained_model_path}/* {saved_model_path}<jupyter_output>Copying gs://workshop-datasets/models/taxi_classifier/saved_model.pb...
Copying gs://workshop-datasets/models/taxi_classifier/variables/variables.data-00000-of-00001...
Copying gs://workshop-datasets/models/taxi_classifier/variables/variables.index...
- [3 files][647.1 KiB/647.1 KiB]                                                
Operation completed over 3 objects/647.1 KiB.<jupyter_text>Inspect the model<jupyter_code>!saved_model_cli show --dir {saved_model_path} --tag_set serve --signature_def serving_default<jupyter_output>The given SavedModel SignatureDef contains the following input(s):
  inputs['dropoff_grid'] tensor_info:
      dtype: DT_STRING
      shape: (-1, 1)
      name: serving_default_dropoff_grid:0
  inputs['euclidean'] tensor_info:
      dtype: DT_DOUBLE
      shape: (-1, 1)
      name: serving_default_euclidean:0
  inputs['payment_type'] tensor_info:
      dtype: DT_STRING
      shape: (-1, 1)
      name: serving_default_payment_type:0
  inputs['pickup_grid'] tensor_info:
      dtype: DT_STRING
      shape: (-1, 1)
      name: serving_default_pickup_grid:0
  inputs['trip_day'] tensor_info:
      dtype: DT_INT64
      shape: (-1, 1)
      name: serving_default_trip_day:0
  inputs['trip_day_of_week'] tensor_info:
      dtype: DT_INT64
      shape: (-1, 1)
      name: serving_default_trip_day_of_week:0
  inputs['trip_hour'] tensor_info:
      dtype: DT_INT64
      shape: (-1, 1)
      name: serving_default_trip_hour:0
  inputs['trip_miles'] tensor_info:
      dtype: DT_DOUBLE
      shape: (-1[...]<jupyter_text>Upload the model<jupyter_code>display_name = f'{PREFIX} Chicago taxi classifier'
description = 'Chicago taxi tip TensorFlow classifier'
serving_image_uri = 'us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-4:latest'

model = vertex_ai.Model.upload(
    display_name=display_name,
    description=description,
    artifact_uri=saved_model_path,
    serving_container_image_uri=serving_image_uri
)

model.wait()<jupyter_output>INFO:google.cloud.aiplatform.models:Creating Model
INFO:google.cloud.aiplatform.models:Create Model backing LRO: projects/630263135640/locations/us-central1/models/1811722483691159552/operations/5339406860241862656
INFO:google.cloud.aiplatform.models:Model created. Resource name: projects/630263135640/locations/us-central1/models/1811722483691159552
INFO:google.cloud.aiplatform.models:To use this Model in another session:
INFO:google.cloud.aiplatform.models:model = aiplatform.Model('projects/630263135640/locations/us-central1/models/1811722483691159552')<jupyter_text>Create an endpoint<jupyter_code>display_name = f'{PREFIX} Chicago taxi endpoint'

endpoint = vertex_ai.Endpoint.create(display_name=display_name)<jupyter_output>INFO:google.cloud.aiplatform.models:Creating Endpoint
INFO:google.cloud.aiplatform.models:Create Endpoint backing LRO: projects/630263135640/locations/us-central1/endpoints/3418003418755629056/operations/7032760320133169152
INFO:google.cloud.aiplatform.models:Endpoint created. Resource name: projects/630263135640/locations/us-central1/endpoints/3418003418755629056
INFO:google.cloud.aiplatform.models:To use this Endpoint in another session:
INFO:google.cloud.aiplatform.models:endpoint = aiplatform.Endpoint('projects/630263135640/locations/us-central1/endpoints/3418003418755629056')<jupyter_text>Deploy the Model<jupyter_code>deployed_model_display_name = f'{PREFIX}-taxi-v1'
traffic_percentage = 100
machine_type = 'n1-standard-4'

endpoint = model.deploy(
        endpoint=endpoint,
        deployed_model_display_name=deployed_model_display_name,
        machine_type=machine_type,
        traffic_percentage=traffic_percentage)<jupyter_output>INFO:google.cloud.aiplatform.models:Deploying model to Endpoint : projects/630263135640/locations/us-central1/endpoints/3418003418755629056
INFO:google.cloud.aiplatform.models:Deploy Endpoint model backing LRO: projects/630263135640/locations/us-central1/endpoints/3418003418755629056/operations/6280659182362296320
INFO:google.cloud.aiplatform.models:Endpoint model deployed. Resource name: projects/630263135640/locations/us-central1/endpoints/3418003418755629056<jupyter_text>Invoking the deployed model using Vertex SDK  Get the endpoint<jupyter_code>filter = f'display_name="{PREFIX} Chicago taxi endpoint"'

for endpoint_info in vertex_ai.Endpoint.list(filter=filter):
    print(endpoint_info)
    
endpoint = vertex_ai.Endpoint(endpoint_info.resource_name)<jupyter_output><google.cloud.aiplatform.models.Endpoint object at 0x7f374c0b8910> 
resource name: projects/630263135640/locations/us-central1/endpoints/3418003418755629056<jupyter_text>Call the endpoint<jupyter_code>test_instances = [  
    
    {
        "dropoff_grid": ["POINT(-87.6 41.9)"],
        "euclidean": [2064.2696],
        "payment_type": ["Credit Card"],
        "pickup_grid": ["POINT(-87.6 41.9)"],
        "trip_miles": [1.37],
        "trip_day": [12],
        "trip_hour": [16],
        "trip_month": [2],
        "trip_day_of_week": [4],
        "trip_seconds": [555]
    }
]

predictions = endpoint.predict(instances=test_instances)
prob = tf.nn.sigmoid(predictions[0])
print('Probability of tip > 20%:', prob.numpy())<jupyter_output>Probability of tip > 20%: [[0.7745298]]
{'fraction_non_alphanumeric': 0.10747663551401869, 'fraction_numerical': 0.0753693096171239, 'mean_word_length': 5.504901960784314, 'pattern_counts': {'":': 10, '<': 38, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '101056', 'license': 'Apache-2.0', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2245, 'n_words': 352, 'path': '02-custom-job-tabular/02-model-deploy.ipynb', 'repo_name': 'jasmeetsb/vertex-ai-workshop', 'size': '6634'}