feat(components): Google - AutoML - Added the Deploy model component (#4520)
This commit is contained in:
parent
68a367de3d
commit
5fe8ef40e1
|
|
@ -0,0 +1,40 @@
|
||||||
|
from typing import NamedTuple
|
||||||
|
from kfp.components import create_component_from_func
|
||||||
|
|
||||||
|
|
||||||
|
def automl_deploy_model(
|
||||||
|
model_path: str,
|
||||||
|
) -> NamedTuple('Outputs', [
|
||||||
|
('model_path', str),
|
||||||
|
]):
|
||||||
|
"""Deploys a trained model.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model_path: The resource name of the model to export. Format: 'projects/<project>/locations/<location>/models/<model>'
|
||||||
|
|
||||||
|
Annotations:
|
||||||
|
author: Alexey Volkov <alexey.volkov@ark-kun.com>
|
||||||
|
"""
|
||||||
|
from google.cloud import automl
|
||||||
|
client = automl.AutoMlClient()
|
||||||
|
response = client.deploy_model(
|
||||||
|
name=model_path,
|
||||||
|
)
|
||||||
|
print('Operation started:')
|
||||||
|
print(response.operation)
|
||||||
|
result = response.result()
|
||||||
|
metadata = response.metadata
|
||||||
|
print('Operation finished:')
|
||||||
|
print(metadata)
|
||||||
|
return (model_path, )
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
automl_deploy_model_op = create_component_from_func(
|
||||||
|
automl_deploy_model,
|
||||||
|
output_component_file='component.yaml',
|
||||||
|
base_image='python:3.8',
|
||||||
|
packages_to_install=[
|
||||||
|
'google-cloud-automl==2.0.0',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
name: Automl deploy model
|
||||||
|
description: |-
|
||||||
|
Deploys a trained model.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model_path: The resource name of the model to export. Format: 'projects/<project>/locations/<location>/models/<model>'
|
||||||
|
|
||||||
|
Annotations:
|
||||||
|
author: Alexey Volkov <alexey.volkov@ark-kun.com>
|
||||||
|
inputs:
|
||||||
|
- {name: model_path, type: String}
|
||||||
|
outputs:
|
||||||
|
- {name: model_path, type: String}
|
||||||
|
implementation:
|
||||||
|
container:
|
||||||
|
image: python:3.8
|
||||||
|
command:
|
||||||
|
- sh
|
||||||
|
- -c
|
||||||
|
- (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
|
||||||
|
'google-cloud-automl==2.0.0' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip
|
||||||
|
install --quiet --no-warn-script-location 'google-cloud-automl==2.0.0' --user)
|
||||||
|
&& "$0" "$@"
|
||||||
|
- python3
|
||||||
|
- -u
|
||||||
|
- -c
|
||||||
|
- |
|
||||||
|
def automl_deploy_model(
|
||||||
|
model_path,
|
||||||
|
):
|
||||||
|
"""Deploys a trained model.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model_path: The resource name of the model to export. Format: 'projects/<project>/locations/<location>/models/<model>'
|
||||||
|
|
||||||
|
Annotations:
|
||||||
|
author: Alexey Volkov <alexey.volkov@ark-kun.com>
|
||||||
|
"""
|
||||||
|
from google.cloud import automl
|
||||||
|
client = automl.AutoMlClient()
|
||||||
|
response = client.deploy_model(
|
||||||
|
name=model_path,
|
||||||
|
)
|
||||||
|
print('Operation started:')
|
||||||
|
print(response.operation)
|
||||||
|
result = response.result()
|
||||||
|
metadata = response.metadata
|
||||||
|
print('Operation finished:')
|
||||||
|
print(metadata)
|
||||||
|
return (model_path, )
|
||||||
|
|
||||||
|
def _serialize_str(str_value: str) -> str:
|
||||||
|
if not isinstance(str_value, str):
|
||||||
|
raise TypeError('Value "{}" has type "{}" instead of str.'.format(str(str_value), str(type(str_value))))
|
||||||
|
return str_value
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
_parser = argparse.ArgumentParser(prog='Automl deploy model', description="Deploys a trained model.\n\n Args:\n model_path: The resource name of the model to export. Format: 'projects/<project>/locations/<location>/models/<model>'\n\n Annotations:\n author: Alexey Volkov <alexey.volkov@ark-kun.com>")
|
||||||
|
_parser.add_argument("--model-path", dest="model_path", type=str, required=True, default=argparse.SUPPRESS)
|
||||||
|
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=1)
|
||||||
|
_parsed_args = vars(_parser.parse_args())
|
||||||
|
_output_files = _parsed_args.pop("_output_paths", [])
|
||||||
|
|
||||||
|
_outputs = automl_deploy_model(**_parsed_args)
|
||||||
|
|
||||||
|
_output_serializers = [
|
||||||
|
_serialize_str,
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
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-path
|
||||||
|
- {inputValue: model_path}
|
||||||
|
- '----output-paths'
|
||||||
|
- {outputPath: model_path}
|
||||||
Loading…
Reference in New Issue