-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model2vec vectorization, closes #801
- Loading branch information
1 parent
4a26804
commit a246052
Showing
7 changed files
with
90 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Model2Vec module | ||
""" | ||
|
||
# Conditional import | ||
try: | ||
from model2vec import StaticModel | ||
|
||
MODEL2VEC = True | ||
except ImportError: | ||
MODEL2VEC = False | ||
|
||
from .base import Vectors | ||
|
||
|
||
class Model2Vec(Vectors): | ||
""" | ||
Builds vectors using Model2Vec. | ||
""" | ||
|
||
def __init__(self, config, scoring, models): | ||
# Check before parent constructor since it calls loadmodel | ||
if not MODEL2VEC: | ||
raise ImportError('Model2Vec is not available - install "vectors" extra to enable') | ||
|
||
super().__init__(config, scoring, models) | ||
|
||
def loadmodel(self, path): | ||
return StaticModel.from_pretrained(path) | ||
|
||
def encode(self, data): | ||
return self.model.encode(data) |
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,40 @@ | ||
""" | ||
Model2Vec module tests | ||
""" | ||
|
||
import os | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from txtai.vectors import VectorsFactory | ||
|
||
|
||
class TestModel2Vec(unittest.TestCase): | ||
""" | ||
Model2vec vectors tests | ||
""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
""" | ||
Create Model2Vec instance. | ||
""" | ||
|
||
cls.model = VectorsFactory.create({"method": "model2vec", "path": "minishlab/M2V_base_output"}, None) | ||
|
||
def testIndex(self): | ||
""" | ||
Test indexing with Model2Vec vectors. | ||
""" | ||
|
||
ids, dimension, batches, stream = self.model.index([(0, "test", None)]) | ||
|
||
self.assertEqual(len(ids), 1) | ||
self.assertEqual(dimension, 256) | ||
self.assertEqual(batches, 1) | ||
self.assertIsNotNone(os.path.exists(stream)) | ||
|
||
# Test shape of serialized embeddings | ||
with open(stream, "rb") as queue: | ||
self.assertEqual(np.load(queue).shape, (1, 256)) |