Spaces:
Runtime error
Runtime error
pip install transformers sentencepiece | |
pip install torch | |
import torch | |
from transformers import AutoTokenizer, AutoModel | |
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rubert-tiny2") | |
model = AutoModel.from_pretrained("cointegrated/rubert-tiny2") | |
# model.cuda() # uncomment it if you have a GPU | |
def embed_bert_cls(text, model, tokenizer): | |
t = tokenizer(text, padding=True, truncation=True, return_tensors='pt') | |
with torch.no_grad(): | |
model_output = model(**{k: v.to(model.device) for k, v in t.items()}) | |
embeddings = model_output.last_hidden_state[:, 0, :] | |
embeddings = torch.nn.functional.normalize(embeddings) | |
return embeddings[0].cpu().numpy() | |
print(embed_bert_cls('привет мир', model, tokenizer).shape) | |
# (312,) | |