Skip to content

Commit

Permalink
[BUG] Fix 404 NOT FOUND issue caused by endpoint tail slash (#2721)
Browse files Browse the repository at this point in the history
* Fix 404 NOT FOUND BUG caused by tail slash

Signed-off-by: Mingqi Hu <mingqi.hu@intel.com>

* Update src/huggingface_hub/constants.py

Co-authored-by: Célina <hanouticelina@gmail.com>

* Update src/huggingface_hub/utils/_http.py

Co-authored-by: Célina <hanouticelina@gmail.com>

* Update src/huggingface_hub/utils/_http.py

Co-authored-by: Célina <hanouticelina@gmail.com>

* Update src/huggingface_hub/constants.py

Co-authored-by: Célina <hanouticelina@gmail.com>

* run style

---------

Signed-off-by: Mingqi Hu <mingqi.hu@intel.com>
Co-authored-by: Célina <hanouticelina@gmail.com>
  • Loading branch information
Mingqi2 and hanouticelina authored Jan 6, 2025
1 parent 126aded commit 438f2fb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/huggingface_hub/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import typing
from typing import Literal, Optional, Tuple
from urllib.parse import urljoin


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

_HF_DEFAULT_ENDPOINT = "https://huggingface.co"
_HF_DEFAULT_STAGING_ENDPOINT = "https://hub-ci.huggingface.co"
ENDPOINT = os.getenv("HF_ENDPOINT") or (_HF_DEFAULT_STAGING_ENDPOINT if _staging_mode else _HF_DEFAULT_ENDPOINT)
ENDPOINT = os.getenv("HF_ENDPOINT", "").rstrip("/") or (
_HF_DEFAULT_STAGING_ENDPOINT if _staging_mode else _HF_DEFAULT_ENDPOINT
)

HUGGINGFACE_CO_URL_TEMPLATE = ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}"
HUGGINGFACE_CO_URL_TEMPLATE = urljoin(ENDPOINT, "/{repo_id}/resolve/{revision}/{filename}")
HUGGINGFACE_HEADER_X_REPO_COMMIT = "X-Repo-Commit"
HUGGINGFACE_HEADER_X_LINKED_ETAG = "X-Linked-Etag"
HUGGINGFACE_HEADER_X_LINKED_SIZE = "X-Linked-Size"
Expand Down
4 changes: 2 additions & 2 deletions src/huggingface_hub/utils/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ def fix_hf_endpoint_in_url(url: str, endpoint: Optional[str]) -> str:
This is useful when using a proxy and the Hugging Face Hub returns a URL with the default endpoint.
"""
endpoint = endpoint or constants.ENDPOINT
endpoint = endpoint.rstrip("/") if endpoint else constants.ENDPOINT
# check if a proxy has been set => if yes, update the returned URL to use the proxy
if endpoint not in (None, constants._HF_DEFAULT_ENDPOINT, constants._HF_DEFAULT_STAGING_ENDPOINT):
if endpoint not in (constants._HF_DEFAULT_ENDPOINT, constants._HF_DEFAULT_STAGING_ENDPOINT):
url = url.replace(constants._HF_DEFAULT_ENDPOINT, endpoint)
url = url.replace(constants._HF_DEFAULT_STAGING_ENDPOINT, endpoint)
return url
Expand Down

0 comments on commit 438f2fb

Please sign in to comment.