|
5 | 5 | import os |
6 | 6 | import re |
7 | 7 | import shutil |
8 | | -import sys |
9 | 8 | import tempfile |
10 | 9 | import warnings |
11 | 10 | from contextlib import contextmanager |
|
16 | 15 | from typing import BinaryIO, Dict, Optional, Tuple, Union |
17 | 16 | from urllib.parse import quote, urlparse |
18 | 17 |
|
19 | | -import packaging.version |
20 | | - |
21 | 18 | import requests |
22 | 19 | from filelock import FileLock |
23 | 20 | from huggingface_hub import constants |
24 | 21 | from requests.exceptions import ConnectTimeout, ProxyError |
25 | 22 |
|
26 | | -from . import __version__ |
| 23 | +from . import __version__ # noqa: F401 # for backward compatibility |
27 | 24 | from .constants import ( |
28 | 25 | DEFAULT_REVISION, |
29 | 26 | HUGGINGFACE_CO_URL_TEMPLATE, |
|
34 | 31 | REPO_TYPES, |
35 | 32 | REPO_TYPES_URL_PREFIXES, |
36 | 33 | ) |
| 34 | +from .utils import get_fastai_version # noqa: F401 # for backward compatibility |
| 35 | +from .utils import get_fastcore_version # noqa: F401 # for backward compatibility |
| 36 | +from .utils import get_graphviz_version # noqa: F401 # for backward compatibility |
| 37 | +from .utils import get_jinja_version # noqa: F401 # for backward compatibility |
| 38 | +from .utils import get_pydot_version # noqa: F401 # for backward compatibility |
| 39 | +from .utils import get_tf_version # noqa: F401 # for backward compatibility |
| 40 | +from .utils import get_torch_version # noqa: F401 # for backward compatibility |
| 41 | +from .utils import is_fastai_available # noqa: F401 # for backward compatibility |
| 42 | +from .utils import is_fastcore_available # noqa: F401 # for backward compatibility |
| 43 | +from .utils import is_graphviz_available # noqa: F401 # for backward compatibility |
| 44 | +from .utils import is_jinja_available # noqa: F401 # for backward compatibility |
| 45 | +from .utils import is_pydot_available # noqa: F401 # for backward compatibility |
| 46 | +from .utils import is_tf_available # noqa: F401 # for backward compatibility |
| 47 | +from .utils import is_torch_available # noqa: F401 # for backward compatibility |
37 | 48 | from .utils import ( |
38 | 49 | EntryNotFoundError, |
39 | 50 | LocalEntryNotFoundError, |
|
44 | 55 | tqdm, |
45 | 56 | validate_hf_hub_args, |
46 | 57 | ) |
| 58 | +from .utils._headers import _http_user_agent |
| 59 | +from .utils._runtime import _PY_VERSION # noqa: F401 # for backward compatibility |
47 | 60 |
|
48 | 61 |
|
49 | 62 | logger = logging.get_logger(__name__) |
50 | 63 |
|
51 | | -_PY_VERSION: str = sys.version.split()[0].rstrip("+") |
52 | | - |
53 | | -if packaging.version.Version(_PY_VERSION) < packaging.version.Version("3.8.0"): |
54 | | - import importlib_metadata |
55 | | -else: |
56 | | - import importlib.metadata as importlib_metadata |
57 | | - |
58 | | -_torch_version = "N/A" |
59 | | -_torch_available = False |
60 | | -try: |
61 | | - _torch_version = importlib_metadata.version("torch") |
62 | | - _torch_available = True |
63 | | -except importlib_metadata.PackageNotFoundError: |
64 | | - pass |
65 | | - |
66 | | -_pydot_available = False |
67 | | - |
68 | | -try: |
69 | | - _pydot_version = importlib_metadata.version("pydot") |
70 | | - _pydot_available = True |
71 | | -except importlib_metadata.PackageNotFoundError: |
72 | | - pass |
73 | | - |
74 | | - |
75 | | -def is_pydot_available(): |
76 | | - return _pydot_available |
77 | | - |
78 | | - |
79 | | -_graphviz_available = False |
80 | | - |
81 | | -try: |
82 | | - _graphviz_version = importlib_metadata.version("graphviz") |
83 | | - _graphviz_available = True |
84 | | -except importlib_metadata.PackageNotFoundError: |
85 | | - pass |
86 | | - |
87 | | - |
88 | | -def is_graphviz_available(): |
89 | | - return _graphviz_available |
90 | | - |
91 | | - |
92 | | -_tf_version = "N/A" |
93 | | -_tf_available = False |
94 | | -_tf_candidates = ( |
95 | | - "tensorflow", |
96 | | - "tensorflow-cpu", |
97 | | - "tensorflow-gpu", |
98 | | - "tf-nightly", |
99 | | - "tf-nightly-cpu", |
100 | | - "tf-nightly-gpu", |
101 | | - "intel-tensorflow", |
102 | | - "intel-tensorflow-avx512", |
103 | | - "tensorflow-rocm", |
104 | | - "tensorflow-macos", |
105 | | -) |
106 | | -for package_name in _tf_candidates: |
107 | | - try: |
108 | | - _tf_version = importlib_metadata.version(package_name) |
109 | | - _tf_available = True |
110 | | - break |
111 | | - except importlib_metadata.PackageNotFoundError: |
112 | | - pass |
113 | | - |
114 | | -_fastai_version = "N/A" |
115 | | -_fastai_available = False |
116 | | -try: |
117 | | - _fastai_version: str = importlib_metadata.version("fastai") |
118 | | - _fastai_available = True |
119 | | -except importlib_metadata.PackageNotFoundError: |
120 | | - pass |
121 | | - |
122 | | -_fastcore_version = "N/A" |
123 | | -_fastcore_available = False |
124 | | -try: |
125 | | - _fastcore_version: str = importlib_metadata.version("fastcore") |
126 | | - _fastcore_available = True |
127 | | -except importlib_metadata.PackageNotFoundError: |
128 | | - pass |
129 | | - |
130 | | -_jinja_version = "N/A" |
131 | | -_jinja_available = False |
132 | | -try: |
133 | | - _jinja_version: str = importlib_metadata.version("Jinja2") |
134 | | - _jinja_available = True |
135 | | -except importlib_metadata.PackageNotFoundError: |
136 | | - pass |
137 | | - |
138 | | - |
139 | | -def is_torch_available(): |
140 | | - return _torch_available |
141 | | - |
142 | | - |
143 | | -def is_tf_available(): |
144 | | - return _tf_available |
145 | | - |
146 | | - |
147 | | -def get_tf_version(): |
148 | | - return _tf_version |
149 | | - |
150 | | - |
151 | | -def is_fastai_available(): |
152 | | - return _fastai_available |
153 | | - |
154 | | - |
155 | | -def get_fastai_version(): |
156 | | - return _fastai_version |
157 | | - |
158 | | - |
159 | | -def is_fastcore_available(): |
160 | | - return _fastcore_available |
161 | | - |
162 | | - |
163 | | -def get_fastcore_version(): |
164 | | - return _fastcore_version |
165 | | - |
166 | | - |
167 | | -def is_jinja_available(): |
168 | | - return _jinja_available |
169 | | - |
170 | | - |
171 | | -def get_jinja_version(): |
172 | | - return _jinja_version |
173 | | - |
174 | 64 |
|
175 | 65 | _are_symlinks_supported_in_dir: Dict[str, bool] = {} |
176 | 66 |
|
@@ -432,38 +322,12 @@ def http_user_agent( |
432 | 322 | library_version: Optional[str] = None, |
433 | 323 | user_agent: Union[Dict, str, None] = None, |
434 | 324 | ) -> str: |
435 | | - """Formats a user-agent string with basic info about a request. |
436 | | -
|
437 | | - Args: |
438 | | - library_name (`str`, *optional*): |
439 | | - The name of the library to which the object corresponds. |
440 | | - library_version (`str`, *optional*): |
441 | | - The version of the library. |
442 | | - user_agent (`str`, `dict`, *optional*): |
443 | | - The user agent info in the form of a dictionary or a single string. |
444 | | -
|
445 | | - Returns: |
446 | | - The formatted user-agent string. |
447 | | - """ |
448 | | - if library_name is not None: |
449 | | - ua = f"{library_name}/{library_version}" |
450 | | - else: |
451 | | - ua = "unknown/None" |
452 | | - ua += f"; hf_hub/{__version__}" |
453 | | - ua += f"; python/{_PY_VERSION}" |
454 | | - if is_torch_available(): |
455 | | - ua += f"; torch/{_torch_version}" |
456 | | - if is_tf_available(): |
457 | | - ua += f"; tensorflow/{_tf_version}" |
458 | | - if is_fastai_available(): |
459 | | - ua += f"; fastai/{_fastai_version}" |
460 | | - if is_fastcore_available(): |
461 | | - ua += f"; fastcore/{_fastcore_version}" |
462 | | - if isinstance(user_agent, dict): |
463 | | - ua += "; " + "; ".join(f"{k}/{v}" for k, v in user_agent.items()) |
464 | | - elif isinstance(user_agent, str): |
465 | | - ua += "; " + user_agent |
466 | | - return ua |
| 325 | + """Deprecated in favor of [`build_hf_headers`].""" |
| 326 | + return _http_user_agent( |
| 327 | + library_name=library_name, |
| 328 | + library_version=library_version, |
| 329 | + user_agent=user_agent, |
| 330 | + ) |
467 | 331 |
|
468 | 332 |
|
469 | 333 | class OfflineModeIsEnabled(ConnectionError): |
@@ -733,8 +597,8 @@ def cached_download( |
733 | 597 |
|
734 | 598 | os.makedirs(cache_dir, exist_ok=True) |
735 | 599 |
|
736 | | - headers = build_hf_headers(use_auth_token=use_auth_token) |
737 | | - headers["user-agent"] = http_user_agent( |
| 600 | + headers = build_hf_headers( |
| 601 | + use_auth_token=use_auth_token, |
738 | 602 | library_name=library_name, |
739 | 603 | library_version=library_version, |
740 | 604 | user_agent=user_agent, |
@@ -1172,8 +1036,8 @@ def hf_hub_download( |
1172 | 1036 |
|
1173 | 1037 | url = hf_hub_url(repo_id, filename, repo_type=repo_type, revision=revision) |
1174 | 1038 |
|
1175 | | - headers = build_hf_headers(use_auth_token=use_auth_token) |
1176 | | - headers["user-agent"] = http_user_agent( |
| 1039 | + headers = build_hf_headers( |
| 1040 | + use_auth_token=use_auth_token, |
1177 | 1041 | library_name=library_name, |
1178 | 1042 | library_version=library_version, |
1179 | 1043 | user_agent=user_agent, |
|
0 commit comments