|
23 | 23 |
|
24 | 24 | logger = logging.getLogger(__name__) |
25 | 25 |
|
| 26 | +_PY_VERSION: str = sys.version.split()[0] |
26 | 27 |
|
27 | | -try: |
28 | | - import torch |
| 28 | +if tuple(int(i) for i in _PY_VERSION.split(".")) < (3, 8, 0): |
| 29 | + import importlib_metadata |
| 30 | +else: |
| 31 | + import importlib.metadata as importlib_metadata |
29 | 32 |
|
| 33 | +_torch_version = "N/A" |
| 34 | +_torch_available = False |
| 35 | +try: |
| 36 | + _torch_version = importlib_metadata.version("torch") |
30 | 37 | _torch_available = True |
31 | | -except ImportError: |
32 | | - _torch_available = False |
| 38 | +except importlib_metadata.PackageNotFoundError: |
| 39 | + pass |
33 | 40 |
|
| 41 | +_tf_version = "N/A" |
| 42 | +_tf_available = False |
34 | 43 | try: |
35 | | - import tensorflow as tf |
36 | | - |
| 44 | + _tf_version = importlib_metadata.version("tensorflow") |
37 | 45 | _tf_available = True |
38 | | -except ImportError: |
39 | | - _tf_available = False |
| 46 | +except importlib_metadata.PackageNotFoundError: |
| 47 | + pass |
40 | 48 |
|
41 | 49 |
|
42 | 50 | def is_torch_available(): |
@@ -161,11 +169,11 @@ def http_user_agent( |
161 | 169 | else: |
162 | 170 | ua = "unknown/None" |
163 | 171 | ua += "; hf_hub/{}".format(__version__) |
164 | | - ua += "; python/{}".format(sys.version.split()[0]) |
| 172 | + ua += "; python/{}".format(_PY_VERSION) |
165 | 173 | if is_torch_available(): |
166 | | - ua += "; torch/{}".format(torch.__version__) |
| 174 | + ua += "; torch/{}".format(_torch_version) |
167 | 175 | if is_tf_available(): |
168 | | - ua += "; tensorflow/{}".format(tf.__version__) |
| 176 | + ua += "; tensorflow/{}".format(_tf_version) |
169 | 177 | if isinstance(user_agent, dict): |
170 | 178 | ua += "; " + "; ".join("{}/{}".format(k, v) for k, v in user_agent.items()) |
171 | 179 | elif isinstance(user_agent, str): |
|
0 commit comments