Skip to content

Commit

Permalink
NL Server: use small spacy model instead of large (#4079)
Browse files Browse the repository at this point in the history
Per SO (https://stackoverflow.com/a/57337084) and spacy website
(https://spacy.io/models/en), the accuracy difference for
Parts-Of-Speech task (for verb detection) between large vs. small is
very small. The verb test sanity passes.

This should cut down the memory usage per NL server a bunch, since the
small model is apparently just 10MB (vs. 780MB).

I see that heap-usage drops from 970MB to 230MB.

Also, drop a super edge case handling for test environment which loads
SentenceTransformer always.
  • Loading branch information
pradh authored Mar 29, 2024
1 parent cdd1e2d commit 8150140
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 28 deletions.
18 changes: 1 addition & 17 deletions nl_server/gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,7 @@ def download_model_folder(model_folder: str) -> str:
# Only download if needed.
model_path = os.path.join(directory, model_folder)
if os.path.exists(model_path):
if os.environ.get('FLASK_ENV') not in [
'local', 'test', 'integration_test', 'webdriver'
]:
# If a production or production-like enrivonment,
# just return the model_path.
return model_path

# Check if this path can still be loaded as a Sentence Transformer
# model. If not, delete it and download anew.
try:
_ = SentenceTransformer(model_path)
return model_path
except:
print(f"Could not load the model from ({model_path}).")
print("Deleting this path and re-downloading.")
shutil.rmtree(model_path)
assert (not os.path.exists(model_path))
return model_path

print(
f"Model ({model_folder}) was either not previously downloaded or cannot successfully be loaded. Downloading to: {model_path}"
Expand Down
2 changes: 1 addition & 1 deletion nl_server/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import logging
import os
from typing import Any, Dict
from typing import Dict

from diskcache import Cache
from flask import Flask
Expand Down
4 changes: 2 additions & 2 deletions nl_server/nl_attribute_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

from typing import List

import en_core_web_lg
import en_core_web_sm


class NLAttributeModel:

def __init__(self) -> None:
self.spacy_model_ = en_core_web_lg.load()
self.spacy_model_ = en_core_web_sm.load()

def detect_verbs(self, query: str) -> List[str]:
try:
Expand Down
4 changes: 2 additions & 2 deletions nl_server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spacy==3.5.0
Werkzeug==3.0.1
# Downloading the named-entity recognition (NER) library spacy and the large EN model
# using the guidelines here: https://spacy.io/usage/models#production
-f https://github.com/explosion/spacy-models/releases/download/en_core_web_lg-3.5.0/en_core_web_lg-3.5.0-py3-none-any.whl
en_core_web_lg==3.5.0
-f https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0-py3-none-any.whl
en_core_web_sm==3.5.0
8 changes: 2 additions & 6 deletions nl_server/tests/verb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ def setUpClass(cls) -> None:
['give']
],
[('Elaborate how your roles in management accounting covering – '
'planning, organizing and directing, and controlling can assist'
'planning, organizing and directing, and controlling can assist '
'the above organization in achieving their goals and objectives.'
),
[
'Elaborate', 'covering', 'organizing', 'directing', 'controlling',
'achieving'
]],
), ['covering', 'organizing', 'controlling', 'assist', 'achieving']],
['How to write scholarship essay', ['write']]
])
def test_verb_detection(self, query_str, expected):
Expand Down

0 comments on commit 8150140

Please sign in to comment.