mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
24 KiB
24 KiB
In [ ]:
# Copyright 2022 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-aiplatform
! pip3 install tensorflow-hubIn [ ]:
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 [ ]:
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
LOCATION = "us-central1" # @param {type:"string"}In [ ]:
BUCKET_URI = f"gs://your-bucket-name-{PROJECT_ID}-unique" # @param {type:"string"}In [ ]:
! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}In [ ]:
from google.cloud import aiplatform
aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)In [ ]:
DEPLOY_GPU, DEPLOY_NGPU = (None, None)In [ ]:
TF = "2-5"
if DEPLOY_GPU:
DEPLOY_VERSION = "tf2-gpu.{}".format(TF)
else:
DEPLOY_VERSION = "tf2-cpu.{}".format(TF)
DEPLOY_IMAGE = "{}-docker.pkg.dev/vertex-ai/prediction/{}:latest".format(
LOCATION.split("-")[0], DEPLOY_VERSION
)
print("Deployment:", DEPLOY_IMAGE, DEPLOY_GPU, DEPLOY_NGPU)In [ ]:
MACHINE_TYPE = "n1-standard"
VCPU = "4"
DEPLOY_COMPUTE = MACHINE_TYPE + "-" + VCPU
print("Train machine type", DEPLOY_COMPUTE)In [ ]:
MODEL_DIR = BUCKET_URI + "/model"
! gsutil cp -r gs://cloud-samples-data/vertex-ai/google-cloud-aiplatform-ci-artifacts/models/penguins/estimator/ {MODEL_DIR}In [ ]:
model = aiplatform.Model.upload(
display_name="example",
artifact_uri=MODEL_DIR,
serving_container_image_uri=DEPLOY_IMAGE,
)
print(model)In [ ]:
endpoint = aiplatform.Endpoint.create(
display_name="example",
project=PROJECT_ID,
location=LOCATION,
labels={"your_key": "your_value"},
)
print(endpoint)In [ ]:
response = endpoint.deploy(
model=model,
deployed_model_display_name="example",
machine_type=DEPLOY_COMPUTE,
)
print(endpoint)In [ ]:
import json
from google.api import httpbody_pb2
from google.cloud import aiplatform_v1
DATA = {
"signature_name": "predict",
"instances": [
{
"island": "DREAM",
"culmen_length_mm": 36.6,
"culmen_depth_mm": 18.4,
"flipper_length_mm": 184.0,
"body_mass_g": 3475.0,
"sex": "FEMALE",
}
],
}
http_body = httpbody_pb2.HttpBody(
data=json.dumps(DATA).encode("utf-8"),
content_type="application/json",
)
req = aiplatform_v1.RawPredictRequest(
http_body=http_body, endpoint=endpoint.resource_name
)In [ ]:
API_ENDPOINT = "{}-aiplatform.googleapis.com".format(LOCATION)
client_options = {"api_endpoint": API_ENDPOINT}
pred_client = aiplatform.gapic.PredictionServiceClient(client_options=client_options)
response = pred_client.raw_predict(req)
print(response)In [ ]:
delete_bucket = False
delete_model = True
delete_endpoint = True
if delete_endpoint:
try:
endpoint.undeploy_all()
endpoint.delete()
except Exception as e:
print(e)
if delete_model:
try:
model.delete()
except Exception as e:
print(e)
if delete_bucket:
! gsutil rm -rf {BUCKET_URI}