Skip to content

Commit 89e670d

Browse files
committed
dont load the spacy sm model for inference
1 parent f8869bb commit 89e670d

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

NewsSentiment/dataset.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,6 @@
4242
from NewsSentiment.models.FXBaseModel import FXBaseModel
4343

4444
logger = get_logger()
45-
try:
46-
nlp = spacy.load("en_core_web_sm")
47-
except OSError:
48-
spacy.cli.download("en_core_web_sm")
49-
nlp = spacy.load("en_core_web_sm")
50-
51-
# get list of parser's labels
52-
parser_index = nlp.pipe_names.index("parser")
53-
nlp_dep_parser_labels = list(nlp.pipeline[parser_index][1].labels)
5445

5546

5647
class RandomOversampler(torch.utils.data.sampler.Sampler):
@@ -86,6 +77,7 @@ def __iter__(self):
8677

8778

8879
class FXEasyTokenizer:
80+
NLP_DEP_PARSER_LABELS = None
8981
NUM_CATEGORIES_OF_SELECTED_KNOWLEDGE_SOURCES = 0
9082
__PROCESSED_KNOWLEDGE_SOURCES = set()
9183

@@ -96,11 +88,26 @@ def __init__(
9688
knowledge_sources: Iterable[str],
9789
is_use_natural_target_phrase_for_spc: bool,
9890
):
91+
self._get_labels()
9992
self.tokenizers_name_and_obj = tokenizers_name_and_obj
10093
self.max_seq_len = max_seq_len
10194
self.knowledge_sources = knowledge_sources
10295
self.is_use_natural_target_phrase_for_spc = is_use_natural_target_phrase_for_spc
10396

97+
@classmethod
98+
def _get_labels(cls):
99+
if cls.NLP_DEP_PARSER_LABELS is None:
100+
return
101+
try:
102+
nlp = spacy.load("en_core_web_sm")
103+
except OSError:
104+
spacy.cli.download("en_core_web_sm")
105+
nlp = spacy.load("en_core_web_sm")
106+
107+
# get list of parser's labels
108+
parser_index = nlp.pipe_names.index("parser")
109+
cls.NLP_DEP_PARSER_LABELS = list(nlp.pipeline[parser_index][1].labels)
110+
104111
@staticmethod
105112
def create_entire_text(
106113
text_left: str,

0 commit comments

Comments
 (0)