Create README.md
Browse files
README.md
CHANGED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- bn
|
4 |
+
- en
|
5 |
+
pipeline_tag: translation
|
6 |
+
---
|
7 |
+
# Bangla Sentence Transformer
|
8 |
+
|
9 |
+
Sentence Transformer is a cutting-edge natural language processing (NLP) model that is capable of encoding and transforming sentences into high-dimensional embeddings. With this technology, we can unlock powerful insights and applications in various fields like text classification, information retrieval, semantic search, and more.
|
10 |
+
|
11 |
+
This model is finetune from ```stsb-xlm-r-multilingual```
|
12 |
+
it's now available on Hugging Face! 🎉🎉
|
13 |
+
|
14 |
+
## Install
|
15 |
+
|
16 |
+
```
|
17 |
+
pip install -U sentence-transformers
|
18 |
+
```
|
19 |
+
|
20 |
+
```python
|
21 |
+
from sentence_transformers import SentenceTransformer
|
22 |
+
sentences = ['আমি আপেল খেতে পছন্দ করি। ', 'আমার একটি আপেল মোবাইল আছে।','আপনি কি এখানে কাছাকাছি থাকেন?', 'আশেপাশে কেউ আছেন?']
|
23 |
+
|
24 |
+
model = SentenceTransformer('shihab17/bangla-sentence-transformer ')
|
25 |
+
embeddings = model.encode(sentences)
|
26 |
+
print(embeddings)
|
27 |
+
```
|
28 |
+
|
29 |
+
```python
|
30 |
+
from transformers import AutoTokenizer, AutoModel
|
31 |
+
import torch
|
32 |
+
|
33 |
+
|
34 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
35 |
+
def mean_pooling(model_output, attention_mask):
|
36 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
37 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
38 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
39 |
+
|
40 |
+
|
41 |
+
# Sentences we want sentence embeddings for
|
42 |
+
sentences = ['আমি আপেল খেতে পছন্দ করি। ', 'আমার একটি আপেল মোবাইল আছে।','আপনি কি এখানে কাছাকাছি থাকেন?', 'আশেপাশে কেউ আছেন?']
|
43 |
+
|
44 |
+
# Load model from HuggingFace Hub
|
45 |
+
tokenizer = AutoTokenizer.from_pretrained('shihab17/bangla-sentence-transformer')
|
46 |
+
model = AutoModel.from_pretrained('shihab17/bangla-sentence-transformer')
|
47 |
+
|
48 |
+
# Tokenize sentences
|
49 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
50 |
+
|
51 |
+
# Compute token embeddings
|
52 |
+
with torch.no_grad():
|
53 |
+
model_output = model(**encoded_input)
|
54 |
+
|
55 |
+
# Perform pooling. In this case, mean pooling.
|
56 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
57 |
+
|
58 |
+
print("Sentence embeddings:")
|
59 |
+
print(sentence_embeddings)
|
60 |
+
```
|
61 |
+
|
62 |
+
## How to get sentence similarity
|
63 |
+
|
64 |
+
```python
|
65 |
+
from sentence_transformers import SentenceTransformer
|
66 |
+
from sentence_transformers.util import pytorch_cos_sim
|
67 |
+
|
68 |
+
|
69 |
+
transformer = SentenceTransformer('shihab17/bangla-sentence-transformer')
|
70 |
+
|
71 |
+
sentences = ['আমি আপেল খেতে পছন্দ করি। ', 'আমার একটি আপেল মোবাইল আছে।','আপনি কি এখানে কাছাকাছি থাকেন?', 'আশেপাশে কেউ আছেন?']
|
72 |
+
|
73 |
+
sentences_embeddings = transformer.encode(sentences)
|
74 |
+
|
75 |
+
for i in range(len(sentences)):
|
76 |
+
for j in range(i, len(sentences)):
|
77 |
+
sen_1 = sentences[i]
|
78 |
+
sen_2 = sentences[j]
|
79 |
+
sim_score = float(pytorch_cos_sim(sentences_embeddings[i], sentences_embeddings[j]))
|
80 |
+
print(sen_1, '----->', sen_2, sim_score)
|
81 |
+
```
|
82 |
+
|
83 |
+
## Best MSE: 7.57528096437454
|