diff --git a/community-content/vertex_pipeline_kfpv2/README.md b/community-content/vertex_pipeline_kfpv2/README.md new file mode 100644 index 000000000..d1468084e --- /dev/null +++ b/community-content/vertex_pipeline_kfpv2/README.md @@ -0,0 +1,29 @@ +# Kubeflow pipeline on Vertex + +## Overview +Theses examples show the codes of KFP v2 with aiplatform SDK +* create endpoint +* train model +* deploy model + +## Requirements +* Python 3.8 +* Vertex on Google Cloud Platform + +## Quickstart +* Compiler + * use python to compiler example.py to example.json +* Run + * Open Vertex on GCP + * Click 'Pipelines' and create run + * Click 'Upload file' to upload example.json + * Select bucket then submit + +## Cleanup +Following are optional to cleanup +* endpoint + * Click 'Online prediction' + * Click 'Actions' to delete endpoint +* model + * Click 'Model Registry' + * Click 'More' to delete model diff --git a/community-content/vertex_pipeline_kfpv2/create-endpoint.py b/community-content/vertex_pipeline_kfpv2/create-endpoint.py new file mode 100644 index 000000000..d9fed1aef --- /dev/null +++ b/community-content/vertex_pipeline_kfpv2/create-endpoint.py @@ -0,0 +1,28 @@ +from kfp.v2 import dsl + +@dsl.component(base_image='python:3.8',packages_to_install=['google-cloud-aiplatform==1.36.0']) +def create_endpoint( + endpoint_name: str, + project_id: str, + location: str, +): + import json + from google.cloud import aiplatform + + aiplatform.init( + project=project_id, + location=location, + ) + endpoint = aiplatform.Endpoint.create(display_name=endpoint_name) + + +@dsl.pipeline(name='create-endpoint') +def pipeline_create_endpoint(): + create_endpoint("auto_endpoint", "990000000009", "us-west1") + + +if __name__ == "__main__": + from kfp.v2 import compiler + compiler.Compiler().compile( + pipeline_func=pipeline_create_endpoint, + package_path='create_endpoint.json') \ No newline at end of file diff --git a/community-content/vertex_pipeline_kfpv2/deploy-model.py b/community-content/vertex_pipeline_kfpv2/deploy-model.py new file mode 100644 index 000000000..84e13aba2 --- /dev/null +++ b/community-content/vertex_pipeline_kfpv2/deploy-model.py @@ -0,0 +1,37 @@ +from kfp.v2 import dsl + +@dsl.component(base_image='python:3.8',packages_to_install=['google-cloud-aiplatform==1.36.0']) +def deploy_model( + model_id: str, + endpoint_id: str, + machine_type: str, + min_replica_count: int, + max_replica_count: int, +): + import json + from google.cloud import aiplatform + + model = aiplatform.Model(model_id) + endpoint = aiplatform.Endpoint(endpoint_id) + + endpoint = model.deploy( + endpoint=endpoint, + machine_type=machine_type, + min_replica_count=min_replica_count, + max_replica_count=max_replica_count, + ) + + +@dsl.pipeline(name='deploy-model') +def pipeline_deploy_model(): + project = "projects/990000000009/locations/us-west1" + model_id = project + "/models/1100000000000000001" + endpoint_id = project + "/endpoints/2200000000000000002" + deploy_model(model_id, endpoint_id, "n1-standard-2", 1, 1) + + +if __name__ == "__main__": + from kfp.v2 import compiler + compiler.Compiler().compile( + pipeline_func=pipeline_deploy_model, + package_path='deploy_model.json') \ No newline at end of file diff --git a/community-content/vertex_pipeline_kfpv2/train-model.py b/community-content/vertex_pipeline_kfpv2/train-model.py new file mode 100644 index 000000000..ab5505e4a --- /dev/null +++ b/community-content/vertex_pipeline_kfpv2/train-model.py @@ -0,0 +1,30 @@ +from kfp.v2 import dsl + +@dsl.component(base_image='python:3.8',packages_to_install=['google-cloud-aiplatform==1.36.0,scikit-learn==1.3.2']) +def train_model( + model_name: str, + project_id: str, + location: str, +): + from google.cloud import aiplatform + from sklearn.linear_model import LinearRegression + + aiplatform.init( + project=project_id, + location=location, + staging_bucket=f"gs://{shared_state['model-bucket']}", + ) + model = LinearRegression() + aiplatform.save_model(model, model_name) + + +@dsl.pipeline(name='train-model') +def pipeline_train_model(): + train_model("lr-model", "990000000009", "us-west1") + + +if __name__ == "__main__": + from kfp.v2 import compiler + compiler.Compiler().compile( + pipeline_func=pipeline_train_model, + package_path='train_model.json') \ No newline at end of file