Skip to content

Commit 438f2fb

Browse files
[BUG] Fix 404 NOT FOUND issue caused by endpoint tail slash (#2721)
* Fix 404 NOT FOUND BUG caused by tail slash Signed-off-by: Mingqi Hu <[email protected]> * Update src/huggingface_hub/constants.py Co-authored-by: Célina <[email protected]> * Update src/huggingface_hub/utils/_http.py Co-authored-by: Célina <[email protected]> * Update src/huggingface_hub/utils/_http.py Co-authored-by: Célina <[email protected]> * Update src/huggingface_hub/constants.py Co-authored-by: Célina <[email protected]> * run style --------- Signed-off-by: Mingqi Hu <[email protected]> Co-authored-by: Célina <[email protected]>
1 parent 126aded commit 438f2fb

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/huggingface_hub/constants.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import typing
44
from typing import Literal, Optional, Tuple
5+
from urllib.parse import urljoin
56

67

78
# Possible values for env variables
@@ -63,9 +64,11 @@ def _as_int(value: Optional[str]) -> Optional[int]:
6364

6465
_HF_DEFAULT_ENDPOINT = "https://huggingface.co"
6566
_HF_DEFAULT_STAGING_ENDPOINT = "https://hub-ci.huggingface.co"
66-
ENDPOINT = os.getenv("HF_ENDPOINT") or (_HF_DEFAULT_STAGING_ENDPOINT if _staging_mode else _HF_DEFAULT_ENDPOINT)
67+
ENDPOINT = os.getenv("HF_ENDPOINT", "").rstrip("/") or (
68+
_HF_DEFAULT_STAGING_ENDPOINT if _staging_mode else _HF_DEFAULT_ENDPOINT
69+
)
6770

68-
HUGGINGFACE_CO_URL_TEMPLATE = ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}"
71+
HUGGINGFACE_CO_URL_TEMPLATE = urljoin(ENDPOINT, "/{repo_id}/resolve/{revision}/{filename}")
6972
HUGGINGFACE_HEADER_X_REPO_COMMIT = "X-Repo-Commit"
7073
HUGGINGFACE_HEADER_X_LINKED_ETAG = "X-Linked-Etag"
7174
HUGGINGFACE_HEADER_X_LINKED_SIZE = "X-Linked-Size"

src/huggingface_hub/utils/_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ def fix_hf_endpoint_in_url(url: str, endpoint: Optional[str]) -> str:
338338
339339
This is useful when using a proxy and the Hugging Face Hub returns a URL with the default endpoint.
340340
"""
341-
endpoint = endpoint or constants.ENDPOINT
341+
endpoint = endpoint.rstrip("/") if endpoint else constants.ENDPOINT
342342
# check if a proxy has been set => if yes, update the returned URL to use the proxy
343-
if endpoint not in (None, constants._HF_DEFAULT_ENDPOINT, constants._HF_DEFAULT_STAGING_ENDPOINT):
343+
if endpoint not in (constants._HF_DEFAULT_ENDPOINT, constants._HF_DEFAULT_STAGING_ENDPOINT):
344344
url = url.replace(constants._HF_DEFAULT_ENDPOINT, endpoint)
345345
url = url.replace(constants._HF_DEFAULT_STAGING_ENDPOINT, endpoint)
346346
return url

0 commit comments

Comments
 (0)