Skip to content

Commit 60f33b9

Browse files
committed
Enhance repo_type_and_id_from_hf_id of hf_api
1 parent 1a39248 commit 60f33b9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/huggingface_hub/hf_api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,14 @@ def repo_type_and_id_from_hf_id(hf_id: str, hub_url: Optional[str] = None) -> tu
236236
"""
237237
input_hf_id = hf_id
238238

239-
hub_url = re.sub(r"https?://", "", hub_url if hub_url is not None else constants.ENDPOINT)
240-
is_hf_url = hub_url in hf_id and "@" not in hf_id
239+
hub_url = hub_url if hub_url is not None else constants.ENDPOINT
240+
hub_url = hub_url.rstrip("/")
241+
if hf_id.startswith(hub_url):
242+
hf_id = hf_id[len(hub_url):].lstrip("/")
243+
elif hf_id.startswith(hub_url.replace("https://", "").replace("http://", "")):
244+
# Handle urls like "localhost:8080/hf/model/xxx"
245+
# https://github.com/huggingface/huggingface_hub/issues/3494
246+
hf_id = hf_id[len(hub_url.replace("https://", "").replace("http://", "")):].lstrip("/")
241247

242248
HFFS_PREFIX = "hf://"
243249
if hf_id.startswith(HFFS_PREFIX): # Remove "hf://" prefix if exists

tests/test_hf_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,9 @@ def test_git_push_end_to_end(self):
27602760
class ParseHFUrlTest(unittest.TestCase):
27612761
def test_repo_type_and_id_from_hf_id_on_correct_values(self):
27622762
possible_values = {
2763+
"http://localhost:8080/hf/user/id": [None, "user", "id"],
2764+
"http://localhost:8080/hf/datasets/user/id": ["dataset", "user", "id"],
2765+
"http://localhost:8080/hf/models/user/id": ["model", "user", "id"],
27632766
"https://huggingface.co/id": [None, None, "id"],
27642767
"https://huggingface.co/user/id": [None, "user", "id"],
27652768
"https://huggingface.co/datasets/user/id": ["dataset", "user", "id"],

0 commit comments

Comments
 (0)