-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path0-demo.py
83 lines (65 loc) · 2.37 KB
/
0-demo.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
#!/usr/bin/env python3
import json
import os.path as path
from typing import Dict
from kfp.compiler import Compiler
from kfp.dsl import ContainerOp, ExitHandler, pipeline
from kubernetes import client as k8s
OUT_DIR = '/out'
METADATA_FILE = 'mlpipeline-ui-metadata.json'
METRICS_FILE = 'mlpipeline-metrics.json'
METADATA_FILE_PATH = path.join(OUT_DIR, METADATA_FILE)
METRICS_FILE_PATH = path.join(OUT_DIR, METRICS_FILE)
@pipeline(name='My pipeline', description='')
def pipeline():
deploy = demo_op('deploy', is_exit_handler=True)
with ExitHandler(deploy):
deps = demo_op('setup dependencies')
analyze = demo_op('analyze data')
analyze.after(deps)
train1 = demo_op('training 1')
train2 = demo_op('training 2')
train3 = demo_op('training 3')
train1.after(analyze)
train2.after(analyze)
train3.after(analyze)
predict = demo_op('predict')
predict.after(train1)
predict.after(train2)
predict.after(train3)
matrix = demo_op('create confusion-matrix')
roc = demo_op('create roc')
matrix.after(predict)
roc.after(predict)
if __name__ == '__main__':
Compiler().compile(pipeline)
def markdown_metadata(result: str) -> str:
return json.dumps({
'outputs': [{
'type': 'markdown',
'source': 'The result: %s' % result,
'storage': 'inline',
}]
})
def demo_op(name: str, is_exit_handler=False) -> ContainerOp:
op = ContainerOp(name=name,
image='alpine:latest',
command=['sh', '-c'],
arguments=[
'echo "Running step $0" && echo "$1" > $2',
name,
markdown_metadata(name),
METADATA_FILE_PATH,
],
is_exit_handler=is_exit_handler,
output_artifact_paths=default_artifact_path())
op.add_volume(
k8s.V1Volume(name='volume',
empty_dir=k8s.V1EmptyDirVolumeSource())).add_volume_mount(
k8s.V1VolumeMount(name='volume', mount_path=OUT_DIR))
return op
def default_artifact_path() -> Dict[str, str]:
return {
path.splitext(METADATA_FILE)[0]: METADATA_FILE_PATH,
path.splitext(METRICS_FILE)[0]: METRICS_FILE_PATH,
}