diff --git a/model_card_toolkit/base_model_card_field.py b/model_card_toolkit/base_model_card_field.py index 5d4e46e..0c65f5a 100644 --- a/model_card_toolkit/base_model_card_field.py +++ b/model_card_toolkit/base_model_card_field.py @@ -37,6 +37,13 @@ class BaseModelCardField(abc.ABC): need to override this unless it needs some special process. """ + def __len__(self) -> int: + """Returns the number of items in a field. Ignores None values recursively, + so the length of a field that only contains another field that has all None + values would be 0. + """ + return len(self.to_dict()) + @property @abc.abstractmethod def _proto_type(self): diff --git a/model_card_toolkit/core.py b/model_card_toolkit/core.py index fa61bc7..49e60ce 100644 --- a/model_card_toolkit/core.py +++ b/model_card_toolkit/core.py @@ -399,7 +399,7 @@ def export_format(self, # If model_card is not passed in, read from Proto file. else: model_card = self._read_proto_file(self._mcta_proto_file) - if not model_card: + if model_card is None: raise ValueError('model_card could not be found. ' 'Call scaffold_assets() to generate model_card.') diff --git a/model_card_toolkit/model_card_test.py b/model_card_toolkit/model_card_test.py index c3fcade..06eb201 100644 --- a/model_card_toolkit/model_card_test.py +++ b/model_card_toolkit/model_card_test.py @@ -52,7 +52,7 @@ def test_merge_from_proto_and_to_proto_with_all_fields(self): self.assertEqual(want_proto, got_proto) - def test_copy_from_proto_sucess(self): + def test_copy_from_proto_success(self): # Test fields convert. owner = model_card.Owner(name="my_name1") owner_proto = model_card_pb2.Owner(name="my_name2", contact="my_contact2") @@ -71,7 +71,7 @@ def test_copy_from_proto_sucess(self): model_card.ModelDetails( owners=[model_card.Owner(name="my_name2", contact="my_contact2")])) - def test_merge_from_proto_sucess(self): + def test_merge_from_proto_success(self): # Test fields convert. owner = model_card.Owner(name="my_name1") owner_proto = model_card_pb2.Owner(contact="my_contact1") @@ -109,7 +109,7 @@ def test_merge_from_proto_with_invalid_proto(self): TypeError, ".*expected .*Owner got .*Version.*"): owner.merge_from_proto(wrong_proto) - def test_to_proto_sucess(self): + def test_to_proto_success(self): # Test fields convert. owner = model_card.Owner() self.assertEqual(owner.to_proto(), model_card_pb2.Owner()) @@ -125,8 +125,7 @@ def test_to_proto_sucess(self): self.assertEqual( model_details.to_proto(), model_card_pb2.ModelDetails( - owners=[model_card_pb2.Owner(name="my_name", contact="my_contact")], - version=model_card_pb2.Version())) + owners=[model_card_pb2.Owner(name="my_name", contact="my_contact")])) def test_to_proto_with_invalid_field(self): owner = model_card.Owner() diff --git a/model_card_toolkit/template/html/default_template.html.jinja b/model_card_toolkit/template/html/default_template.html.jinja index c826ef1..7a617a4 100644 --- a/model_card_toolkit/template/html/default_template.html.jinja +++ b/model_card_toolkit/template/html/default_template.html.jinja @@ -82,7 +82,7 @@ {% macro render_metrics_graphics(graphics) %}
- {{ graphics.description }} + {% if graphics.description %}

{{ graphics.description }}

{% endif %} {{ render_graphics(graphics.collection) }}
@@ -115,6 +115,9 @@ margin-left: auto; margin-right: auto; } + table { + margin-bottom: 10px; + } table th { background: #eee; } @@ -131,14 +134,15 @@ caption { font-weight: bold; } - Model Card for {{ model_details.name }} + Model Card{% if model_details.name %} for {{ model_details.name }}{% endif %}

- Model Card for {{ model_details.name }} + Model Card{% if model_details.name %} for {{ model_details.name }}{% endif %}

+ {% if model_details %}

Model Details

{% if model_details.overview %}

Overview

@@ -178,6 +182,7 @@ {% endfor %} {% endif %}
+ {% endif %} {% 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 %}

Model Parameters

diff --git a/model_card_toolkit/template/md/default_template.md.jinja b/model_card_toolkit/template/md/default_template.md.jinja index c0ca4f6..0281584 100644 --- a/model_card_toolkit/template/md/default_template.md.jinja +++ b/model_card_toolkit/template/md/default_template.md.jinja @@ -25,7 +25,7 @@ |Name|Value| -----|------{% for metric in metrics %} |{{ metric_name(metric) }}|{{ metric_value(metric) }}|{% endfor %}{% endmacro %} -# Model Card for {{ model_details.name }} +# Model Card{% if model_details.name %} for {{ model_details.name }}{% endif %}{% if model_details %} ## Model Details{% if model_details.overview %} @@ -50,7 +50,7 @@ ### Citations {% for citation in model_details.citations %} * {{ citation.citation }}{% endfor %} -{% endif %}{% if model_parameters.model_architecture or model_parameters.input_format or model_parameters.output_format %} +{% endif %}{% endif %}{% if model_parameters.model_architecture or model_parameters.input_format or model_parameters.output_format %} ## Model Parameters {% if model_parameters.model_architecture %}