Skip to content

Commit 0ae8136

Browse files
authored
relax spacy import to relax dep (#622)
1 parent b9a2e56 commit 0ae8136

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ dependencies = [
7272
# Base metrics
7373
"nltk==3.9.1",
7474
"scikit-learn",
75-
"spacy==3.7.2",
7675
"sacrebleu",
7776
"rouge_score==0.1.2",
7877
"sentencepiece>=0.1.99",

src/lighteval/metrics/imports/data_stats_metric.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828
from collections import Counter
2929
from multiprocessing import Pool
3030

31-
import spacy
32-
3331
from lighteval.metrics.imports.data_stats_utils import Fragments
32+
from lighteval.utils.imports import NO_SPACY_ERROR_MSG, is_spacy_available
3433

3534

3635
logger = logging.getLogger(__name__)
@@ -72,6 +71,10 @@ def __init__(self, n_gram=3, n_workers=24, case=False, tokenize=True):
7271
:param tokenize: whether to tokenize the input; otherwise assumes that the input
7372
is a string of space-separated tokens
7473
"""
74+
if not is_spacy_available():
75+
raise ImportError(NO_SPACY_ERROR_MSG)
76+
import spacy
77+
7578
self.n_gram = n_gram
7679
self.n_workers = n_workers
7780
self.case = case

src/lighteval/utils/imports.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,10 @@ def wrapper(*args, **kwargs):
147147

148148

149149
NO_LATEX2SYMPY2_EXTENDED_ERROR_MSG = "You are trying to parse latex expressions, for which you need `latex2sympy2_extended`, which is not available in your environment. Please install it using `pip install lighteval[math]`."
150+
151+
152+
def is_spacy_available() -> bool:
153+
return importlib.util.find_spec("spacy") is not None
154+
155+
156+
NO_SPACY_ERROR_MSG = "You are trying to use some metrics requiring `spacy`, which is not available in your environment. Please install it using pip."

0 commit comments

Comments
 (0)