Views
No views yet









pip install -U sentence-transformers) and then run the following code:1import numpy as np
2from sentence_transformers import SentenceTransformer
3
4model = SentenceTransformer("AkshitaS/bhasha-embed-v0")
5
6queries = [
7 "प्रणव ने कानून की पढ़ाई की और ३० की उम्र में राजनीति से जुड़ गए",
8 "Pranav studied law and became a politician at the age of 30.",
9 "Pranav ne kanoon ki padhai kari aur 30 ki umar mein rajneeti se jud gaye"
10]
11documents = [
12 "प्रणव ने कानून की पढ़ाई की और ३० की उम्र में राजनीति से जुड़ गए",
13 "Pranav studied law and became a politician at the age of 30.",
14 "Pranav ne kanoon ki padhai kari aur 30 ki umar mein rajneeti se jud gaye",
15 "प्रणव का जन्म राजनीतिज्ञों के परिवार में हुआ था",
16 "Pranav was born in a family of politicians",
17 "Pranav ka janm rajneetigyon ke parivar mein hua tha"
18]
19
20query_embeddings = model.encode(queries, normalize_embeddings=True)
21document_embeddings = model.encode(documents, normalize_embeddings=True)
22
23similarity_matrix = (query_embeddings @ document_embeddings.T)
24print(similarity_matrix.shape)
25# (3, 6)
26print(np.round(similarity_matrix, 2))
27#[[1.00 0.97 0.97 0.92 0.90 0.91]
28# [0.97 1.00 0.96 0.90 0.91 0.91]
29# [0.97 0.96 1.00 0.89 0.90 0.92]]1import numpy as np
2from torch import Tensor
3import torch.nn.functional as F
4from transformers import AutoTokenizer, AutoModel
5
6
7def average_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
8 last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
9 return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
10
11
12model_id = "AkshitaS/bhasha-embed-v0"
13tokenizer = AutoTokenizer.from_pretrained(model_id)
14model = AutoModel.from_pretrained(model_id)
15
16queries = [
17 "प्रणव ने कानून की पढ़ाई की और ३० की उम्र में राजनीति से जुड़ गए",
18 "Pranav studied law and became a politician at the age of 30.",
19 "Pranav ne kanoon ki padhai kari aur 30 ki umar mein rajneeti se jud gaye"
20]
21documents = [
22 "प्रणव ने कानून की पढ़ाई की और ३० की उम्र में राजनीति से जुड़ गए",
23 "Pranav studied law and became a politician at the age of 30.",
24 "Pranav ne kanoon ki padhai kari aur 30 ki umar mein rajneeti se jud gaye",
25 "प्रणव का जन्म राजनीतिज्ञों के परिवार में हुआ था",
26 "Pranav was born in a family of politicians",
27 "Pranav ka janm rajneetigyon ke parivar mein hua tha"
28]
29
30input_texts = queries + documents
31batch_dict = tokenizer(input_texts, padding=True, truncation=True, return_tensors='pt')
32outputs = model(**batch_dict)
33embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
34
35embeddings = F.normalize(embeddings, p=2, dim=1)
36similarity_matrix = (embeddings[:len(queries)] @ embeddings[len(queries):].T).detach().numpy()
37print(similarity_matrix.shape)
38# (3, 6)
39print(np.round(similarity_matrix, 2))
40#[[1.00 0.97 0.97 0.92 0.90 0.91]
41# [0.97 1.00 0.96 0.90 0.91 0.91]
42# [0.97 0.96 1.00 0.89 0.90 0.92]]@misc{sukhlecha_2024_bhasha_embed_v0,
author = {Sukhlecha, Akshita},
title = {Bhasha-embed-v0},
howpublished = {Hugging Face},
month = {June},
year = {2024},
url = {https://huggingface.co/AkshitaS/bhasha-embed-v0}
}