Spaces:
Runtime error
Runtime error
romsyflux
commited on
Commit
•
b77ab00
1
Parent(s):
092bad3
Added import_utils diarize_utils
Browse files- utils/import_utils.py +145 -0
utils/import_utils.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2022 The HuggingFace Team. All rights reserved.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""
|
15 |
+
Import utilities: Utilities related to imports and our lazy inits.
|
16 |
+
"""
|
17 |
+
|
18 |
+
import importlib.util
|
19 |
+
import sys
|
20 |
+
from collections import OrderedDict
|
21 |
+
|
22 |
+
# The package importlib_metadata is in a different place, depending on the python version.
|
23 |
+
if sys.version_info < (3, 8):
|
24 |
+
import importlib_metadata
|
25 |
+
else:
|
26 |
+
import importlib.metadata as importlib_metadata
|
27 |
+
|
28 |
+
|
29 |
+
_transformers_available = importlib.util.find_spec("transformers") is not None
|
30 |
+
try:
|
31 |
+
_ = importlib_metadata.version("transformers")
|
32 |
+
_transformers_metadata = importlib_metadata.metadata("transformers")
|
33 |
+
except importlib_metadata.PackageNotFoundError:
|
34 |
+
_transformers_available = False
|
35 |
+
|
36 |
+
|
37 |
+
_accelerate_available = importlib.util.find_spec("accelerate") is not None
|
38 |
+
try:
|
39 |
+
_ = importlib_metadata.version("accelerate")
|
40 |
+
_accelerate_metadata = importlib_metadata.metadata("accelerate")
|
41 |
+
except importlib_metadata.PackageNotFoundError:
|
42 |
+
_accelerate_available = False
|
43 |
+
|
44 |
+
|
45 |
+
_scipy_available = importlib.util.find_spec("scipy") is not None
|
46 |
+
try:
|
47 |
+
_ = importlib_metadata.version("scipy")
|
48 |
+
_scipy_metadata = importlib_metadata.metadata("scipy")
|
49 |
+
except importlib_metadata.PackageNotFoundError:
|
50 |
+
_scipy_available = False
|
51 |
+
|
52 |
+
|
53 |
+
_pyannote_available = importlib.util.find_spec("pyannote.audio") is not None
|
54 |
+
try:
|
55 |
+
_ = importlib_metadata.version("pyannote.audio")
|
56 |
+
_pyannote_metadata = importlib_metadata.metadata("pyannote.audio")
|
57 |
+
except importlib_metadata.PackageNotFoundError:
|
58 |
+
_pyannote_available = False
|
59 |
+
|
60 |
+
|
61 |
+
_torchaudio_available = importlib.util.find_spec("torchaudio") is not None
|
62 |
+
try:
|
63 |
+
_ = importlib_metadata.version("torchaudio")
|
64 |
+
_torchaudio_metadata = importlib_metadata.metadata("torchaudio")
|
65 |
+
except importlib_metadata.PackageNotFoundError:
|
66 |
+
_torchaudio_available = False
|
67 |
+
|
68 |
+
|
69 |
+
def is_transformers_available():
|
70 |
+
return _transformers_available
|
71 |
+
|
72 |
+
|
73 |
+
def is_accelerate_available():
|
74 |
+
return _accelerate_available
|
75 |
+
|
76 |
+
|
77 |
+
def is_scipy_available():
|
78 |
+
return _scipy_available
|
79 |
+
|
80 |
+
|
81 |
+
def is_pyannote_available():
|
82 |
+
return _pyannote_available
|
83 |
+
|
84 |
+
|
85 |
+
def is_torchaudio_available():
|
86 |
+
return _torchaudio_available
|
87 |
+
|
88 |
+
|
89 |
+
TRANSFORMERS_IMPORT_ERROR = """
|
90 |
+
{0} requires the transformers library but it was not found in your environment. You can install it with pip: `pip install transformers`. Please note that you may need to restart your runtime after installation.
|
91 |
+
"""
|
92 |
+
|
93 |
+
|
94 |
+
ACCELERATE_IMPORT_ERROR = """
|
95 |
+
{0} requires the accelerate library but it was not found in your environment. You can install it with pip: `pip install accelerate`. Please note that you may need to restart your runtime after installation.
|
96 |
+
"""
|
97 |
+
|
98 |
+
|
99 |
+
SCIPY_IMPORT_ERROR = """
|
100 |
+
{0} requires the scipy library but it was not found in your environment. You can install it with pip: `pip install scipy`. Please note that you may need to restart your runtime after installation.
|
101 |
+
"""
|
102 |
+
|
103 |
+
|
104 |
+
PYANNOTE_IMPORT_ERROR = """
|
105 |
+
{0} requires the pyannote.audio library but it was not found in your environment. You can install it with pip: `pip install pyannote.audio`. Please note that you may need to restart your runtime after installation.
|
106 |
+
"""
|
107 |
+
|
108 |
+
|
109 |
+
TORCHAUDIO_IMPORT_ERROR = """
|
110 |
+
{0} requires the torchaudio library but it was not found in your environment. You can install it with pip: `pip install torchaudio`. Please note that you may need to restart your runtime after installation.
|
111 |
+
"""
|
112 |
+
|
113 |
+
BACKENDS_MAPPING = OrderedDict(
|
114 |
+
[
|
115 |
+
("transformers", (is_transformers_available, TRANSFORMERS_IMPORT_ERROR)),
|
116 |
+
("accelerate", (is_accelerate_available, ACCELERATE_IMPORT_ERROR)),
|
117 |
+
("scipy", (is_scipy_available, SCIPY_IMPORT_ERROR)),
|
118 |
+
("pyannote.audio", (is_pyannote_available, PYANNOTE_IMPORT_ERROR)),
|
119 |
+
("torchaudio", (is_torchaudio_available, TORCHAUDIO_IMPORT_ERROR)),
|
120 |
+
]
|
121 |
+
)
|
122 |
+
|
123 |
+
|
124 |
+
def requires_backends(obj, backends):
|
125 |
+
if not isinstance(backends, (list, tuple)):
|
126 |
+
backends = [backends]
|
127 |
+
|
128 |
+
name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__
|
129 |
+
|
130 |
+
checks = (BACKENDS_MAPPING[backend] for backend in backends)
|
131 |
+
failed = [msg.format(name) for available, msg in checks if not available()]
|
132 |
+
if failed:
|
133 |
+
raise ImportError("".join(failed))
|
134 |
+
|
135 |
+
|
136 |
+
class DummyObject(type):
|
137 |
+
"""
|
138 |
+
Metaclass for the dummy objects. Any class inheriting from it will return the ImportError generated by
|
139 |
+
`requires_backend` each time a user tries to access any method of that class.
|
140 |
+
"""
|
141 |
+
|
142 |
+
def __getattribute__(cls, key):
|
143 |
+
if key.startswith("_") and key != "_from_config":
|
144 |
+
return super().__getattribute__(key)
|
145 |
+
requires_backends(cls, cls._backends)
|