Skip to content

Commit ec5449f

Browse files
authored
Support both huggingface_hub v0.x and v1.x (#12389)
* Support huggingface_hub 0.x and 1.x * httpx
1 parent 310fdaf commit ec5449f

File tree

9 files changed

+24
-22
lines changed

9 files changed

+24
-22
lines changed

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
"filelock",
103103
"flax>=0.4.1",
104104
"hf-doc-builder>=0.3.0",
105-
"huggingface-hub>=0.34.0",
105+
"httpx<1.0.0",
106+
"huggingface-hub>=0.34.0,<2.0",
106107
"requests-mock==1.10.0",
107108
"importlib_metadata",
108109
"invisible-watermark>=0.2.0",
@@ -259,6 +260,7 @@ def run(self):
259260
install_requires = [
260261
deps["importlib_metadata"],
261262
deps["filelock"],
263+
deps["httpx"],
262264
deps["huggingface-hub"],
263265
deps["numpy"],
264266
deps["regex"],

src/diffusers/configuration_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
from huggingface_hub import DDUFEntry, create_repo, hf_hub_download
3131
from huggingface_hub.utils import (
3232
EntryNotFoundError,
33+
HfHubHTTPError,
3334
RepositoryNotFoundError,
3435
RevisionNotFoundError,
3536
validate_hf_hub_args,
3637
)
37-
from requests import HTTPError
3838
from typing_extensions import Self
3939

4040
from . import __version__
@@ -419,7 +419,7 @@ def load_config(
419419
raise EnvironmentError(
420420
f"{pretrained_model_name_or_path} does not appear to have a file named {cls.config_name}."
421421
)
422-
except HTTPError as err:
422+
except HfHubHTTPError as err:
423423
raise EnvironmentError(
424424
"There was a specific connection error when trying to load"
425425
f" {pretrained_model_name_or_path}:\n{err}"

src/diffusers/dependency_versions_table.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"filelock": "filelock",
1010
"flax": "flax>=0.4.1",
1111
"hf-doc-builder": "hf-doc-builder>=0.3.0",
12-
"huggingface-hub": "huggingface-hub>=0.34.0",
12+
"httpx": "httpx<1.0.0",
13+
"huggingface-hub": "huggingface-hub>=0.34.0,<2.0",
1314
"requests-mock": "requests-mock==1.10.0",
1415
"importlib_metadata": "importlib_metadata",
1516
"invisible-watermark": "invisible-watermark>=0.2.0",

src/diffusers/models/modeling_flax_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
from huggingface_hub import create_repo, hf_hub_download
2727
from huggingface_hub.utils import (
2828
EntryNotFoundError,
29+
HfHubHTTPError,
2930
RepositoryNotFoundError,
3031
RevisionNotFoundError,
3132
validate_hf_hub_args,
3233
)
33-
from requests import HTTPError
3434

3535
from .. import __version__, is_torch_available
3636
from ..utils import (
@@ -385,7 +385,7 @@ def from_pretrained(
385385
raise EnvironmentError(
386386
f"{pretrained_model_name_or_path} does not appear to have a file named {FLAX_WEIGHTS_NAME}."
387387
)
388-
except HTTPError as err:
388+
except HfHubHTTPError as err:
389389
raise EnvironmentError(
390390
f"There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n"
391391
f"{err}"

src/diffusers/pipelines/pipeline_loading_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
from pathlib import Path
2020
from typing import Any, Callable, Dict, List, Optional, Union
2121

22+
import httpx
2223
import requests
2324
import torch
2425
from huggingface_hub import DDUFEntry, ModelCard, model_info, snapshot_download
25-
from huggingface_hub.utils import OfflineModeIsEnabled, validate_hf_hub_args
26+
from huggingface_hub.utils import HfHubHTTPError, OfflineModeIsEnabled, validate_hf_hub_args
2627
from packaging import version
27-
from requests.exceptions import HTTPError
2828

2929
from .. import __version__
3030
from ..utils import (
@@ -1110,7 +1110,7 @@ def _download_dduf_file(
11101110
if not local_files_only:
11111111
try:
11121112
info = model_info(pretrained_model_name, token=token, revision=revision)
1113-
except (HTTPError, OfflineModeIsEnabled, requests.ConnectionError) as e:
1113+
except (HfHubHTTPError, OfflineModeIsEnabled, requests.ConnectionError, httpx.NetworkError) as e:
11141114
logger.warning(f"Couldn't connect to the Hub: {e}.\nWill try to load from local cache.")
11151115
local_files_only = True
11161116
model_info_call_error = e # save error to reraise it if model is not cached locally

src/diffusers/pipelines/pipeline_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pathlib import Path
2424
from typing import Any, Callable, Dict, List, Optional, Union, get_args, get_origin
2525

26+
import httpx
2627
import numpy as np
2728
import PIL.Image
2829
import requests
@@ -36,9 +37,8 @@
3637
read_dduf_file,
3738
snapshot_download,
3839
)
39-
from huggingface_hub.utils import OfflineModeIsEnabled, validate_hf_hub_args
40+
from huggingface_hub.utils import HfHubHTTPError, OfflineModeIsEnabled, validate_hf_hub_args
4041
from packaging import version
41-
from requests.exceptions import HTTPError
4242
from tqdm.auto import tqdm
4343
from typing_extensions import Self
4444

@@ -1616,7 +1616,7 @@ def download(cls, pretrained_model_name, **kwargs) -> Union[str, os.PathLike]:
16161616
if not local_files_only:
16171617
try:
16181618
info = model_info(pretrained_model_name, token=token, revision=revision)
1619-
except (HTTPError, OfflineModeIsEnabled, requests.ConnectionError) as e:
1619+
except (HfHubHTTPError, OfflineModeIsEnabled, requests.ConnectionError, httpx.NetworkError) as e:
16201620
logger.warning(f"Couldn't connect to the Hub: {e}.\nWill try to load from local cache.")
16211621
local_files_only = True
16221622
model_info_call_error = e # save error to reraise it if model is not cached locally

src/diffusers/utils/hub_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
from huggingface_hub.file_download import REGEX_COMMIT_HASH
3939
from huggingface_hub.utils import (
4040
EntryNotFoundError,
41+
HfHubHTTPError,
4142
RepositoryNotFoundError,
4243
RevisionNotFoundError,
4344
is_jinja_available,
4445
validate_hf_hub_args,
4546
)
4647
from packaging import version
47-
from requests import HTTPError
4848

4949
from .. import __version__
5050
from .constants import (
@@ -316,7 +316,7 @@ def _get_model_file(
316316
raise EnvironmentError(
317317
f"{pretrained_model_name_or_path} does not appear to have a file named {weights_name}."
318318
) from e
319-
except HTTPError as e:
319+
except HfHubHTTPError as e:
320320
raise EnvironmentError(
321321
f"There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{e}"
322322
) from e
@@ -432,7 +432,7 @@ def _get_checkpoint_shard_files(
432432

433433
# We have already dealt with RepositoryNotFoundError and RevisionNotFoundError when getting the index, so
434434
# we don't have to catch them here. We have also dealt with EntryNotFoundError.
435-
except HTTPError as e:
435+
except HfHubHTTPError as e:
436436
raise EnvironmentError(
437437
f"We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load {pretrained_model_name_or_path}. You should try"
438438
" again after checking your internet connection."

tests/models/test_modeling_common.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@
3737
import torch.nn as nn
3838
from accelerate.utils.modeling import _get_proper_dtype, compute_module_sizes, dtype_byte_size
3939
from huggingface_hub import ModelCard, delete_repo, snapshot_download, try_to_load_from_cache
40-
from huggingface_hub.utils import is_jinja_available
40+
from huggingface_hub.utils import HfHubHTTPError, is_jinja_available
4141
from parameterized import parameterized
42-
from requests.exceptions import HTTPError
4342

4443
from diffusers.models import FluxTransformer2DModel, SD3Transformer2DModel, UNet2DConditionModel
4544
from diffusers.models.attention_processor import (
@@ -272,7 +271,7 @@ def test_cached_files_are_used_when_no_internet(self):
272271
response_mock = mock.Mock()
273272
response_mock.status_code = 500
274273
response_mock.headers = {}
275-
response_mock.raise_for_status.side_effect = HTTPError
274+
response_mock.raise_for_status.side_effect = HfHubHTTPError("Server down", response=mock.Mock())
276275
response_mock.json.return_value = {}
277276

278277
# Download this model to make sure it's in the cache.
@@ -296,7 +295,7 @@ def test_local_files_only_with_sharded_checkpoint(self):
296295
error_response = mock.Mock(
297296
status_code=500,
298297
headers={},
299-
raise_for_status=mock.Mock(side_effect=HTTPError),
298+
raise_for_status=mock.Mock(side_effect=HfHubHTTPError("Server down", response=mock.Mock())),
300299
json=mock.Mock(return_value={}),
301300
)
302301

tests/pipelines/test_pipelines.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
import torch
3434
import torch.nn as nn
3535
from huggingface_hub import snapshot_download
36+
from huggingface_hub.utils import HfHubHTTPError
3637
from parameterized import parameterized
3738
from PIL import Image
38-
from requests.exceptions import HTTPError
3939
from transformers import CLIPImageProcessor, CLIPModel, CLIPTextConfig, CLIPTextModel, CLIPTokenizer
4040

4141
from diffusers import (
@@ -430,7 +430,7 @@ def test_cached_files_are_used_when_no_internet(self):
430430
response_mock = mock.Mock()
431431
response_mock.status_code = 500
432432
response_mock.headers = {}
433-
response_mock.raise_for_status.side_effect = HTTPError
433+
response_mock.raise_for_status.side_effect = HfHubHTTPError("Server down", response=mock.Mock())
434434
response_mock.json.return_value = {}
435435

436436
# Download this model to make sure it's in the cache.
@@ -457,7 +457,7 @@ def test_local_files_only_are_used_when_no_internet(self):
457457
response_mock = mock.Mock()
458458
response_mock.status_code = 500
459459
response_mock.headers = {}
460-
response_mock.raise_for_status.side_effect = HTTPError
460+
response_mock.raise_for_status.side_effect = HfHubHTTPError("Server down", response=mock.Mock())
461461
response_mock.json.return_value = {}
462462

463463
# first check that with local files only the pipeline can only be used if cached

0 commit comments

Comments
 (0)