Created
June 21, 2023 19:50
-
-
Save HumairAK/d3d01a05080ad958226799449f167a3f to your computer and use it in GitHub Desktop.
flip-coin example in kfp v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kfp | |
from kfp import dsl | |
@dsl.component(base_image="quay.io/hukhan/python:3.9.17-alpine3.18") | |
def get_random_int_op(minimum: int, maximum: int) -> int: | |
"""Generate a random number between minimum and maximum (inclusive).""" | |
import random | |
result = random.randint(minimum, maximum) | |
print(result) | |
return result | |
@dsl.component(base_image="quay.io/hukhan/python:3.9.17-alpine3.18") | |
def flip_coin_op() -> str: | |
"""Flip a coin and output heads or tails randomly.""" | |
import random | |
result = random.choice(['heads', 'tails']) | |
print(result) | |
return result | |
@dsl.component(base_image="quay.io/hukhan/python:3.9.17-alpine3.18") | |
def print_op(message: str): | |
"""Print a message.""" | |
print(message) | |
@dsl.component(base_image="quay.io/hukhan/python:3.9.17-alpine3.18") | |
def fail_op(message: str): | |
"""Fails.""" | |
import sys | |
print(message) | |
sys.exit(1) | |
@dsl.pipeline( | |
name='tutorial-control-flows', | |
description='Shows how to use dsl.Condition(), dsl.ParallelFor, and dsl.ExitHandler().' | |
) | |
def control_flows_pipeline(): | |
flip = flip_coin_op() | |
with dsl.ParallelFor(['heads', 'tails']) as expected_result: | |
with dsl.Condition(flip.output == expected_result): | |
random_num_head = get_random_int_op(minimum=0, maximum=9) | |
with dsl.Condition(random_num_head.output > 5): | |
print_op( | |
message=f'{expected_result} and {random_num_head.output} > 5!' | |
) | |
with dsl.Condition(random_num_head.output <= 5): | |
print_op( | |
message=f'{expected_result} and {random_num_head.output} <= 5!' | |
) | |
if __name__ == '__main__': | |
kfp.compiler.Compiler().compile(control_flows_pipeline, __file__ + '.yaml') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment