Upload examples of kfp v2 (#2627)

This commit is contained in:
Aiden010200
2024-01-18 19:08:49 +00:00
committed by GitHub
parent c8f95d2121
commit e735195230
4 changed files with 124 additions and 0 deletions
@@ -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
@@ -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')
@@ -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')
@@ -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')