|
1 |
| -# Copyright 2021 The HuggingFace Inc. team. All rights reserved. |
| 1 | +# flake8: noqa |
| 2 | +# There's no way to ignore "F401 '...' imported but unused" warnings in this |
| 3 | +# module, but to preserve other warnings. So, don't check this module at all. |
| 4 | + |
| 5 | +# Copyright 2022 The HuggingFace Inc. team. All rights reserved. |
2 | 6 | #
|
3 | 7 | # Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 8 | # you may not use this file except in compliance with the License.
|
|
11 | 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 16 | # See the License for the specific language governing permissions and
|
13 | 17 | # limitations under the License.
|
14 |
| -import importlib |
15 |
| -import os |
16 |
| -from collections import OrderedDict |
17 | 18 |
|
18 |
| -import importlib_metadata |
19 |
| -from requests.exceptions import HTTPError |
20 | 19 |
|
| 20 | +import os |
| 21 | + |
| 22 | +from .import_utils import ( |
| 23 | + ENV_VARS_TRUE_AND_AUTO_VALUES, |
| 24 | + ENV_VARS_TRUE_VALUES, |
| 25 | + USE_JAX, |
| 26 | + USE_TF, |
| 27 | + USE_TORCH, |
| 28 | + DummyObject, |
| 29 | + is_flax_available, |
| 30 | + is_inflect_available, |
| 31 | + is_scipy_available, |
| 32 | + is_tf_available, |
| 33 | + is_torch_available, |
| 34 | + is_transformers_available, |
| 35 | + is_unidecode_available, |
| 36 | + requires_backends, |
| 37 | +) |
21 | 38 | from .logging import get_logger
|
22 | 39 |
|
23 | 40 |
|
|
35 | 52 | DIFFUSERS_CACHE = default_cache_path
|
36 | 53 | DIFFUSERS_DYNAMIC_MODULE_NAME = "diffusers_modules"
|
37 | 54 | HF_MODULES_CACHE = os.getenv("HF_MODULES_CACHE", os.path.join(hf_cache_home, "modules"))
|
38 |
| - |
39 |
| - |
40 |
| -_transformers_available = importlib.util.find_spec("transformers") is not None |
41 |
| -try: |
42 |
| - _transformers_version = importlib_metadata.version("transformers") |
43 |
| - logger.debug(f"Successfully imported transformers version {_transformers_version}") |
44 |
| -except importlib_metadata.PackageNotFoundError: |
45 |
| - _transformers_available = False |
46 |
| - |
47 |
| - |
48 |
| -_inflect_available = importlib.util.find_spec("inflect") is not None |
49 |
| -try: |
50 |
| - _inflect_version = importlib_metadata.version("inflect") |
51 |
| - logger.debug(f"Successfully imported inflect version {_inflect_version}") |
52 |
| -except importlib_metadata.PackageNotFoundError: |
53 |
| - _inflect_available = False |
54 |
| - |
55 |
| - |
56 |
| -_unidecode_available = importlib.util.find_spec("unidecode") is not None |
57 |
| -try: |
58 |
| - _unidecode_version = importlib_metadata.version("unidecode") |
59 |
| - logger.debug(f"Successfully imported unidecode version {_unidecode_version}") |
60 |
| -except importlib_metadata.PackageNotFoundError: |
61 |
| - _unidecode_available = False |
62 |
| - |
63 |
| - |
64 |
| -_modelcards_available = importlib.util.find_spec("modelcards") is not None |
65 |
| -try: |
66 |
| - _modelcards_version = importlib_metadata.version("modelcards") |
67 |
| - logger.debug(f"Successfully imported modelcards version {_modelcards_version}") |
68 |
| -except importlib_metadata.PackageNotFoundError: |
69 |
| - _modelcards_available = False |
70 |
| - |
71 |
| - |
72 |
| -_scipy_available = importlib.util.find_spec("scipy") is not None |
73 |
| -try: |
74 |
| - _scipy_version = importlib_metadata.version("scipy") |
75 |
| - logger.debug(f"Successfully imported transformers version {_scipy_version}") |
76 |
| -except importlib_metadata.PackageNotFoundError: |
77 |
| - _scipy_available = False |
78 |
| - |
79 |
| - |
80 |
| -def is_transformers_available(): |
81 |
| - return _transformers_available |
82 |
| - |
83 |
| - |
84 |
| -def is_inflect_available(): |
85 |
| - return _inflect_available |
86 |
| - |
87 |
| - |
88 |
| -def is_unidecode_available(): |
89 |
| - return _unidecode_available |
90 |
| - |
91 |
| - |
92 |
| -def is_modelcards_available(): |
93 |
| - return _modelcards_available |
94 |
| - |
95 |
| - |
96 |
| -def is_scipy_available(): |
97 |
| - return _scipy_available |
98 |
| - |
99 |
| - |
100 |
| -class RepositoryNotFoundError(HTTPError): |
101 |
| - """ |
102 |
| - Raised when trying to access a hf.co URL with an invalid repository name, or with a private repo name the user does |
103 |
| - not have access to. |
104 |
| - """ |
105 |
| - |
106 |
| - |
107 |
| -class EntryNotFoundError(HTTPError): |
108 |
| - """Raised when trying to access a hf.co URL with a valid repository and revision but an invalid filename.""" |
109 |
| - |
110 |
| - |
111 |
| -class RevisionNotFoundError(HTTPError): |
112 |
| - """Raised when trying to access a hf.co URL with a valid repository but an invalid revision.""" |
113 |
| - |
114 |
| - |
115 |
| -TRANSFORMERS_IMPORT_ERROR = """ |
116 |
| -{0} requires the transformers library but it was not found in your environment. You can install it with pip: `pip |
117 |
| -install transformers` |
118 |
| -""" |
119 |
| - |
120 |
| - |
121 |
| -UNIDECODE_IMPORT_ERROR = """ |
122 |
| -{0} requires the unidecode library but it was not found in your environment. You can install it with pip: `pip install |
123 |
| -Unidecode` |
124 |
| -""" |
125 |
| - |
126 |
| - |
127 |
| -INFLECT_IMPORT_ERROR = """ |
128 |
| -{0} requires the inflect library but it was not found in your environment. You can install it with pip: `pip install |
129 |
| -inflect` |
130 |
| -""" |
131 |
| - |
132 |
| - |
133 |
| -SCIPY_IMPORT_ERROR = """ |
134 |
| -{0} requires the scipy library but it was not found in your environment. You can install it with pip: `pip install |
135 |
| -scipy` |
136 |
| -""" |
137 |
| - |
138 |
| - |
139 |
| -BACKENDS_MAPPING = OrderedDict( |
140 |
| - [ |
141 |
| - ("transformers", (is_transformers_available, TRANSFORMERS_IMPORT_ERROR)), |
142 |
| - ("unidecode", (is_unidecode_available, UNIDECODE_IMPORT_ERROR)), |
143 |
| - ("inflect", (is_inflect_available, INFLECT_IMPORT_ERROR)), |
144 |
| - ("scipy", (is_scipy_available, SCIPY_IMPORT_ERROR)), |
145 |
| - ] |
146 |
| -) |
147 |
| - |
148 |
| - |
149 |
| -def requires_backends(obj, backends): |
150 |
| - if not isinstance(backends, (list, tuple)): |
151 |
| - backends = [backends] |
152 |
| - |
153 |
| - name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__ |
154 |
| - checks = (BACKENDS_MAPPING[backend] for backend in backends) |
155 |
| - failed = [msg.format(name) for available, msg in checks if not available()] |
156 |
| - if failed: |
157 |
| - raise ImportError("".join(failed)) |
158 |
| - |
159 |
| - |
160 |
| -class DummyObject(type): |
161 |
| - """ |
162 |
| - Metaclass for the dummy objects. Any class inheriting from it will return the ImportError generated by |
163 |
| - `requires_backend` each time a user tries to access any method of that class. |
164 |
| - """ |
165 |
| - |
166 |
| - def __getattr__(cls, key): |
167 |
| - if key.startswith("_"): |
168 |
| - return super().__getattr__(cls, key) |
169 |
| - requires_backends(cls, cls._backends) |
0 commit comments