mustapha commited on
Commit
8ab03cb
1 Parent(s): 97588fd

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +41 -0
README.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ datasets:
4
+ - M-AI-C/quran-en-tafssirs
5
+ language:
6
+ - en
7
+ ---
8
+ ```python
9
+ import torch.nn.functional as F
10
+
11
+ from torch import Tensor
12
+ from transformers import AutoTokenizer, AutoModel
13
+
14
+
15
+ def average_pool(last_hidden_states: Tensor,
16
+ attention_mask: Tensor) -> Tensor:
17
+ last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
18
+ return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
19
+
20
+
21
+ # Each input text should start with "query: " or "passage: ".
22
+ # For tasks other than retrieval, you can simply use the "query: " prefix.
23
+ input_texts = ['query: Who is prophet known for patience',
24
+ 'query: Who is moses',
25
+ "passage: passage 1",
26
+ "passage: passage 2"]
27
+
28
+ tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-small')
29
+ model = AutoModel.from_pretrained('intfloat/e5-small')
30
+
31
+ # Tokenize the input texts
32
+ batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
33
+
34
+ outputs = model(**batch_dict)
35
+ embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
36
+
37
+ # (Optionally) normalize embeddings
38
+ embeddings = F.normalize(embeddings, p=2, dim=1)
39
+ scores = (embeddings[:2] @ embeddings[2:].T) * 100
40
+ print(scores.tolist())
41
+ ```