# Path: model_card_toolkit/tfx/artifact.py
# class ModelCard(Artifact):
#   TYPE_NAME = 'ModelCard'
#   TYPE_ANNOTATION = Metrics
# def create_and_save_artifact(
#     artifact_name: str, artifact_uri: str,
#     store: mlmd.MetadataStore) -> metadata_store_pb2.Artifact:
# 
# Path: model_card_toolkit/tfx/component.py
# class ModelCardGenerator(BaseComponent):
#   """A TFX component to generate a model card.
# 
#   The `ModelCardGenerator` is a [TFX
#   Component](https://www.tensorflow.org/tfx/guide/understanding_tfx_pipelines#component)
#   that generates model cards.
# 
#   The model cards are written to a `ModelCard` artifact that can be fetched
#   from the `outputs['model_card]'` property.
# 
#   Example:
# 
#   ```py
#   context = InteractiveContext()
#   ...
#   mc_gen = ModelCardGenerator(
#       statistics=statistics_gen.outputs['statistics'],
#       evaluation=evaluator.outputs['evaluation'],
#       pushed_model=pusher.outputs['pushed_model'],
#       json="{'model_details': {'name': 'my_model'}}",
#       template_io=[
#           ('html/default_template.html.jinja', 'model_card.html'),
#           ('md/default_template.md.jinja', 'model_card.md')
#       ]
#       )
#   context.run(mc_gen)
#   mc_artifact = mc_gen.outputs['model_card'].get()[0]
#   mc_path = os.path.join(mc_artifact.uri, 'model_card', 'model_card.html')
#   with open(mc_path) as f:
#     mc_content = f.readlines()
#   ```
#   """
# 
#   SPEC_CLASS = ModelCardGeneratorSpec
#   EXECUTOR_SPEC = executor_spec.ExecutorClassSpec(executor.Executor)
# 
#   def __init__(self,
#                evaluation: Optional[types.Channel] = None,
#                statistics: Optional[types.Channel] = None,
#                pushed_model: Optional[types.Channel] = None,
#                json: Optional[str] = None,
#                template_io: Optional[List[Tuple[str, str]]] = None
#               ):
#     """Generate a model card for a TFX pipeline.
# 
#     This executes a Model Card Toolkit workflow, producing a `ModelCard`
#     artifact.
# 
#     Model card generation is partially automated from TFX, using the
#     `ExampleStatistics`, `ModelEvaluation`, and `PushedModel` artifacts. Model
#     card fields may be manually populated using the `json` arg. See the Args
#     section for more details.
# 
#     To use custom model card templates, use the `template_io` arg.
#     `ModelCardGenerator` can generate multiple model cards per execution.
# 
#     Args:
#       evaluation: TFMA output from an
#         [Evaluator](https://www.tensorflow.org/tfx/guide/evaluator) component,
#         used to populate quantitative analysis fields in the model card.
#       statistics: TFDV output from a
#         [StatisticsGen](https://www.tensorflow.org/tfx/guide/statsgen)
#         component, used to populate dataset fields in the model card.
#       pushed_model: PushedModel output from a
#         [Pusher](https://www.tensorflow.org/tfx/guide/pusher) component, used to
#         populate model details in the the model card.
#       json: A JSON string containing `ModelCard` fields. This is particularly
#         useful for fields that cannot be auto-populated from earlier TFX
#         components. If a field is populated both by TFX and JSON, the JSON value
#         will overwrite the TFX value. Use the [Model Card JSON
#         schema](https://github.com/tensorflow/model-card-toolkit/blob/master/model_card_toolkit/schema/v0.0.2/model_card.schema.json).
#       template_io: A list of input/output pairs. The input is the path to a
#         [Jinja](https://jinja.palletsprojects.com/) template. Using data
#         extracted from TFX components and `json`, this template is populated and
#         saved as a model card. The output is a file name where the model card
#         will be written to in the `model_card/` directory. By default,
#         `ModelCardToolkit`'s default HTML template
#         (`default_template.html.jinja`) and file name (`model_card.html`) are
#         used.
#     """
#     spec = ModelCardGeneratorSpec(
#         evaluation=evaluation,
#         statistics=statistics,
#         pushed_model=pushed_model,
#         model_card=types.Channel(type=artifact.ModelCard),
#         json=json,
#         template_io=template_io)
#     super(ModelCardGenerator, self).__init__(spec=spec)


import json as json_lib
from absl.testing import absltest
from model_card_toolkit.tfx import artifact
from model_card_toolkit.tfx.component import ModelCardGenerator
from tfx.types import channel_utils
from tfx.types import standard_artifacts
"""Tests for model_card_toolkit.tfx.component."""






class ComponentTest(absltest.TestCase):

  def test_component_construction(self):
    this_component = ModelCardGenerator(
        statistics=channel_utils.as_channel(
            [standard_artifacts.ExampleStatistics()]),
        evaluation=channel_utils.as_channel(
            [standard_artifacts.ModelEvaluation()]),
        pushed_model=channel_utils.as_channel(
            [standard_artifacts.PushedModel()]),
        json=json_lib.dumps(
            {'model_details': {
                'name': 'my model',
                'version': {
                    'name': 'v1'
                }
            }}),
        template_io=[('path/to/html/template', 'mc.html'),
                     ('path/to/md/template', 'mc.md')])

    with self.subTest('outputs'):
      self.assertEqual(this_component.outputs['model_card'].type_name,

# Path: model_card_toolkit/tfx/artifact.py
# class ModelCard(Artifact):
#   TYPE_NAME = 'ModelCard'
#   TYPE_ANNOTATION = Metrics
# def create_and_save_artifact(
#     artifact_name: str, artifact_uri: str,
#     store: mlmd.MetadataStore) -> metadata_store_pb2.Artifact:
# 
# Path: model_card_toolkit/tfx/component.py
# class ModelCardGenerator(BaseComponent):
#   """A TFX component to generate a model card.
# 
#   The `ModelCardGenerator` is a [TFX
#   Component](https://www.tensorflow.org/tfx/guide/understanding_tfx_pipelines#component)
#   that generates model cards.
# 
#   The model cards are written to a `ModelCard` artifact that can be fetched
#   from the `outputs['model_card]'` property.
# 
#   Example:
# 
#   ```py
#   context = InteractiveContext()
#   ...
#   mc_gen = ModelCardGenerator(
#       statistics=statistics_gen.outputs['statistics'],
#       evaluation=evaluator.outputs['evaluation'],
#       pushed_model=pusher.outputs['pushed_model'],
#       json="{'model_details': {'name': 'my_model'}}",
#       template_io=[
#           ('html/default_template.html.jinja', 'model_card.html'),
#           ('md/default_template.md.jinja', 'model_card.md')
#       ]
#       )
#   context.run(mc_gen)
#   mc_artifact = mc_gen.outputs['model_card'].get()[0]
#   mc_path = os.path.join(mc_artifact.uri, 'model_card', 'model_card.html')
#   with open(mc_path) as f:
#     mc_content = f.readlines()
#   ```
#   """
# 
#   SPEC_CLASS = ModelCardGeneratorSpec
#   EXECUTOR_SPEC = executor_spec.ExecutorClassSpec(executor.Executor)
# 
#   def __init__(self,
#                evaluation: Optional[types.Channel] = None,
#                statistics: Optional[types.Channel] = None,
#                pushed_model: Optional[types.Channel] = None,
#                json: Optional[str] = None,
#                template_io: Optional[List[Tuple[str, str]]] = None
#               ):
#     """Generate a model card for a TFX pipeline.
# 
#     This executes a Model Card Toolkit workflow, producing a `ModelCard`
#     artifact.
# 
#     Model card generation is partially automated from TFX, using the
#     `ExampleStatistics`, `ModelEvaluation`, and `PushedModel` artifacts. Model
#     card fields may be manually populated using the `json` arg. See the Args
#     section for more details.
# 
#     To use custom model card templates, use the `template_io` arg.
#     `ModelCardGenerator` can generate multiple model cards per execution.
# 
#     Args:
#       evaluation: TFMA output from an
#         [Evaluator](https://www.tensorflow.org/tfx/guide/evaluator) component,
#         used to populate quantitative analysis fields in the model card.
#       statistics: TFDV output from a
#         [StatisticsGen](https://www.tensorflow.org/tfx/guide/statsgen)
#         component, used to populate dataset fields in the model card.
#       pushed_model: PushedModel output from a
#         [Pusher](https://www.tensorflow.org/tfx/guide/pusher) component, used to
#         populate model details in the the model card.
#       json: A JSON string containing `ModelCard` fields. This is particularly
#         useful for fields that cannot be auto-populated from earlier TFX
#         components. If a field is populated both by TFX and JSON, the JSON value
#         will overwrite the TFX value. Use the [Model Card JSON
#         schema](https://github.com/tensorflow/model-card-toolkit/blob/master/model_card_toolkit/schema/v0.0.2/model_card.schema.json).
#       template_io: A list of input/output pairs. The input is the path to a
#         [Jinja](https://jinja.palletsprojects.com/) template. Using data
#         extracted from TFX components and `json`, this template is populated and
#         saved as a model card. The output is a file name where the model card
#         will be written to in the `model_card/` directory. By default,
#         `ModelCardToolkit`'s default HTML template
#         (`default_template.html.jinja`) and file name (`model_card.html`) are
#         used.
#     """
#     spec = ModelCardGeneratorSpec(
#         evaluation=evaluation,
#         statistics=statistics,
#         pushed_model=pushed_model,
#         model_card=types.Channel(type=artifact.ModelCard),
#         json=json,
#         template_io=template_io)
#     super(ModelCardGenerator, self).__init__(spec=spec)


# Path: model_card_toolkit/tfx/component_test.py
import json as json_lib
from absl.testing import absltest
from model_card_toolkit.tfx import artifact
from model_card_toolkit.tfx.component import ModelCardGenerator
from tfx.types import channel_utils
from tfx.types import standard_artifacts

"""Tests for model_card_toolkit.tfx.component."""






class ComponentTest(absltest.TestCase):

  def test_component_construction(self):
    this_component = ModelCardGenerator(
        statistics=channel_utils.as_channel(
            [standard_artifacts.ExampleStatistics()]),
        evaluation=channel_utils.as_channel(
            [standard_artifacts.ModelEvaluation()]),
        pushed_model=channel_utils.as_channel(
            [standard_artifacts.PushedModel()]),
        json=json_lib.dumps(
            {'model_details': {
                'name': 'my model',
                'version': {
                    'name': 'v1'
                }
            }}),
        template_io=[('path/to/html/template', 'mc.html'),
                     ('path/to/md/template', 'mc.md')])

    with self.subTest('outputs'):
      self.assertEqual(this_component.outputs['model_card'].type_name,

                       artifact.ModelCard.TYPE_NAME)