Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk.v2): Add HTML and Markdown artifact types #6246

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions sdk/python/kfp/dsl/io_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import os
from typing import Dict, Generic, List, Optional, Type, TypeVar, Union


_GCS_LOCAL_MOUNT_PREFIX = '/gcs/'
_MINIO_LOCAL_MOUNT_PREFIX = '/minio/'
_S3_LOCAL_MOUNT_PREFIX = '/s3/'
Expand Down Expand Up @@ -399,6 +398,28 @@ def load_confusion_matrix(self, slice: str, categories: List[str],
self._update_metadata(slice)


class HTML(Artifact):
"""An artifact representing an HTML file."""
TYPE_NAME = 'system.HTML'

def __init__(self,
name: Optional[str] = None,
uri: Optional[str] = None,
metadata: Optional[Dict] = None):
super().__init__(uri=uri, name=name, metadata=metadata)


class Markdown(Artifact):
"""An artifact representing an Markdown file."""
TYPE_NAME = 'system.Markdown'

def __init__(self,
name: Optional[str] = None,
uri: Optional[str] = None,
metadata: Optional[Dict] = None):
super().__init__(uri=uri, name=name, metadata=metadata)


T = TypeVar('T')


Expand All @@ -407,7 +428,6 @@ class InputAnnotation():
pass



class OutputAnnotation():
"""Marker type for output artifacts."""
pass
Expand All @@ -419,7 +439,6 @@ class OutputAnnotation():
# Input = typing.Annotated[T, InputAnnotation]
# Output = typing.Annotated[T, OutputAnnotation]


# Input represents an Input artifact of type T.
Input = Union[T, InputAnnotation]

Expand All @@ -430,16 +449,16 @@ class OutputAnnotation():
def is_artifact_annotation(typ) -> bool:
if hasattr(typ, '_subs_tree'): # Python 3.6
subs_tree = typ._subs_tree()
return len(subs_tree) == 3 and subs_tree[0] == Union and subs_tree[2] in [InputAnnotation, OutputAnnotation]
return len(subs_tree) == 3 and subs_tree[0] == Union and subs_tree[2] in [
InputAnnotation, OutputAnnotation
]

if not hasattr(typ, '__origin__'):
return False


if typ.__origin__ != Union and type(typ.__origin__) != type(Union):
return False


if not hasattr(typ, '__args__') or len(typ.__args__) != 2:
return False

Expand All @@ -448,28 +467,31 @@ def is_artifact_annotation(typ) -> bool:

return True


def is_input_artifact(typ) -> bool:
"""Returns True if typ is of type Input[T]."""
if not is_artifact_annotation(typ):
return False

if hasattr(typ, '_subs_tree'): # Python 3.6
subs_tree = typ._subs_tree()
return len(subs_tree) == 3 and subs_tree[2] == InputAnnotation
return len(subs_tree) == 3 and subs_tree[2] == InputAnnotation

return typ.__args__[1] == InputAnnotation


def is_output_artifact(typ) -> bool:
"""Returns True if typ is of type Output[T]."""
if not is_artifact_annotation(typ):
return False

if hasattr(typ, '_subs_tree'): # Python 3.6
subs_tree = typ._subs_tree()
return len(subs_tree) == 3 and subs_tree[2] == OutputAnnotation
return len(subs_tree) == 3 and subs_tree[2] == OutputAnnotation

return typ.__args__[1] == OutputAnnotation


def get_io_artifact_class(typ):
if not is_artifact_annotation(typ):
return None
Expand All @@ -484,6 +506,7 @@ def get_io_artifact_class(typ):

return typ.__args__[0]


def get_io_artifact_annotation(typ):
if not is_artifact_annotation(typ):
return None
Expand All @@ -497,7 +520,6 @@ def get_io_artifact_annotation(typ):
return typ.__args__[1]



_SCHEMA_TITLE_TO_TYPE: Dict[str, Artifact] = {
x.TYPE_NAME: x
for x in [Artifact, Model, Dataset, Metrics, ClassificationMetrics]
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/kfp/dsl/type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
'metrics': io_types.Metrics,
'classificationmetrics': io_types.ClassificationMetrics,
'slicedclassificationmetrics': io_types.SlicedClassificationMetrics,
'html': io_types.HTML,
'markdown': io_types.Markdown,
}

# ComponentSpec I/O types to (IR) PipelineTaskSpec I/O types mapping.
Expand Down
12 changes: 12 additions & 0 deletions sdk/python/kfp/dsl/type_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ def test_is_parameter_type(self):
'expected_result':
pb.ArtifactTypeSchema(schema_title='system.Artifact')
},
{
'artifact_class_or_type_name':
io_types.HTML,
'expected_result':
pb.ArtifactTypeSchema(schema_title='system.HTML')
},
{
'artifact_class_or_type_name':
io_types.Markdown,
'expected_result':
pb.ArtifactTypeSchema(schema_title='system.Markdown')
},
)
def test_get_artifact_type_schema(self, artifact_class_or_type_name,
expected_result):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"artifactType": {
"schemaTitle": "system.Artifact"
}
},
"input_h": {
"artifactType": {
"schemaTitle": "system.HTML"
}
}
},
"parameters": {
Expand Down Expand Up @@ -92,6 +97,11 @@
"artifactType": {
"schemaTitle": "system.Artifact"
}
},
"output_8": {
"artifactType": {
"schemaTitle": "system.HTML"
}
}
},
"parameters": {
Expand All @@ -113,7 +123,8 @@
"{{$.inputs.artifacts['input_d'].uri}}",
"{{$.inputs.artifacts['input_e'].uri}}",
"{{$.inputs.artifacts['input_f'].path}}",
"{{$.inputs.artifacts['input_g'].path}}"
"{{$.inputs.artifacts['input_g'].path}}",
"{{$.inputs.artifacts['input_h'].path}}"
],
"image": "gcr.io/image"
}
Expand All @@ -131,7 +142,8 @@
"{{$.outputs.artifacts['output_4'].uri}}",
"{{$.outputs.artifacts['output_5'].uri}}",
"{{$.outputs.artifacts['output_6'].path}}",
"{{$.outputs.artifacts['output_7'].path}}"
"{{$.outputs.artifacts['output_7'].path}}",
"{{$.outputs.artifacts['output_8'].path}}"
],
"image": "gcr.io/image"
}
Expand Down Expand Up @@ -191,6 +203,12 @@
"outputArtifactKey": "output_7",
"producerTask": "upstream"
}
},
"input_h": {
"taskOutputArtifact": {
"outputArtifactKey": "output_8",
"producerTask": "upstream"
}
}
},
"parameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- {name: output_5, type: Datasets}
- {name: output_6, type: Some arbitrary type}
- {name: output_7, type: {GcsPath: {data_type: TSV}}}
- {name: output_8, type: HTML}
implementation:
container:
image: gcr.io/image
Expand All @@ -48,6 +49,7 @@
- {outputUri: output_5}
- {outputPath: output_6}
- {outputPath: output_7}
- {outputPath: output_8}
""")

component_op_2 = components.load_component_from_text("""
Expand All @@ -60,6 +62,7 @@
- {name: input_e, type: Datasets}
- {name: input_f, type: Some arbitrary type}
- {name: input_g, type: {GcsPath: {data_type: TSV}}}
- {name: input_h, type: HTML}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add the Markdown type input here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily need to capture all the types we have in this test.
But type_utils_test.py has UTs for the complete list of artifact types.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks Chen for confirming!

implementation:
container:
image: gcr.io/image
Expand All @@ -71,6 +74,7 @@
- {inputUri: input_e}
- {inputPath: input_f}
- {inputPath: input_g}
- {inputPath: input_h}
""")


Expand All @@ -92,6 +96,7 @@ def my_pipeline(input1: str,
input_e=component_1.outputs['output_5'],
input_f=component_1.outputs['output_6'],
input_g=component_1.outputs['output_7'],
input_h=component_1.outputs['output_8'],
)


Expand Down