This is an updated version of
cointegrated/rubert-tiny: a small Russian BERT-based encoder with high-quality sentence embeddings. This
post in Russian gives more details.
The model should be used as is to produce sentence embeddings (e.g. for KNN classification of short texts) or fine-tuned for a downstream task.
1# pip install transformers sentencepiece
2import torch
3from transformers import AutoTokenizer, AutoModel
4tokenizer = AutoTokenizer.from_pretrained("cointegrated/rubert-tiny2")
5model = AutoModel.from_pretrained("cointegrated/rubert-tiny2")
6# model.cuda() # uncomment it if you have a GPU
7
8def embed_bert_cls(text, model, tokenizer):
9 t = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
10 with torch.no_grad():
11 model_output = model(**{k: v.to(model.device) for k, v in t.items()})
12 embeddings = model_output.last_hidden_state[:, 0, :]
13 embeddings = torch.nn.functional.normalize(embeddings)
14 return embeddings[0].cpu().numpy()
15
16print(embed_bert_cls('привет мир', model, tokenizer).shape)
17# (312,)
1from sentence_transformers import SentenceTransformer
2model = SentenceTransformer('cointegrated/rubert-tiny2')
3sentences = ["привет мир", "hello world", "здравствуй вселенная"]
4embeddings = model.encode(sentences)
5print(embeddings)
For those who want to run the inference with
VLLM, there is a vLLM-optimized version of this model:
WpythonW/rubert-tiny2-vllm