File size: 752 Bytes
4ccf6a3 0e97d35 cf575f8 3f6f474 cf575f8 d654474 4ccf6a3 d654474 cf575f8 3f6f474 cf575f8 0e97d35 cf575f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from functools import lru_cache
import torch
from sentence_transformers import SentenceTransformer
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
list_models = [
'sentence-transformers/paraphrase-multilingual-mpnet-base-v2',
'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2',
'sentence-transformers/all-mpnet-base-v2',
'sentence-transformers/all-MiniLM-L12-v2',
'cyclone/simcse-chinese-roberta-wwm-ext'
]
class SBert:
def __init__(self, path):
print(f'Loading model from {path} ...')
self.model = SentenceTransformer(path, device=DEVICE)
@lru_cache(maxsize=10000)
def __call__(self, x) -> torch.Tensor:
y = self.model.encode(x, convert_to_tensor=True)
return y
|