-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autodetect Model2Vec model paths, closes #822. Refactor vectors packa…
…ge, closes #826.
- Loading branch information
1 parent
4b5164b
commit 5fdbc90
Showing
13 changed files
with
174 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
SentenceTransformers module | ||
""" | ||
|
||
# Conditional import | ||
try: | ||
from sentence_transformers import SentenceTransformer | ||
|
||
SENTENCE_TRANSFORMERS = True | ||
except ImportError: | ||
SENTENCE_TRANSFORMERS = False | ||
|
||
from ..models import Models | ||
|
||
from .base import Vectors | ||
|
||
|
||
class STVectors(Vectors): | ||
""" | ||
Builds vectors using sentence-transformers (aka SBERT). | ||
""" | ||
|
||
def __init__(self, config, scoring, models): | ||
# Check before parent constructor since it calls loadmodel | ||
if not SENTENCE_TRANSFORMERS: | ||
raise ImportError('sentence-transformers is not available - install "vectors" extra to enable') | ||
|
||
super().__init__(config, scoring, models) | ||
|
||
def loadmodel(self, path): | ||
# Tensor device id | ||
deviceid = Models.deviceid(self.config.get("gpu", True)) | ||
|
||
# Additional model arguments | ||
modelargs = self.config.get("vectors", {}) | ||
|
||
# Build embeddings with sentence-transformers | ||
return SentenceTransformer(path, device=Models.device(deviceid), **modelargs) | ||
|
||
def encode(self, data): | ||
# Encode data using vectors model | ||
return self.model.encode(data, batch_size=self.encodebatch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.