Repository: huggingface/huggingface_hub

Issue:
Error: processor.push_to_hub Fails with IsADirectoryError When Pushing to Hugging Face Hub
Hello,

When I run the following code, I encounter an error:
```
from transformers import ViltProcessor  

processor = ViltProcessor.from_pretrained(model_checkpoint)  
processor.push_to_hub("muhammadfhadli/vilt-b32-finetuned-vqa-hse-v0.0.1", private=True)  
```
The traceback is as follows:
```
Traceback (most recent call last):  
  File "<string>", line 1, in <module>  
  File "/home/user/.pyenv/versions/vqa-train/lib/python3.10/site-packages/transformers/utils/hub.py", line 938, in push_to_hub  
    model_card = create_and_tag_model_card(  
  File "/home/user/.pyenv/versions/vqa-train/lib/python3.10/site-packages/transformers/utils/hub.py", line 1193, in create_and_tag_model_card  
    model_card = ModelCard.load(repo_id, token=token, ignore_metadata_errors=ignore_metadata_errors)  
  File "/home/user/.pyenv/versions/vqa-train/lib/python3.10/site-packages/huggingface_hub/repocard.py", line 189, in load  
    with card_path.open(mode="r", newline="", encoding="utf-8") as f:  
  File "/home/user/.pyenv/versions/3.10.12/lib/python3.10/pathlib.py", line 1119, in open  
    return self._accessor.open(self, mode, buffering, encoding, errors,  
IsADirectoryError: [Errno 21] Is a directory: 'muhammadfhadli/vilt-b32-finetuned-vqa-hse-v0.0.1' 
```

It seems like this line is referring to a directory instead of the README.md file:
https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/repocard.py#L175

Here are the package versions I’m using:
- Python 3.10.12
- transformers==4.48.1
- huggingface-hub==0.27.1

Could you please clarify if this is the intended behavior or if it might be a bug?

Thank you!

Hints from issue comments:
@Wauplin: Hi @muhammadfhadli1453 sorry you're facing this issue. This is indeed a bug happening because `"muhammadfhadli/vilt-b32-finetuned-vqa-hse-v0.0.1"` is both a local directory and a repo_id. I opened https://github.com/huggingface/huggingface_hub/pull/2771 to fix it. Temporary solution would be to rename your local folder so that naming doesn't conflict.

Solution patch:
diff --git a/src/huggingface_hub/repocard.py b/src/huggingface_hub/repocard.py
index 2daefbb5..83b22b2b 100644
--- a/src/huggingface_hub/repocard.py
+++ b/src/huggingface_hub/repocard.py
@@ -171,7 +171,7 @@ class RepoCard:
             ```
         """
 
-        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(


Test patch:
diff --git a/tests/test_repocard.py b/tests/test_repocard.py
index 71f60aba..d9408901 100644
--- a/tests/test_repocard.py
+++ b/tests/test_repocard.py
@@ -44,15 +44,8 @@ from huggingface_hub.repocard import REGEX_YAML_BLOCK
 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"
@@ -271,6 +264,23 @@ class RepocardMetadataTest(unittest.TestCase):
         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