Skip to content

Commit

Permalink
Use default model_name in metadata_update (#1157)
Browse files Browse the repository at this point in the history
* set default model name if none is provided

* Update src/huggingface_hub/repocard.py

Co-authored-by: Julien Chaumond <julien@huggingface.co>

* add tests

* update modelcard docstring

Co-authored-by: Julien Chaumond <julien@huggingface.co>
  • Loading branch information
lvwerra and julien-c authored Nov 8, 2022
1 parent 74bb506 commit 9735536
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/huggingface_hub/repocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@ def metadata_update(

for key, value in metadata.items():
if key == "model-index":
# if the new metadata doesn't include a name, either use existing one or repo name
if "name" not in value[0]:
value[0]["name"] = getattr(card, "model_name", repo_id)
model_name, new_results = model_index_to_eval_results(value)
if card.data.eval_results is None:
card.data.eval_results = new_results
Expand Down
7 changes: 4 additions & 3 deletions src/huggingface_hub/repocard_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ class ModelCardData(CardData):
at https://hf.co/metrics. Example: 'accuracy'. Defaults to None.
eval_results (`Union[List[EvalResult], EvalResult]`, *optional*):
List of `huggingface_hub.EvalResult` that define evaluation results of the model. If provided,
`model_name` kwarg must be provided. Defaults to `None`.
`model_name` is used to as a name on PapersWithCode's leaderboards. Defaults to `None`.
model_name (`str`, *optional*):
A name for this model. Required if you provide `eval_results`. It is used along with
A name for this model. It is used along with
`eval_results` to construct the `model-index` within the card's metadata. The name
you supply here is what will be used on PapersWithCode's leaderboards. Defaults to None.
you supply here is what will be used on PapersWithCode's leaderboards. If None is provided
then the repo name is used as a default. Defaults to None.
kwargs (`dict`, *optional*):
Additional metadata that will be added to the model card. Defaults to None.
Expand Down
44 changes: 44 additions & 0 deletions tests/test_repocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,50 @@ def test_update_metadata_on_empty_text_content(self) -> None:
expected_metadata = {"license": "cc-by-sa-4.0", "tag": "test"}
self.assertDictEqual(updated_metadata, expected_metadata)

def test_update_with_existing_name(self):
new_metadata = copy.deepcopy(self.existing_metadata)
new_metadata["model-index"][0].pop("name")
new_metadata["model-index"][0]["results"][0]["metrics"][0][
"value"
] = 0.2862102282047272

metadata_update(
f"{USER}/{self.REPO_NAME}",
new_metadata,
token=self._token,
overwrite=True,
)

card_data = ModelCard.load(f"{USER}/{self.REPO_NAME}", token=self._token)

self.assertEqual(
card_data.data.model_name, self.existing_metadata["model-index"][0]["name"]
)

def test_update_without_existing_name(self):

# delete existing metadata
self._api.upload_file(
path_or_fileobj="# Test".encode(),
repo_id=f"{USER}/{self.REPO_NAME}",
path_in_repo="README.md",
commit_message="Add README to main branch",
)

new_metadata = copy.deepcopy(self.existing_metadata)
new_metadata["model-index"][0].pop("name")

metadata_update(
f"{USER}/{self.REPO_NAME}",
new_metadata,
token=self._token,
overwrite=True,
)

card_data = ModelCard.load(f"{USER}/{self.REPO_NAME}", token=self._token)

self.assertEqual(card_data.data.model_name, f"{USER}/{self.REPO_NAME}")


class TestMetadataUpdateOnMissingCard(unittest.TestCase):
def setUp(self) -> None:
Expand Down

0 comments on commit 9735536

Please sign in to comment.