vamsibanda commited on
Commit
b215f80
1 Parent(s): e286973

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +75 -0
README.md ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: sentence-similarity
3
+ language: en
4
+ license: apache-2.0
5
+ tags:
6
+ - sentence-transformers
7
+ - feature-extraction
8
+ - sentence-similarity
9
+ - transformers
10
+ - onnx
11
+ ---
12
+
13
+ #
14
+
15
+ This is the ONNX model of sentence-transformers/all-MiniLM-L6-v2 [https://seb.sbert.net]. Currently, Hugging Face does not support downloading ONNX model and generate embeddings. I have created a workaround using sbert and optimum together to generate embeddings.
16
+
17
+ ```
18
+ pip install onnx
19
+ pip install onnxruntime==1.10.0
20
+ pip install transformers>4.6.1
21
+ pip install sentencepiece
22
+ pip install sentence-transformers
23
+ pip install optimum
24
+ pip install torch==1.9.0
25
+ ```
26
+
27
+ Then you can use the model like this:
28
+
29
+ ```python
30
+ import os
31
+ from sentence_transformers.util import snapshot_download
32
+ from transformers import AutoTokenizer
33
+ from optimum.onnxruntime import ORTModelForFeatureExtraction
34
+ from sentence_transformers.models import Transformer, Pooling, Dense
35
+ import torch
36
+ from transformers.modeling_outputs import BaseModelOutput
37
+ import torch.nn.functional as F
38
+ import shutil
39
+
40
+ model_name = 'vamsibanda/sbert-onnx-all-MiniLM-L12-v2'
41
+ cache_folder = './'
42
+ model_path = os.path.join(cache_folder, model_name.replace("/", "_"))
43
+
44
+ def generate_embedding(text):
45
+ token = tokenizer(text, return_tensors='pt')
46
+ embeddings = model(input_ids=token['input_ids'], attention_mask=token['attention_mask'])
47
+ sbert_embeddings = mean_pooling(embeddings, token['attention_mask'])
48
+ sbert_embeddings = F.normalize(sbert_embeddings, p=2, dim=1)
49
+ return sbert_embeddings.tolist()[0]
50
+
51
+ def mean_pooling(model_output, attention_mask):
52
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
53
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
54
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
55
+
56
+ def download_onnx_model(model_name, cache_folder, model_path, force_download = False):
57
+ if force_download and os.path.exists(model_path):
58
+ shutil.rmtree(model_path)
59
+ elif os.path.exists(model_path):
60
+ return
61
+ snapshot_download(model_name,
62
+ cache_dir=cache_folder,
63
+ library_name='sentence-transformers'
64
+ )
65
+ return
66
+
67
+
68
+ _ = download_onnx_model(model_name, cache_folder, model_path)
69
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
70
+ model = ORTModelForFeatureExtraction.from_pretrained(model_path, force_download=False)
71
+ pooling_layer = Pooling.load(f"{model_path}/1_Pooling")
72
+
73
+ generate_embedding('That is a happy person')
74
+
75
+ ```