Skip to content

Commit

Permalink
[cleanup] Unique constants in tests + env variable for inference tests (
Browse files Browse the repository at this point in the history
#2855)

* Unique constants in tests + remove deprecated old token path + env variable for inference tests

* fix tests

* fix test
  • Loading branch information
Wauplin authored Feb 13, 2025
1 parent b19ab11 commit 0c1e566
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 48 deletions.
17 changes: 8 additions & 9 deletions src/huggingface_hub/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ 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", "").rstrip("/") or (
_HF_DEFAULT_STAGING_ENDPOINT if _staging_mode else _HF_DEFAULT_ENDPOINT
)

ENDPOINT = os.getenv("HF_ENDPOINT", _HF_DEFAULT_ENDPOINT).rstrip("/")
HUGGINGFACE_CO_URL_TEMPLATE = ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}"

if _staging_mode:
ENDPOINT = _HF_DEFAULT_STAGING_ENDPOINT
HUGGINGFACE_CO_URL_TEMPLATE = _HF_DEFAULT_STAGING_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 Expand Up @@ -143,18 +145,15 @@ def _as_int(value: Optional[str]) -> Optional[int]:
or _is_true(os.environ.get("DO_NOT_TRACK")) # https://consoledonottrack.com/
)

# In the past, token was stored in a hardcoded location
# `_OLD_HF_TOKEN_PATH` is deprecated and will be removed "at some point".
# See https://github.com/huggingface/huggingface_hub/issues/1232
_OLD_HF_TOKEN_PATH = os.path.expanduser("~/.huggingface/token")
HF_TOKEN_PATH = os.environ.get("HF_TOKEN_PATH", os.path.join(HF_HOME, "token"))
HF_STORED_TOKENS_PATH = os.path.join(os.path.dirname(HF_TOKEN_PATH), "stored_tokens")

if _staging_mode:
# In staging mode, we use a different cache to ensure we don't mix up production and staging data or tokens
# In practice in `huggingface_hub` tests, we monkeypatch these values with temporary directories. The following
# lines are only used in third-party libraries tests (e.g. `transformers`, `diffusers`, etc.).
_staging_home = os.path.join(os.path.expanduser("~"), ".cache", "huggingface_staging")
HUGGINGFACE_HUB_CACHE = os.path.join(_staging_home, "hub")
_OLD_HF_TOKEN_PATH = os.path.join(_staging_home, "_old_token")
HF_TOKEN_PATH = os.path.join(_staging_home, "token")

# Here, `True` will disable progress bars globally without possibility of enabling it
Expand Down
36 changes: 4 additions & 32 deletions src/huggingface_hub/utils/_hf_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.
"""Contain helper class to retrieve/store token from/to local cache."""

import warnings
from pathlib import Path
from typing import Optional

Expand All @@ -23,10 +22,6 @@


class HfFolder:
path_token = Path(constants.HF_TOKEN_PATH)
# Private attribute. Will be removed in v0.15
_old_path_token = Path(constants._OLD_HF_TOKEN_PATH)

# TODO: deprecate when adapted in transformers/datasets/gradio
# @_deprecate_method(version="1.0", message="Use `huggingface_hub.login` instead.")
@classmethod
Expand All @@ -41,8 +36,9 @@ def save_token(cls, token: str) -> None:
token (`str`):
The token to save to the [`HfFolder`]
"""
cls.path_token.parent.mkdir(parents=True, exist_ok=True)
cls.path_token.write_text(token)
path_token = Path(constants.HF_TOKEN_PATH)
path_token.parent.mkdir(parents=True, exist_ok=True)
path_token.write_text(token)

# TODO: deprecate when adapted in transformers/datasets/gradio
# @_deprecate_method(version="1.0", message="Use `huggingface_hub.get_token` instead.")
Expand All @@ -57,12 +53,6 @@ def get_token(cls) -> Optional[str]:
Returns:
`str` or `None`: The token, `None` if it doesn't exist.
"""
# 0. Check if token exist in old path but not new location
try:
cls._copy_to_new_path_and_warn()
except Exception: # if not possible (e.g. PermissionError), do not raise
pass

return get_token()

# TODO: deprecate when adapted in transformers/datasets/gradio
Expand All @@ -73,24 +63,6 @@ def delete_token(cls) -> None:
Deletes the token from storage. Does not fail if token does not exist.
"""
try:
cls.path_token.unlink()
except FileNotFoundError:
pass

try:
cls._old_path_token.unlink()
Path(constants.HF_TOKEN_PATH).unlink()
except FileNotFoundError:
pass

@classmethod
def _copy_to_new_path_and_warn(cls):
if cls._old_path_token.exists() and not cls.path_token.exists():
cls.save_token(cls._old_path_token.read_text())
warnings.warn(
f"A token has been found in `{cls._old_path_token}`. This is the old"
" path where tokens were stored. The new location is"
f" `{cls.path_token}` which is configurable using `HF_HOME` environment"
" variable. Your token has been copied to this new location. You can"
" now safely delete the old token file manually or use"
" `huggingface-cli logout`."
)
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@
from _pytest.fixtures import SubRequest

import huggingface_hub
from huggingface_hub import constants
from huggingface_hub.utils import SoftTemporaryDirectory, logging

from .testing_utils import set_write_permission_and_retry


@pytest.fixture(autouse=True, scope="function")
def patch_constants(mocker):
with SoftTemporaryDirectory() as cache_dir:
mocker.patch.object(constants, "HF_HOME", cache_dir)
mocker.patch.object(constants, "HF_HUB_CACHE", os.path.join(cache_dir, "hub"))
mocker.patch.object(constants, "HUGGINGFACE_HUB_CACHE", os.path.join(cache_dir, "hub"))
mocker.patch.object(constants, "HF_ASSETS_CACHE", os.path.join(cache_dir, "assets"))
mocker.patch.object(constants, "HF_TOKEN_PATH", os.path.join(cache_dir, "token"))
mocker.patch.object(constants, "HF_STORED_TOKENS_PATH", os.path.join(cache_dir, "stored_tokens"))
yield


logger = logging.get_logger(__name__)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_inference_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@
},
}
API_KEY_ENV_VARIABLES = {
"hf-inference": "HF_TOKEN",
"hf-inference": "HF_INFERENCE_TEST_TOKEN",
"fal-ai": "FAL_AI_KEY",
"fireworks-ai": "HF_TOKEN",
"fireworks-ai": "HF_INFERENCE_TEST_TOKEN",
"replicate": "REPLICATE_KEY",
"sambanova": "SAMBANOVA_API_KEY",
"together": "TOGETHER_API_KEY",
Expand Down
6 changes: 1 addition & 5 deletions tests/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
import pytest
import requests

from huggingface_hub.utils import (
is_package_available,
logging,
reset_sessions,
)
from huggingface_hub.utils import is_package_available, logging, reset_sessions
from tests.testing_constants import ENDPOINT_PRODUCTION, ENDPOINT_PRODUCTION_URL_SCHEME


Expand Down

0 comments on commit 0c1e566

Please sign in to comment.