11"""Model Card TFX Component.
22
3- The ModelCardGenerator is used to generate model cards in TFX pipelines .
3+ The ModelCardGenerator is used to generate model cards in a TFX pipeline .
44"""
55
66from typing import Any , List , Tuple , Optional
1919
2020
2121class ModelCardGeneratorSpec (component_spec .ComponentSpec ):
22- """Component spec for Model Card TFX component ."""
22+ """Component spec for the ModelCardGenerator ."""
2323 PARAMETERS = {
2424 'json' :
2525 component_spec .ExecutionParameter (type = str , optional = True ),
@@ -50,12 +50,34 @@ class ModelCardGeneratorSpec(component_spec.ComponentSpec):
5050class ModelCardGenerator (BaseComponent ):
5151 """A TFX component to generate a model card.
5252
53- Uses ExampleStatistics, ModelEvaluation, and PushedModel artifacts to generate
54- a model card. Writes a ModelCard artifact.
53+ The `ModelCardGenerator` is a [TFX
54+ Component](https://www.tensorflow.org/tfx/guide/understanding_tfx_pipelines#component)
55+ that generates model cards.
5556
56- Accepts `json` to populate model card fields manually.
57+ The model cards are written to a `ModelCard` artifact that can be fetched
58+ from the `outputs['model_card]'` property.
5759
58- Accepts `template_io` to use custom Jinja templates.
60+ Example:
61+
62+ ```py
63+ context = InteractiveContext()
64+ ...
65+ mc_gen = ModelCardGenerator(
66+ statistics=statistics_gen.outputs['statistics'],
67+ evaluation=evaluator.outputs['evaluation'],
68+ pushed_model=pusher.outputs['pushed_model'],
69+ json="{'model_details': {'name': 'my_model'}}",
70+ template_io=[
71+ ('html/default_template.html.jinja', 'model_card.html'),
72+ ('md/default_template.md.jinja', 'model_card.md')
73+ ]
74+ )
75+ context.run(mc_gen)
76+ mc_artifact = mc_gen.outputs['model_card']
77+ mc_path = os.path.join(mc_artifact.uri, 'model_card', 'model_card.html')
78+ with open(mc_path) as f:
79+ mc_content = f.readlines()
80+ ```
5981 """
6082
6183 SPEC_CLASS = ModelCardGeneratorSpec
@@ -70,21 +92,40 @@ def __init__(self,
7092 ):
7193 """Generate a model card for a TFX pipeline.
7294
95+ This executes a Model Card Toolkit workflow, producing a `ModelCard`
96+ artifact.
97+
98+ Model card generation is partially automated from TFX, using the
99+ `ExampleStatistics`, `ModelEvaluation`, and `PushedModel` artifacts. Model
100+ card fields may be manually populated using the `json` arg. See the Args
101+ section for more details.
102+
103+ To use custom model card templates, use the `template_io` arg.
104+ `ModelCardGenerator` can generate multiple model cards per execution.
105+
73106 Args:
74- evaluation: TFMA output, used to populate quantitative analysis fields in
75- the model card.
76- statistics: TFDV output, used to populate dataset fields in the model
77- card.
78- pushed_model: PushedModel output, used to populate model details in the
79- the model card.
80- json: A JSON object containing `ModelCard` fields. This is particularly
107+ evaluation: TFMA output from an
108+ [Evaluator](https://www.tensorflow.org/tfx/guide/evaluator) component,
109+ used to populate quantitative analysis fields in the model card.
110+ statistics: TFDV output from a
111+ [StatisticsGen](https://www.tensorflow.org/tfx/guide/statsgen)
112+ component, used to populate dataset fields in the model card.
113+ pushed_model: PushedModel output from a
114+ [Pusher](https://www.tensorflow.org/tfx/guide/pusher) component, used to
115+ populate model details in the the model card.
116+ json: A JSON string containing `ModelCard` fields. This is particularly
81117 useful for fields that cannot be auto-populated from earlier TFX
82118 components. If a field is populated both by TFX and JSON, the JSON value
83- will overwrite the TFX value.
84- template_io: A list of input/output pairs. The input is a jinja template
85- path to use when generating model card documents. The output is the file
86- name to write the model card document to. If nothing is provided,
87- `ModelCardToolkit`'s default HTML template and file name are used.
119+ will overwrite the TFX value. Use the [Model Card JSON
120+ schema](https://github.com/tensorflow/model-card-toolkit/blob/master/model_card_toolkit/schema/v0.0.2/model_card.schema.json).
121+ template_io: A list of input/output pairs. The input is the path to a
122+ [Jinja](https://jinja.palletsprojects.com/) template. Using data
123+ extracted from TFX components and `json`, this template is populated and
124+ saved as a model card. The output is a file name where the model card
125+ will be written to in the `model_card/` directory. By default,
126+ `ModelCardToolkit`'s default HTML template
127+ (`default_template.html.jinja`) and file name (`model_card.html`) are
128+ used.
88129 """
89130 spec = ModelCardGeneratorSpec (
90131 evaluation = evaluation ,
0 commit comments