Skip to content

Commit

Permalink
Detect nonexistence of README.md by EntryNotFoundError instead of lis…
Browse files Browse the repository at this point in the history
…ting files
  • Loading branch information
juhoinkinen committed Sep 23, 2024
1 parent 2b2d3ea commit d237aea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions annif/hfh_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ def upsert_modelcard(repo_id, projects, token, revision):
"""This function creates or updates a Model Card in a Hugging Face Hub repository
with some metadata in it."""
from huggingface_hub import ModelCard
from huggingface_hub.utils import EntryNotFoundError

card_exists = "README.md" in _list_files_in_hf_hub(repo_id, token, revision)
if card_exists:
try:
card = ModelCard.load(repo_id)
commit_message = "Update README.md with Annif"
else:
except EntryNotFoundError:
card = _create_modelcard(repo_id)
commit_message = "Create README.md with Annif"

Expand Down
15 changes: 8 additions & 7 deletions tests/test_hfh_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest import mock

import huggingface_hub
from huggingface_hub.utils import EntryNotFoundError

import annif.hfh_util
from annif.config import AnnifConfigCFG
Expand Down Expand Up @@ -106,12 +107,15 @@ def test_copy_project_config_overwrite(copy, exists):
)


@mock.patch("annif.hfh_util._list_files_in_hf_hub", return_value=[])
@mock.patch(
"huggingface_hub.ModelCard",
"huggingface_hub.ModelCard.load",
side_effect=EntryNotFoundError("dummymessage"),
)
@mock.patch("huggingface_hub.HfFileSystem.glob", return_value=[])
def test_upsert_modelcard_insert_new(glob, ModelCard, _list_files_in_hf_hub, project):
@mock.patch(
"huggingface_hub.ModelCard",
)
def test_upsert_modelcard_insert_new(ModelCard, glob, load, project):
repo_id = "annif-user/annif-repo"
project.vocab_lang = "fi"
projects = [project]
Expand All @@ -136,7 +140,6 @@ def test_upsert_modelcard_insert_new(glob, ModelCard, _list_files_in_hf_hub, pro


@mock.patch("huggingface_hub.ModelCard.push_to_hub")
@mock.patch("annif.hfh_util._list_files_in_hf_hub", return_value=["README.md"])
@mock.patch(
"huggingface_hub.ModelCard.load", return_value=huggingface_hub.ModelCard("foobar")
)
Expand All @@ -150,9 +153,7 @@ def test_upsert_modelcard_insert_new(glob, ModelCard, _list_files_in_hf_hub, pro
vocab=dummy
""",
)
def test_upsert_modelcard_update_existing(
read_text, glob, load, _list_files_in_hf_hub, push_to_hub, project
):
def test_upsert_modelcard_update_existing(read_text, glob, load, push_to_hub, project):
repo_id = "annif-user/annif-repo"
project.vocab_lang = "fi"
projects = [project]
Expand Down

0 comments on commit d237aea

Please sign in to comment.