Skip to content

Commit 3ba2f8f

Browse files
committed
fix versions
1 parent 2d58802 commit 3ba2f8f

3 files changed

Lines changed: 528 additions & 30 deletions

File tree

pipeline.yaml

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -276,24 +276,34 @@ deploymentSpec:
276276
)\n\n # 1. Initialize Vertex AI and find blessed model\n aiplatform.init(project=project_id,\
277277
\ location=location)\n\n client = aiplatform_v1.ModelServiceClient(\n\
278278
\ client_options={\"api_endpoint\": f\"{location}-aiplatform.googleapis.com\"\
279-
}\n )\n request = {\n \"parent\": f\"projects/{project_id}/locations/{location}\"\
280-
,\n \"filter\": f\"display_name={model_name}\"\n }\n\n models\
281-
\ = list(client.list_models(request=request))\n blessed_model = None\n\
282-
\n print(f\"Found {len(models)} models with name {model_name}\")\n\n\
283-
\ for model in models:\n print(f\"Model: {model.name}, Aliases:\
284-
\ {list(model.version_aliases)}\")\n if \"blessed\" in model.version_aliases:\n\
285-
\ blessed_model = model\n break\n\n if not blessed_model:\n\
286-
\ raise ValueError(f\"No blessed version found for model {model_name}.\
287-
\ Available models: {[(m.name, list(m.version_aliases)) for m in models]}\"\
288-
)\n\n print(f\"Found blessed model: {blessed_model.name}\")\n print(f\"\
289-
Model URI: {blessed_model.artifact_uri}\")\n\n # 2. Download joblib model\
290-
\ from blessed version\n gcs_uri = blessed_model.artifact_uri\n if\
291-
\ not gcs_uri.startswith('gs://'):\n raise ValueError(f\"Expected\
292-
\ GCS URI, got: {gcs_uri}\")\n\n bucket_name = gcs_uri.replace('gs://',\
293-
\ '').split('/')[0]\n model_path = '/'.join(gcs_uri.replace('gs://',\
294-
\ '').split('/')[1:])\n\n print(f\"Downloading model from gs://{bucket_name}/{model_path}\"\
295-
)\n\n storage_client = storage.Client()\n bucket = storage_client.bucket(bucket_name)\n\
296-
\n # Download and validate the model\n model_blob_path = f\"{model_path}/model.joblib\"\
279+
}\n )\n\n print(f\"Searching for blessed model with name: {model_name}\"\
280+
)\n\n # Use the high-level aiplatform library to list all model versions\n\
281+
\ models = aiplatform.Model.list(filter=f\"display_name={model_name}\"\
282+
)\n blessed_model = None\n\n print(f\"Found {len(models)} model versions\
283+
\ with name {model_name}\")\n\n # Search through all model versions (each\
284+
\ item in models is already a version)\n for parent_model in models:\n\
285+
\ print(f\"Checking parent model: projects/57434141298/locations/{location}/models/{parent_model.name}\"\
286+
)\n\n # List all versions of this model\n versions_request\
287+
\ = {\"name\": parent_model.name}\n versions = list(client.list_model_versions(request=versions_request))\n\
288+
\n print(f\"Found {len(versions)} versions for this model\")\n\n\
289+
\ for version in versions:\n print(f\"Version {version.version_id}:\
290+
\ Aliases = {list(version.version_aliases)}\")\n if \"blessed\"\
291+
\ in version.version_aliases:\n blessed_model = version\n\
292+
\ print(f\"Found blessed version: {version.version_id}\"\
293+
)\n break\n\n if blessed_model:\n break\n\
294+
\n if not blessed_model:\n available_versions = [(m.resource_name,\
295+
\ m.version_id, list(m.version_aliases)) for m in models]\n raise\
296+
\ ValueError(f\"No blessed version found for model {model_name}. Available\
297+
\ versions: {available_versions}\")\n\n print(f\"Found blessed model:\
298+
\ {blessed_model.name}\")\n print(f\"Model URI: {blessed_model.artifact_uri}\"\
299+
)\n\n # 2. Download joblib model from blessed version\n gcs_uri =\
300+
\ blessed_model.artifact_uri\n if not gcs_uri.startswith('gs://'):\n\
301+
\ raise ValueError(f\"Expected GCS URI, got: {gcs_uri}\")\n\n \
302+
\ bucket_name = gcs_uri.replace('gs://', '').split('/')[0]\n model_path\
303+
\ = '/'.join(gcs_uri.replace('gs://', '').split('/')[1:])\n\n print(f\"\
304+
Downloading model from gs://{bucket_name}/{model_path}\")\n\n storage_client\
305+
\ = storage.Client()\n bucket = storage_client.bucket(bucket_name)\n\n\
306+
\ # Download and validate the model\n model_blob_path = f\"{model_path}/model.joblib\"\
297307
\n blob = bucket.blob(model_blob_path)\n\n if not blob.exists():\n\
298308
\ raise ValueError(f\"Model file not found at gs://{bucket_name}/{model_blob_path}\"\
299309
)\n\n with tempfile.NamedTemporaryFile(suffix='.joblib', delete=False)\

src/ml_pipelines_kfp/iris_xgboost/pipelines/components/deploy.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def deploy_blessed_model_to_fastapi(
1919
service_endpoint: Output[Artifact]
2020
):
2121
from google.cloud import aiplatform, aiplatform_v1, run_v2, storage
22+
from google.auth import default
2223
import joblib
2324
import tempfile
2425
import os
@@ -28,30 +29,49 @@ def deploy_blessed_model_to_fastapi(
2829
print(f"Starting FastAPI deployment for blessed model: {model_name}")
2930
print(f"Service name: {service_name}")
3031

31-
# 1. Initialize Vertex AI and find blessed model
32+
# 1. Initialize Vertex AI and get credentials
3233
aiplatform.init(project=project_id, location=location)
3334

35+
# Get default credentials
36+
credentials, _ = default()
37+
38+
# Create client with explicit credentials
3439
client = aiplatform_v1.ModelServiceClient(
40+
credentials=credentials,
3541
client_options={"api_endpoint": f"{location}-aiplatform.googleapis.com"}
3642
)
37-
request = {
38-
"parent": f"projects/{project_id}/locations/{location}",
39-
"filter": f"display_name={model_name}"
40-
}
4143

42-
models = list(client.list_models(request=request))
44+
print(f"Searching for blessed model with name: {model_name}")
45+
46+
# Use the high-level aiplatform library to list all model versions
47+
models = aiplatform.Model.list(filter=f"display_name={model_name}")
4348
blessed_model = None
4449

45-
print(f"Found {len(models)} models with name {model_name}")
50+
print(f"Found {len(models)} model versions with name {model_name}")
4651

47-
for model in models:
48-
print(f"Model: {model.name}, Aliases: {list(model.version_aliases)}")
49-
if "blessed" in model.version_aliases:
50-
blessed_model = model
52+
# Search through all model versions (each item in models is already a version)
53+
for parent_model in models:
54+
print(f"Checking parent model: projects/57434141298/locations/{location}/models/{parent_model.name}")
55+
56+
# List all versions of this model
57+
versions_request = {"name": parent_model.name}
58+
versions = list(client.list_model_versions(request=versions_request))
59+
60+
print(f"Found {len(versions)} versions for this model")
61+
62+
for version in versions:
63+
print(f"Version {version.version_id}: Aliases = {list(version.version_aliases)}")
64+
if "blessed" in version.version_aliases:
65+
blessed_model = version
66+
print(f"Found blessed version: {version.version_id}")
67+
break
68+
69+
if blessed_model:
5170
break
5271

5372
if not blessed_model:
54-
raise ValueError(f"No blessed version found for model {model_name}. Available models: {[(m.name, list(m.version_aliases)) for m in models]}")
73+
available_versions = [(m.resource_name, m.version_id, list(m.version_aliases)) for m in models]
74+
raise ValueError(f"No blessed version found for model {model_name}. Available versions: {available_versions}")
5575

5676
print(f"Found blessed model: {blessed_model.name}")
5777
print(f"Model URI: {blessed_model.artifact_uri}")

0 commit comments

Comments
 (0)