-
Notifications
You must be signed in to change notification settings - Fork 4
/
component.yaml
176 lines (160 loc) · 6.79 KB
/
component.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
name: Upload Scikit learn pickle model to Google Cloud Vertex AI
metadata:
annotations: {author: Alexey Volkov <alexey.volkov@ark-kun.com>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/google-cloud/Vertex_AI/Models/Upload_Scikit-learn_pickle_model/component.yaml'}
inputs:
- {name: model, type: ScikitLearnPickleModel}
- {name: sklearn_version, type: String, optional: true}
- {name: display_name, type: String, optional: true}
- {name: description, type: String, optional: true}
- {name: project, type: String, optional: true}
- {name: location, type: String, optional: true}
- {name: labels, type: JsonObject, optional: true}
- {name: staging_bucket, type: String, optional: true}
outputs:
- {name: model_name, type: GoogleCloudVertexAiModelName}
- {name: model_dict, type: JsonObject}
implementation:
container:
image: python:3.9
command:
- sh
- -c
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
'google-cloud-aiplatform==1.16.0' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3
-m pip install --quiet --no-warn-script-location 'google-cloud-aiplatform==1.16.0'
--user) && "$0" "$@"
- sh
- -ec
- |
program_path=$(mktemp)
printf "%s" "$0" > "$program_path"
python3 -u "$program_path" "$@"
- |
def upload_Scikit_learn_pickle_model_to_Google_Cloud_Vertex_AI(
model_path,
sklearn_version = None,
display_name = None,
description = None,
# Uncomment when anyone requests these:
# instance_schema_uri: str = None,
# parameters_schema_uri: str = None,
# prediction_schema_uri: str = None,
# explanation_metadata: "google.cloud.aiplatform_v1.types.explanation_metadata.ExplanationMetadata" = None,
# explanation_parameters: "google.cloud.aiplatform_v1.types.explanation.ExplanationParameters" = None,
project = None,
location = None,
labels = None,
# encryption_spec_key_name: str = None,
staging_bucket = None,
):
import json
import os
import shutil
import tempfile
from google.cloud import aiplatform
if not location:
location = os.environ.get("CLOUD_ML_REGION")
if not labels:
labels = {}
labels["component-source"] = "github-com-ark-kun-pipeline-components"
# The serving container decides the model type based on the model file extension.
# So we need to rename the mode file (e.g. /tmp/inputs/model/data) to *.pkl
_, renamed_model_path = tempfile.mkstemp(suffix=".pkl")
shutil.copyfile(src=model_path, dst=renamed_model_path)
model = aiplatform.Model.upload_scikit_learn_model_file(
model_file_path=renamed_model_path,
sklearn_version=sklearn_version,
display_name=display_name,
description=description,
# instance_schema_uri=instance_schema_uri,
# parameters_schema_uri=parameters_schema_uri,
# prediction_schema_uri=prediction_schema_uri,
# explanation_metadata=explanation_metadata,
# explanation_parameters=explanation_parameters,
project=project,
location=location,
labels=labels,
# encryption_spec_key_name=encryption_spec_key_name,
staging_bucket=staging_bucket,
)
model_json = json.dumps(model.to_dict(), indent=2)
print(model_json)
return (model.resource_name, model_json)
def _serialize_json(obj) -> str:
if isinstance(obj, str):
return obj
import json
def default_serializer(obj):
if hasattr(obj, 'to_struct'):
return obj.to_struct()
else:
raise TypeError("Object of type '%s' is not JSON serializable and does not have .to_struct() method." % obj.__class__.__name__)
return json.dumps(obj, default=default_serializer, sort_keys=True)
import json
import argparse
_parser = argparse.ArgumentParser(prog='Upload Scikit learn pickle model to Google Cloud Vertex AI', description='')
_parser.add_argument("--model", dest="model_path", type=str, required=True, default=argparse.SUPPRESS)
_parser.add_argument("--sklearn-version", dest="sklearn_version", type=str, required=False, default=argparse.SUPPRESS)
_parser.add_argument("--display-name", dest="display_name", type=str, required=False, default=argparse.SUPPRESS)
_parser.add_argument("--description", dest="description", type=str, required=False, default=argparse.SUPPRESS)
_parser.add_argument("--project", dest="project", type=str, required=False, default=argparse.SUPPRESS)
_parser.add_argument("--location", dest="location", type=str, required=False, default=argparse.SUPPRESS)
_parser.add_argument("--labels", dest="labels", type=json.loads, required=False, default=argparse.SUPPRESS)
_parser.add_argument("--staging-bucket", dest="staging_bucket", type=str, required=False, default=argparse.SUPPRESS)
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=2)
_parsed_args = vars(_parser.parse_args())
_output_files = _parsed_args.pop("_output_paths", [])
_outputs = upload_Scikit_learn_pickle_model_to_Google_Cloud_Vertex_AI(**_parsed_args)
_output_serializers = [
str,
_serialize_json,
]
import os
for idx, output_file in enumerate(_output_files):
try:
os.makedirs(os.path.dirname(output_file))
except OSError:
pass
with open(output_file, 'w') as f:
f.write(_output_serializers[idx](_outputs[idx]))
args:
- --model
- {inputPath: model}
- if:
cond: {isPresent: sklearn_version}
then:
- --sklearn-version
- {inputValue: sklearn_version}
- if:
cond: {isPresent: display_name}
then:
- --display-name
- {inputValue: display_name}
- if:
cond: {isPresent: description}
then:
- --description
- {inputValue: description}
- if:
cond: {isPresent: project}
then:
- --project
- {inputValue: project}
- if:
cond: {isPresent: location}
then:
- --location
- {inputValue: location}
- if:
cond: {isPresent: labels}
then:
- --labels
- {inputValue: labels}
- if:
cond: {isPresent: staging_bucket}
then:
- --staging-bucket
- {inputValue: staging_bucket}
- '----output-paths'
- {outputPath: model_name}
- {outputPath: model_dict}