mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
* Migrate gsutil usage to gcloud storage
* changes for 4301
* linter changes
* Revert "linter changes"
This reverts commit 6665133b2c.
* Apply automated linter fixes
* Update training-multi-class-classification-model-for-ads-targeting-usecase.ipynb
* Update training-multi-class-classification-model-for-ads-targeting-usecase.ipynb
* removed model_garden folder changes
* Update model_garden_mediapipe_object_detection.ipynb
---------
Co-authored-by: gurusai-voleti <gvoleti@google.com>
29 KiB
29 KiB
In [ ]:
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.In [ ]:
! pip3 install --upgrade --quiet google-cloud-aiplatformIn [ ]:
import sys
if "google.colab" in sys.modules:
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)In [ ]:
import sys
if "google.colab" in sys.modules:
from google.colab import auth
auth.authenticate_user()In [ ]:
# Set project id to be the current project ID.
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
LOCATION = "us-central1" # @param {type:"string"}In [ ]:
from google.cloud import aiplatform
aiplatform.init(project=PROJECT_ID, location=LOCATION)In [ ]:
# Create GCS Bucket
BUCKET_URI = "gs://your-bucket-name-unique" # @param {type:"string"}
! gcloud storage buckets create --location={LOCATION} --project={PROJECT_ID} {BUCKET_URI}In [ ]:
endpoint = aiplatform.Endpoint.create(
display_name="test-dedicated-endpoint",
dedicated_endpoint_enabled=True,
)In [ ]:
! pip freeze | grep google-cloud-aiplatformIn [ ]:
# List all your models
for my_model in aiplatform.Model.list():
print(my_model.display_name)
print(my_model.gca_resource.name)
# If you want to use an existing model, use the resource id
# model = aiplatform.Model('projects/12345/locations/us-central1/models/456789')In [ ]:
# @title Upload a new tensorflow model
# @markdown You can skip this if use an existing model.
# TF Model
DISPLAY_NAME = "tensorflow model" # @param {type:"string"}
ARTIFACT_URI = BUCKET_URI + "/tensorflow"
IMAGE_URI = "us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-12:latest"
# Copy Test Models to the Bucket
! gcloud storage cp --recursive "gs://cloud-samples-data/vertex-ai/prediction/test-models-requests/tensorflow/*" {ARTIFACT_URI}
model = aiplatform.Model.upload(
display_name=DISPLAY_NAME,
artifact_uri=ARTIFACT_URI,
serving_container_image_uri=IMAGE_URI,
)In [ ]:
endpoint.deploy(model=model, traffic_percentage=100, machine_type="e2-standard-8")In [ ]:
# @title Predict
# @markdown You can use client library.
use_python_sdk = True # @param {type:"boolean"}
# @markdown response = my_endpoint.predict( \
# @markdown   instances=[{"feat_1":val_1, "feat_2":val_2}]}, \
# @markdown   headers = {'Content-Type':'application/json'}, \
# @markdown   dedicated_endpoint=True, \
# @markdown )
if use_python_sdk:
instances = [
{"dense_input": [14.0, 7.0, 2545.461893666405, 54.2, 48.5, 0.0, 61.3, 0.0, 0.0, 0.0]},
{"dense_input": [28.0, 14.0, 1234, 27.1, 90, 0.0, 61.3, 0.0, 0.0, 0.0]}
]
response = endpoint.predict(instances=instances, use_dedicated_endpoint=True)
print(response)
# @markdown You can also make HTTP/GRPC request directly.
use_http = True # @param {type:"boolean"}
# @markdown POST request to the following URL:\
# @markdown `https://ENDPOINT_ID.us-central1-PROJECT_NUMBER.prediction.vertexai.goog/v1/projects/PROJECT_NUMBER/locations/LOCATION/endpoints/ENDPOINT_ID:predict`
if use_http:
request_path = f"https://{endpoint.gca_resource.dedicated_endpoint_dns}/v1/{endpoint.resource_name}:predict"
! curl {request_path} -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" -d \
'{{ \
"instances": [ \
{{"dense_input": [14.0, 7.0, 2545.461893666405, 54.2, 48.5, 0.0, 61.3, 0.0, 0.0, 0.0]}}, \
{{"dense_input": [28.0, 14.0, 1234, 27.1, 90, 0.0, 61.3, 0.0, 0.0, 0.0]}} \
] \
}}'In [ ]:
# @title Raw Predict
# @markdown You can use client library.
use_python_sdk = True # @param {type:"boolean"}
# @markdown response = my_endpoint.raw_predict( \
# @markdown    body = b'{"instances":[{"feat_1":val_1, "feat_2":val_2}]}', \
# @markdown    headers = {'Content-Type':'application/json'}, \
# @markdown    dedicated_endpoint=True, \
# @markdown ) \
# @markdown status_code = response.status_code \
# @markdown results = json.dumps(response.text)
if use_python_sdk:
body = b'{ \
"instances": [ \
{"dense_input": [14.0, 7.0, 2545.461893666405, 54.2, 48.5, 0.0, 61.3, 0.0, 0.0, 0.0]}, \
{"dense_input": [28.0, 14.0, 1234, 27.1, 90, 0.0, 61.3, 0.0, 0.0, 0.0]} \
]\
}'
import os
token = !gcloud auth print-access-token
token = token[0]
headers = {"content-type": "application/json", "Authorization": f"Bearer {token}"}
response = endpoint.raw_predict(body=body, headers=headers, use_dedicated_endpoint=True)
# print(response.status_code)
print(response.text)
# @markdown You can also make HTTP/GRPC request directly.
use_http = True # @param {type:"boolean"}
# @markdown POST request to the following URL:\
# @markdown `https://ENDPOINT_ID.us-central1-PROJECT_NUMBER.prediction.vertexai.goog/v1/projects/PROJECT_NUMBER/locations/LOCATION/endpoints/ENDPOINT_ID:rawPredict`
if use_http:
request_path = f"https://{endpoint.gca_resource.dedicated_endpoint_dns}/v1/{endpoint.resource_name}:rawPredict"
! curl {request_path} -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" -d \
'{{ \
"instances": [ \
{{"dense_input": [14.0, 7.0, 2545.461893666405, 54.2, 48.5, 0.0, 61.3, 0.0, 0.0, 0.0]}}, \
{{"dense_input": [28.0, 14.0, 1234, 27.1, 90, 0.0, 61.3, 0.0, 0.0, 0.0]}} \
] \
}}'In [ ]:
# @title Stream Raw Predict
# @markdown You can use client library.
use_python_sdk = True # @param {type:"boolean"}
# @markdown for stream_response in my_endpoint.stream_raw_predict( \
# @markdown    body = b'{"instances":[{"feat_1":val_1, "feat_2":val_2}]}', \
# @markdown    headers = {'Content-Type':'application/json'}, \
# @markdown    use_dedicated_endpoint=True, \
# @markdown ): \
# @markdown    status_code = response.status_code \
# @markdown    stream_result = json.dumps(response.text)
if use_python_sdk:
body = b'{ \
"instances": [ \
{"dense_input": [14.0, 7.0, 2545.461893666405, 54.2, 48.5, 0.0, 61.3, 0.0, 0.0, 0.0]}, \
{"dense_input": [28.0, 14.0, 1234, 27.1, 90, 0.0, 61.3, 0.0, 0.0, 0.0]} \
]\
}'
import os
token = !gcloud auth print-access-token
token = token[0]
headers = {"content-type": "application/json", "Authorization": f"Bearer {token}"}
for stream_response in endpoint.raw_predict(body=body, headers=headers, use_dedicated_endpoint=True):
# print(response.status_code)
print(response.text)
# @markdown You can also make HTTP/GRPC request directly.
use_http = True # @param {type:"boolean"}
# @markdown POST request to the following URL:\
# @markdown `https://ENDPOINT_ID.us-central1-PROJECT_NUMBER.prediction.vertexai.goog/v1/projects/PROJECT_NUMBER/locations/LOCATION/endpoints/ENDPOINT_ID:streamRawPredict`
if use_http:
request_path = f"https://{endpoint.gca_resource.dedicated_endpoint_dns}/v1/{endpoint.resource_name}:streamRawPredict"
! curl {request_path} -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" -d \
'{{ \
"instances": [ \
{{"dense_input": [14.0, 7.0, 2545.461893666405, 54.2, 48.5, 0.0, 61.3, 0.0, 0.0, 0.0]}}, \
{{"dense_input": [28.0, 14.0, 1234, 27.1, 90, 0.0, 61.3, 0.0, 0.0, 0.0]}} \
] \
}}'In [ ]:
endpoint.deploy(model=model, traffic_percentage=50, machine_type="e2-standard-8")In [ ]:
instances = [
{
"dense_input": [
14.0,
7.0,
2545.461893666405,
54.2,
48.5,
0.0,
61.3,
0.0,
0.0,
0.0,
]
},
{"dense_input": [28.0, 14.0, 1234, 27.1, 90, 0.0, 61.3, 0.0, 0.0, 0.0]},
]
counter = {}
for i in range(1000):
response = endpoint.predict(instances=instances, use_dedicated_endpoint=True)
if response.deployed_model_id in counter.keys():
counter[response.deployed_model_id] += 1
else:
counter[response.deployed_model_id] = 1
print(counter)In [ ]:
deployed_model_id_0 = list(counter)[0]
deployed_model_id_1 = list(counter)[1]
endpoint.update(traffic_split={deployed_model_id_0: 20, deployed_model_id_1: 80})In [ ]:
endpoint.undeploy_all()
endpoint.delete()
model.delete()In [ ]:
! gcloud storage rm --recursive {BUCKET_URI}In [ ]:
# for e in aiplatform.Endpoint.list():
# e.undeploy_all()
# e.delete()