Skip to content

Commit 1614bc9

Browse files
committed
fix versions
1 parent 2d58802 commit 1614bc9

3 files changed

Lines changed: 634 additions & 35 deletions

File tree

pipeline.yaml

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -270,30 +270,46 @@ deploymentSpec:
270270
\ *\n\ndef deploy_blessed_model_to_fastapi(\n project_id: str,\n location:\
271271
\ str,\n model_name: str,\n service_name: str,\n service_endpoint:\
272272
\ Output[Artifact]\n):\n from google.cloud import aiplatform, aiplatform_v1,\
273-
\ run_v2, storage\n import joblib\n import tempfile\n import os\n\
274-
\ import requests\n import time\n\n print(f\"Starting FastAPI deployment\
275-
\ for blessed model: {model_name}\")\n print(f\"Service name: {service_name}\"\
276-
)\n\n # 1. Initialize Vertex AI and find blessed model\n aiplatform.init(project=project_id,\
277-
\ location=location)\n\n client = aiplatform_v1.ModelServiceClient(\n\
278-
\ 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\"\
273+
\ run_v2, storage\n from google.auth import default\n import joblib\n\
274+
\ import tempfile\n import os\n import requests\n import time\n\
275+
\n print(f\"Starting FastAPI deployment for blessed model: {model_name}\"\
276+
)\n print(f\"Service name: {service_name}\")\n\n # 1. Initialize Vertex\
277+
\ AI and get credentials\n aiplatform.init(project=project_id, location=location)\n\
278+
\n # Get default credentials\n credentials, _ = default()\n print(credentials)\n\
279+
\n # Create client with explicit credentials\n client = aiplatform_v1.ModelServiceClient(\n\
280+
\ credentials=credentials,\n client_options={\"api_endpoint\"\
281+
: f\"{location}-aiplatform.googleapis.com\"}\n )\n\n print(f\"Searching\
282+
\ for blessed model with name: {model_name}\")\n\n # Use the high-level\
283+
\ aiplatform library to list all model versions\n # models = aiplatform.Model.list(filter=f\"\
284+
display_name={model_name}\")\n # blessed_model = None\n\n request\
285+
\ = {\n \"parent\": f\"projects/{project_id}/locations/{location}\"\
286+
,\n \"filter\": f\"display_name={model_name}\"\n }\n\n\
287+
\ models = list(client.list_models(request=request))\n blessed_model\
288+
\ = None\n\n print(f\"Found {len(models)} model versions with name {model_name}\"\
289+
)\n\n # Search through all model versions (each item in models is already\
290+
\ a version)\n for parent_model in models:\n print(f\"Checking\
291+
\ parent model: {parent_model.name}\")\n\n # List all versions of\
292+
\ this model\n versions_request = {\"name\": parent_model.name}\n\
293+
\ versions = list(client.list_model_versions(request=versions_request))\n\
294+
\n print(f\"Found {len(versions)} versions for this model\")\n\n\
295+
\ for version in versions:\n print(f\"Version {version.version_id}:\
296+
\ Aliases = {list(version.version_aliases)}\")\n if \"blessed\"\
297+
\ in version.version_aliases:\n blessed_model = version\n\
298+
\ print(f\"Found blessed version: {version.version_id}\"\
299+
)\n break\n\n if blessed_model:\n break\n\
300+
\n if not blessed_model:\n available_versions = [(m.resource_name,\
301+
\ m.version_id, list(m.version_aliases)) for m in models]\n raise\
302+
\ ValueError(f\"No blessed version found for model {model_name}. Available\
303+
\ versions: {available_versions}\")\n\n print(f\"Found blessed model:\
304+
\ {blessed_model.name}\")\n print(f\"Model URI: {blessed_model.artifact_uri}\"\
305+
)\n\n # 2. Download joblib model from blessed version\n gcs_uri =\
306+
\ blessed_model.artifact_uri\n if not gcs_uri.startswith('gs://'):\n\
307+
\ raise ValueError(f\"Expected GCS URI, got: {gcs_uri}\")\n\n \
308+
\ bucket_name = gcs_uri.replace('gs://', '').split('/')[0]\n model_path\
309+
\ = '/'.join(gcs_uri.replace('gs://', '').split('/')[1:])\n\n print(f\"\
310+
Downloading model from gs://{bucket_name}/{model_path}\")\n\n storage_client\
311+
\ = storage.Client()\n bucket = storage_client.bucket(bucket_name)\n\n\
312+
\ # Download and validate the model\n model_blob_path = f\"{model_path}/model.joblib\"\
297313
\n blob = bucket.blob(model_blob_path)\n\n if not blob.exists():\n\
298314
\ raise ValueError(f\"Model file not found at gs://{bucket_name}/{model_blob_path}\"\
299315
)\n\n with tempfile.NamedTemporaryFile(suffix='.joblib', delete=False)\

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

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"google-cloud-run>=0.10.0",
99
"google-cloud-storage>=2.10.0",
1010
"requests>=2.31.0",
11-
"joblib>=1.4.2"
11+
"joblib>=1.4.2",
12+
"scikit-learn>=1.3.0",
13+
"pandas>=2.0.0",
14+
"numpy>=1.24.0"
1215
]
1316
)
1417
def deploy_blessed_model_to_fastapi(
@@ -19,6 +22,7 @@ def deploy_blessed_model_to_fastapi(
1922
service_endpoint: Output[Artifact]
2023
):
2124
from google.cloud import aiplatform, aiplatform_v1, run_v2, storage
25+
from google.auth import default
2226
import joblib
2327
import tempfile
2428
import os
@@ -28,30 +32,58 @@ def deploy_blessed_model_to_fastapi(
2832
print(f"Starting FastAPI deployment for blessed model: {model_name}")
2933
print(f"Service name: {service_name}")
3034

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

38+
# Get default credentials
39+
credentials, _ = default()
40+
print(credentials)
41+
42+
# Create client with explicit credentials
3443
client = aiplatform_v1.ModelServiceClient(
44+
credentials=credentials,
3545
client_options={"api_endpoint": f"{location}-aiplatform.googleapis.com"}
3646
)
47+
48+
print(f"Searching for blessed model with name: {model_name}")
49+
50+
# Use the high-level aiplatform library to list all model versions
51+
# models = aiplatform.Model.list(filter=f"display_name={model_name}")
52+
# blessed_model = None
53+
3754
request = {
38-
"parent": f"projects/{project_id}/locations/{location}",
39-
"filter": f"display_name={model_name}"
40-
}
55+
"parent": f"projects/{project_id}/locations/{location}",
56+
"filter": f"display_name={model_name}"
57+
}
4158

4259
models = list(client.list_models(request=request))
4360
blessed_model = None
4461

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

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
64+
# Search through all model versions (each item in models is already a version)
65+
for parent_model in models:
66+
print(f"Checking parent model: {parent_model.name}")
67+
68+
# List all versions of this model
69+
versions_request = {"name": parent_model.name}
70+
versions = list(client.list_model_versions(request=versions_request))
71+
72+
print(f"Found {len(versions)} versions for this model")
73+
74+
for version in versions:
75+
print(f"Version {version.version_id}: Aliases = {list(version.version_aliases)}")
76+
if "blessed" in version.version_aliases:
77+
blessed_model = version
78+
print(f"Found blessed version: {version.version_id}")
79+
break
80+
81+
if blessed_model:
5182
break
5283

5384
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]}")
85+
available_versions = [(m.resource_name, m.version_id, list(m.version_aliases)) for m in models]
86+
raise ValueError(f"No blessed version found for model {model_name}. Available versions: {available_versions}")
5587

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

0 commit comments

Comments
 (0)