Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit ce96783

Browse files
shuklak13ml-fairness-infra-github
authored andcommitted
improve model_card_toolkit.tfx docstrings
This will be used to generate the API docs at https://www.tensorflow.org/responsible_ai/model_card_toolkit/api_docs/python/model_card_toolkit PiperOrigin-RevId: 429084124
1 parent 0f4ca36 commit ce96783

File tree

4 files changed

+89
-80
lines changed

4 files changed

+89
-80
lines changed

model_card_toolkit/tfx/README.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

model_card_toolkit/tfx/artifact.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111

1212
class ModelCard(Artifact):
13+
"""A [TFX/MLMD artifact](https://www.tensorflow.org/tfx/guide/mlmd#data_model) to model card assets.
14+
15+
Assets include:
16+
* a data file containing the model card fields, located at
17+
`<uri>/data/model_card.proto`.
18+
* the model card itself, located at the `<uri>/model_card/ directory`.
19+
"""
1320
TYPE_NAME = 'ModelCard'
1421
TYPE_ANNOTATION = Metrics
1522

model_card_toolkit/tfx/component.py

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

66
from typing import Any, List, Tuple, Optional
@@ -19,7 +19,7 @@
1919

2020

2121
class 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):
5050
class 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,

model_card_toolkit/tfx/executor.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Model Card TFX Component Executor.
22
3-
The ModelCard Executor is used to generate model cards in TFX pipelines.
3+
The ModelCard Executor handles the ModelCardToolkit workflow in the
4+
ModelCardGenerator.
45
"""
56

67
from typing import Any, Dict, List, Optional
@@ -54,31 +55,37 @@ def Do(self, input_dict: Dict[str, List[types.Artifact]],
5455
"""Generate a model card for a TFX pipeline.
5556
5657
This executes a Model Card Toolkit workflow, producing a `ModelCard`
57-
artifact. This artifact references to a directory containing the Model Card
58-
document, as well as the `ModelCard` used to construct the document.
58+
artifact.
5959
6060
Args:
6161
input_dict: Input dict from key to a list of artifacts, including:
62-
- evaluation: TFMA output, used to populate quantitative analysis fields
63-
in the model card.
64-
- statistics: TFDV output, used to populate dataset fields in the model
65-
card.
66-
- pushed_model: PushedModel output, used to populate model details in
67-
the model card.
62+
- evaluation: TFMA output from an
63+
[Evaluator](https://www.tensorflow.org/tfx/guide/evaluator) component,
64+
used to populate quantitative analysis fields in the model card.
65+
- statistics: TFDV output from a
66+
[StatisticsGen](https://www.tensorflow.org/tfx/guide/statsgen)
67+
component, used to populate dataset fields in the model card.
68+
- pushed_model: PushedModel output from a
69+
[Pusher](https://www.tensorflow.org/tfx/guide/pusher) component, used
70+
to populate model details in the the model card.
6871
output_dict: Output dict from key to a list of artifacts, including:
6972
- model_card: An artifact referencing the directory containing the Model
7073
Card document, as well as the `ModelCard` used to construct the
7174
document.
7275
exec_properties: An optional dict of execution properties, including:
73-
- json: A JSON object containing `ModelCard` fields. This is
76+
- json: A JSON string containing `ModelCard` fields. This is
7477
particularly useful for fields that cannot be auto-populated from
7578
earlier TFX components. If a field is populated both by TFX and JSON,
76-
the JSON value will overwrite the TFX value.
77-
- template_io: A list of input/output pairs. The input is a jinja
78-
template path to use when generating model card documents. The output
79-
is the file name to write the model card document to. If nothing is
80-
provided, `ModelCardToolkit`'s default HTML template and file name are
81-
used.
79+
the JSON value will overwrite the TFX value. Use the [Model Card JSON
80+
schema](https://github.com/tensorflow/model-card-toolkit/blob/master/model_card_toolkit/schema/v0.0.2/model_card.schema.json).
81+
- template_io: A list of input/output pairs. The input is the path to a
82+
[Jinja](https://jinja.palletsprojects.com/) template. Using data
83+
extracted from TFX components and `json`, this template is populated
84+
and saved as a model card. The output is a file name where the model
85+
card will be written to in the `model_card/` directory. By default,
86+
`ModelCardToolkit`'s default HTML template
87+
(`default_template.html.jinja`) and file name (`model_card.html`)
88+
are used.
8289
"""
8390

8491
# Initialize ModelCardToolkit

0 commit comments

Comments
 (0)