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

Commit f24c2c7

Browse files
authored
Prevent empty fields from rendering as "None" (#251)
1 parent eec432b commit f24c2c7

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

model_card_toolkit/base_model_card_field.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class BaseModelCardField(abc.ABC):
3737
need to override this unless it needs some special process.
3838
"""
3939

40+
def __len__(self) -> int:
41+
"""Returns the number of items in a field. Ignores None values recursively,
42+
so the length of a field that only contains another field that has all None
43+
values would be 0.
44+
"""
45+
return len(self.to_dict())
46+
4047
@property
4148
@abc.abstractmethod
4249
def _proto_type(self):

model_card_toolkit/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def export_format(self,
399399
# If model_card is not passed in, read from Proto file.
400400
else:
401401
model_card = self._read_proto_file(self._mcta_proto_file)
402-
if not model_card:
402+
if model_card is None:
403403
raise ValueError('model_card could not be found. '
404404
'Call scaffold_assets() to generate model_card.')
405405

model_card_toolkit/model_card_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_merge_from_proto_and_to_proto_with_all_fields(self):
5252

5353
self.assertEqual(want_proto, got_proto)
5454

55-
def test_copy_from_proto_sucess(self):
55+
def test_copy_from_proto_success(self):
5656
# Test fields convert.
5757
owner = model_card.Owner(name="my_name1")
5858
owner_proto = model_card_pb2.Owner(name="my_name2", contact="my_contact2")
@@ -71,7 +71,7 @@ def test_copy_from_proto_sucess(self):
7171
model_card.ModelDetails(
7272
owners=[model_card.Owner(name="my_name2", contact="my_contact2")]))
7373

74-
def test_merge_from_proto_sucess(self):
74+
def test_merge_from_proto_success(self):
7575
# Test fields convert.
7676
owner = model_card.Owner(name="my_name1")
7777
owner_proto = model_card_pb2.Owner(contact="my_contact1")
@@ -109,7 +109,7 @@ def test_merge_from_proto_with_invalid_proto(self):
109109
TypeError, ".*expected .*Owner got .*Version.*"):
110110
owner.merge_from_proto(wrong_proto)
111111

112-
def test_to_proto_sucess(self):
112+
def test_to_proto_success(self):
113113
# Test fields convert.
114114
owner = model_card.Owner()
115115
self.assertEqual(owner.to_proto(), model_card_pb2.Owner())
@@ -125,8 +125,7 @@ def test_to_proto_sucess(self):
125125
self.assertEqual(
126126
model_details.to_proto(),
127127
model_card_pb2.ModelDetails(
128-
owners=[model_card_pb2.Owner(name="my_name", contact="my_contact")],
129-
version=model_card_pb2.Version()))
128+
owners=[model_card_pb2.Owner(name="my_name", contact="my_contact")]))
130129

131130
def test_to_proto_with_invalid_field(self):
132131
owner = model_card.Owner()

model_card_toolkit/template/html/default_template.html.jinja

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
{% macro render_metrics_graphics(graphics) %}
8383
<div class="row">
8484
<div class="col">
85-
{{ graphics.description }}
85+
{% if graphics.description %}<p>{{ graphics.description }}</p>{% endif %}
8686
{{ render_graphics(graphics.collection) }}
8787
</div>
8888
</div>
@@ -115,6 +115,9 @@
115115
margin-left: auto;
116116
margin-right: auto;
117117
}
118+
table {
119+
margin-bottom: 10px;
120+
}
118121
table th {
119122
background: #eee;
120123
}
@@ -131,14 +134,15 @@
131134
caption { font-weight: bold; }
132135
</style>
133136
<title>
134-
Model Card for {{ model_details.name }}
137+
Model Card{% if model_details.name %} for {{ model_details.name }}{% endif %}
135138
</title>
136139
</head>
137140
<body>
138141
<h1>
139-
Model Card for {{ model_details.name }}
142+
Model Card{% if model_details.name %} for {{ model_details.name }}{% endif %}
140143
</h1>
141144
<div class="row">
145+
{% if model_details %}
142146
<div class="col card">
143147
<h2>Model Details</h2>
144148
{% if model_details.overview %}<h3>Overview</h3>
@@ -178,6 +182,7 @@
178182
{% endfor %}
179183
</ul>{% endif %}
180184
</div>
185+
{% endif %}
181186
{% if model_parameters.model_architecture or model_parameters.input_format or model_parameters.input_format_map or model_parameters.output_format or model_parameters.output_format_map %}
182187
<div class="col card">
183188
<h2>Model Parameters</h2>

model_card_toolkit/template/md/default_template.md.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
|Name|Value|
2626
-----|------{% for metric in metrics %}
2727
|{{ metric_name(metric) }}|{{ metric_value(metric) }}|{% endfor %}{% endmacro %}
28-
# Model Card for {{ model_details.name }}
28+
# Model Card{% if model_details.name %} for {{ model_details.name }}{% endif %}{% if model_details %}
2929

3030
## Model Details{% if model_details.overview %}
3131

@@ -50,7 +50,7 @@
5050
### Citations
5151
{% for citation in model_details.citations %}
5252
* {{ citation.citation }}{% endfor %}
53-
{% endif %}{% if model_parameters.model_architecture or model_parameters.input_format or model_parameters.output_format %}
53+
{% endif %}{% endif %}{% if model_parameters.model_architecture or model_parameters.input_format or model_parameters.output_format %}
5454

5555
## Model Parameters
5656
{% if model_parameters.model_architecture %}

0 commit comments

Comments
 (0)