Skip to content

Commit b7abb35

Browse files
authored
Fix RepoCard.load when passing a repo_id that is also a dir path (#2771)
1 parent b0a42ad commit b7abb35

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/huggingface_hub/repocard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def load(
171171
```
172172
"""
173173

174-
if Path(repo_id_or_path).exists():
174+
if Path(repo_id_or_path).is_file():
175175
card_path = Path(repo_id_or_path)
176176
elif isinstance(repo_id_or_path, str):
177177
card_path = Path(

tests/test_repocard.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,8 @@
4444
from huggingface_hub.repocard_data import CardData
4545
from huggingface_hub.utils import SoftTemporaryDirectory, is_jinja_available
4646

47-
from .testing_constants import (
48-
ENDPOINT_STAGING,
49-
TOKEN,
50-
USER,
51-
)
52-
from .testing_utils import (
53-
repo_name,
54-
with_production_testing,
55-
)
47+
from .testing_constants import ENDPOINT_STAGING, TOKEN, USER
48+
from .testing_utils import repo_name, with_production_testing
5649

5750

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

273266

267+
@with_production_testing
268+
def test_load_from_hub_if_repo_id_or_path_is_a_dir(monkeypatch):
269+
"""If `repo_id_or_path` happens to be both a `repo_id` and a local directory, the card must be loaded from the Hub.
270+
271+
Path can only be a file path.
272+
273+
Regression test for https://github.com/huggingface/huggingface_hub/issues/2768.
274+
"""
275+
with SoftTemporaryDirectory() as tmpdir:
276+
monkeypatch.chdir(tmpdir)
277+
repo_id = "openai-community/gpt2"
278+
(Path(tmpdir) / "openai-community" / "gpt2").mkdir(parents=True)
279+
280+
card = RepoCard.load(repo_id)
281+
assert "GPT-2" in str(card) # loaded from Hub
282+
283+
274284
class RepocardMetadataUpdateTest(unittest.TestCase):
275285
def setUp(self) -> None:
276286
self.token = TOKEN

0 commit comments

Comments
 (0)