@@ -270,30 +270,46 @@ deploymentSpec:
270270 \ *\n\n def 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)\
0 commit comments