-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlightweight_python_functions_v2_with_outputs.py
86 lines (69 loc) · 2.69 KB
/
lightweight_python_functions_v2_with_outputs.py
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
# Copyright 2021 The Kubeflow Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Lightweight functions v2 with outputs."""
from typing import NamedTuple
import os
from kfp import compiler, dsl
from kfp.dsl import Input, Dataset, Model, Metrics, component
# In tests, we install a KFP package from the PR under test. Users should not
# normally need to specify `kfp_package_path` in their component definitions.
_KFP_PACKAGE_PATH = os.getenv('KFP_PACKAGE_PATH')
@component(kfp_package_path=_KFP_PACKAGE_PATH)
def concat_message(first: str, second: str) -> str:
return first + second
@component(kfp_package_path=_KFP_PACKAGE_PATH)
def add_numbers(first: int, second: int) -> int:
return first + second
@component(kfp_package_path=_KFP_PACKAGE_PATH)
def output_artifact(number: int, message: str) -> Dataset:
result = [message for _ in range(number)]
return '\n'.join(result)
@component(kfp_package_path=_KFP_PACKAGE_PATH)
def output_named_tuple(
artifact: Input[Dataset]
) -> NamedTuple('Outputs', [
('scalar', str),
('metrics', Metrics),
('model', Model),
]):
scalar = "123"
import json
metrics = json.dumps({
'metrics': [{
'name': 'accuracy',
'numberValue': 0.9,
'format': "PERCENTAGE",
}]
})
with open(artifact.path, 'r') as f:
artifact_contents = f.read()
model = "Model contents: " + artifact_contents
from collections import namedtuple
output = namedtuple('Outputs', ['scalar', 'metrics', 'model'])
return output(scalar, metrics, model)
@dsl.pipeline(name='functions-with-outputs')
def pipeline(
first_message: str = 'first',
second_message: str = 'second',
first_number: int = 1,
second_number: int = 2,
):
concat_task = concat_message(first=first_message, second=second_message)
add_numbers_task = add_numbers(first=first_number, second=second_number)
output_artifact_task = output_artifact(
number=add_numbers_task.output, message=concat_task.output)
output_name_tuple = output_named_tuple(artifact=output_artifact_task.output)
if __name__ == '__main__':
compiler.Compiler().compile(
pipeline_func=pipeline, package_path=__file__ + '.yaml')