Allow soft dependencies in the namespace with ImportErrors at use (#7537)
* PoC on RAG * Format class name/obj name * Better name in message * PoC on one TF model * Add PyTorch and TF dummy objects + script * Treat scikit-learn * Bad copy pastes * Typo
This commit is contained in:
@@ -133,6 +133,15 @@ try:
|
||||
except ImportError:
|
||||
_faiss_available = False
|
||||
|
||||
try:
|
||||
import sklearn.metrics # noqa: F401
|
||||
|
||||
import scipy.stats # noqa: F401
|
||||
|
||||
_has_sklearn = True
|
||||
except (AttributeError, ImportError):
|
||||
_has_sklearn = False
|
||||
|
||||
|
||||
default_cache_path = os.path.join(torch_cache_home, "transformers")
|
||||
|
||||
@@ -194,6 +203,88 @@ def is_faiss_available():
|
||||
return _faiss_available
|
||||
|
||||
|
||||
def is_sklearn_available():
|
||||
return _has_sklearn
|
||||
|
||||
|
||||
DATASETS_IMPORT_ERROR = """
|
||||
{0} requires the 🤗 Datasets library but it was not found in your enviromnent. You can install it with:
|
||||
```
|
||||
pip install datasets
|
||||
```
|
||||
In a notebook or a colab, you can install it by executing a cell with
|
||||
```
|
||||
!pip install datasets
|
||||
```
|
||||
then restarting your kernel.
|
||||
|
||||
Note that if you have a local folder named `datasets` or a local python file named `datasets.py` in your current
|
||||
working directory, python may try to import this instead of the 🤗 Datasets library. You should rename this folder or
|
||||
that python file if that's the case.
|
||||
"""
|
||||
|
||||
|
||||
FAISS_IMPORT_ERROR = """
|
||||
{0} requires the faiss library but it was not found in your enviromnent. Checkout the instructions on the
|
||||
installation page of its repo: https://github.com/facebookresearch/faiss/blob/master/INSTALL.md and follow the ones
|
||||
that match your enviromnent.
|
||||
"""
|
||||
|
||||
|
||||
PYTORCH_IMPORT_ERROR = """
|
||||
{0} requires the PyTorch library but it was not found in your enviromnent. Checkout the instructions on the
|
||||
installation page: https://pytorch.org/get-started/locally/ and follow the ones that match your enviromnent.
|
||||
"""
|
||||
|
||||
|
||||
SKLEARN_IMPORT_ERROR = """
|
||||
{0} requires the scikit-learn library but it was not found in your enviromnent. You can install it with:
|
||||
```
|
||||
pip install -U scikit-learn
|
||||
```
|
||||
In a notebook or a colab, you can install it by executing a cell with
|
||||
```
|
||||
!pip install -U scikit-learn
|
||||
```
|
||||
"""
|
||||
|
||||
|
||||
TENSORFLOW_IMPORT_ERROR = """
|
||||
{0} requires the TensorFlow library but it was not found in your enviromnent. Checkout the instructions on the
|
||||
installation page: https://www.tensorflow.org/install and follow the ones that match your enviromnent.
|
||||
"""
|
||||
|
||||
|
||||
def requires_datasets(obj):
|
||||
name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__
|
||||
if not is_datasets_available():
|
||||
raise ImportError(DATASETS_IMPORT_ERROR.format(name))
|
||||
|
||||
|
||||
def requires_faiss(obj):
|
||||
name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__
|
||||
if not is_faiss_available():
|
||||
raise ImportError(FAISS_IMPORT_ERROR.format(name))
|
||||
|
||||
|
||||
def requires_pytorch(obj):
|
||||
name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__
|
||||
if not is_torch_available():
|
||||
raise ImportError(PYTORCH_IMPORT_ERROR.format(name))
|
||||
|
||||
|
||||
def requires_sklearn(obj):
|
||||
name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__
|
||||
if not is_sklearn_available():
|
||||
raise ImportError(SKLEARN_IMPORT_ERROR.format(name))
|
||||
|
||||
|
||||
def requires_tf(obj):
|
||||
name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__
|
||||
if not is_tf_available():
|
||||
raise ImportError(TENSORFLOW_IMPORT_ERROR.format(name))
|
||||
|
||||
|
||||
def add_start_docstrings(*docstr):
|
||||
def docstring_decorator(fn):
|
||||
fn.__doc__ = "".join(docstr) + (fn.__doc__ if fn.__doc__ is not None else "")
|
||||
|
||||
Reference in New Issue
Block a user