Skip to content

Commit

Permalink
Fix RepoCard.load when passing a repo_id that is also a dir path (#2771)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wauplin authored Jan 23, 2025
1 parent b0a42ad commit b7abb35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/huggingface_hub/repocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def load(
```
"""

if Path(repo_id_or_path).exists():
if Path(repo_id_or_path).is_file():
card_path = Path(repo_id_or_path)
elif isinstance(repo_id_or_path, str):
card_path = Path(
Expand Down
28 changes: 19 additions & 9 deletions tests/test_repocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,8 @@
from huggingface_hub.repocard_data import CardData
from huggingface_hub.utils import SoftTemporaryDirectory, is_jinja_available

from .testing_constants import (
ENDPOINT_STAGING,
TOKEN,
USER,
)
from .testing_utils import (
repo_name,
with_production_testing,
)
from .testing_constants import ENDPOINT_STAGING, TOKEN, USER
from .testing_utils import repo_name, with_production_testing


SAMPLE_CARDS_DIR = Path(__file__).parent / "fixtures/cards"
Expand Down Expand Up @@ -271,6 +264,23 @@ def test_metadata_eval_result(self):
self.assertEqual(content, DUMMY_MODELCARD_EVAL_RESULT.splitlines())


@with_production_testing
def test_load_from_hub_if_repo_id_or_path_is_a_dir(monkeypatch):
"""If `repo_id_or_path` happens to be both a `repo_id` and a local directory, the card must be loaded from the Hub.
Path can only be a file path.
Regression test for https://github.com/huggingface/huggingface_hub/issues/2768.
"""
with SoftTemporaryDirectory() as tmpdir:
monkeypatch.chdir(tmpdir)
repo_id = "openai-community/gpt2"
(Path(tmpdir) / "openai-community" / "gpt2").mkdir(parents=True)

card = RepoCard.load(repo_id)
assert "GPT-2" in str(card) # loaded from Hub


class RepocardMetadataUpdateTest(unittest.TestCase):
def setUp(self) -> None:
self.token = TOKEN
Expand Down

0 comments on commit b7abb35

Please sign in to comment.