ggrn commited on
Commit
7ea1f47
1 Parent(s): 8d87455

docs(readme): fork original readme and add js examples

Browse files
Files changed (1) hide show
  1. README.md +177 -2
README.md CHANGED
@@ -1,6 +1,181 @@
1
  ---
 
2
  library_name: "transformers.js"
 
 
 
3
  ---
4
- https://huggingface.co/thenlper/gte-small with ONNX weights to be compatible with Transformers.js.
5
 
6
- Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [🤗 Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).~
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ pipeline_tag: feature-extraction
3
  library_name: "transformers.js"
4
+ language:
5
+ - en
6
+ license: mit
7
  ---
 
8
 
9
+ # gte-small
10
+
11
+ > Fork of https://huggingface.co/thenlper/gte-small with ONNX weights to be compatible with Transformers.js.
12
+
13
+ Gegeral Text Embeddings (GTE) model.
14
+
15
+ The GTE models are trained by Alibaba DAMO Academy. They are mainly based on the BERT framework and currently offer three different sizes of models, including [GTE-large](https://huggingface.co/thenlper/gte-large), [GTE-base](https://huggingface.co/thenlper/gte-base), and [GTE-small](https://huggingface.co/thenlper/gte-small). The GTE models are trained on a large-scale corpus of relevance text pairs, covering a wide range of domains and scenarios. This enables the GTE models to be applied to various downstream tasks of text embeddings, including **information retrieval**, **semantic textual similarity**, **text reranking**, etc.
16
+
17
+ ## Metrics
18
+
19
+ Performance of GTE models were compared with other popular text embedding models on the MTEB benchmark. For more detailed comparison results, please refer to the [MTEB leaderboard](https://huggingface.co/spaces/mteb/leaderboard).
20
+
21
+
22
+
23
+ | Model Name | Model Size (GB) | Dimension | Sequence Length | Average (56) | Clustering (11) | Pair Classification (3) | Reranking (4) | Retrieval (15) | STS (10) | Summarization (1) | Classification (12) |
24
+ |:----:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
25
+ | [**gte-large**](https://huggingface.co/thenlper/gte-large) | 0.67 | 1024 | 512 | **63.13** | 46.84 | 85.00 | 59.13 | 52.22 | 83.35 | 31.66 | 73.33 |
26
+ | [**gte-base**](https://huggingface.co/thenlper/gte-base) | 0.22 | 768 | 512 | **62.39** | 46.2 | 84.57 | 58.61 | 51.14 | 82.3 | 31.17 | 73.01 |
27
+ | [e5-large-v2](https://huggingface.co/intfloat/e5-large-v2) | 1.34 | 1024| 512 | 62.25 | 44.49 | 86.03 | 56.61 | 50.56 | 82.05 | 30.19 | 75.24 |
28
+ | [e5-base-v2](https://huggingface.co/intfloat/e5-base-v2) | 0.44 | 768 | 512 | 61.5 | 43.80 | 85.73 | 55.91 | 50.29 | 81.05 | 30.28 | 73.84 |
29
+ | [**gte-small**](https://huggingface.co/thenlper/gte-small) | 0.07 | 384 | 512 | **61.36** | 44.89 | 83.54 | 57.7 | 49.46 | 82.07 | 30.42 | 72.31 |
30
+ | [text-embedding-ada-002](https://platform.openai.com/docs/guides/embeddings) | - | 1536 | 8192 | 60.99 | 45.9 | 84.89 | 56.32 | 49.25 | 80.97 | 30.8 | 70.93 |
31
+ | [e5-small-v2](https://huggingface.co/intfloat/e5-base-v2) | 0.13 | 384 | 512 | 59.93 | 39.92 | 84.67 | 54.32 | 49.04 | 80.39 | 31.16 | 72.94 |
32
+ | [sentence-t5-xxl](https://huggingface.co/sentence-transformers/sentence-t5-xxl) | 9.73 | 768 | 512 | 59.51 | 43.72 | 85.06 | 56.42 | 42.24 | 82.63 | 30.08 | 73.42 |
33
+ | [all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2) | 0.44 | 768 | 514 | 57.78 | 43.69 | 83.04 | 59.36 | 43.81 | 80.28 | 27.49 | 65.07 |
34
+ | [sgpt-bloom-7b1-msmarco](https://huggingface.co/bigscience/sgpt-bloom-7b1-msmarco) | 28.27 | 4096 | 2048 | 57.59 | 38.93 | 81.9 | 55.65 | 48.22 | 77.74 | 33.6 | 66.19 |
35
+ | [all-MiniLM-L12-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L12-v2) | 0.13 | 384 | 512 | 56.53 | 41.81 | 82.41 | 58.44 | 42.69 | 79.8 | 27.9 | 63.21 |
36
+ | [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) | 0.09 | 384 | 512 | 56.26 | 42.35 | 82.37 | 58.04 | 41.95 | 78.9 | 30.81 | 63.05 |
37
+ | [contriever-base-msmarco](https://huggingface.co/nthakur/contriever-base-msmarco) | 0.44 | 768 | 512 | 56.00 | 41.1 | 82.54 | 53.14 | 41.88 | 76.51 | 30.36 | 66.68 |
38
+ | [sentence-t5-base](https://huggingface.co/sentence-transformers/sentence-t5-base) | 0.22 | 768 | 512 | 55.27 | 40.21 | 85.18 | 53.09 | 33.63 | 81.14 | 31.39 | 69.81 |
39
+
40
+
41
+ ## Usage
42
+
43
+ This model can be used with both [Python](#python) and [JavaScript](#javascript).
44
+
45
+ ### Python
46
+ Use with [Transformers](https://huggingface.co/docs/transformers/index) and [PyTorch](https://pytorch.org/docs/stable/index.html):
47
+
48
+ ```python
49
+ import torch.nn.functional as F
50
+ from torch import Tensor
51
+ from transformers import AutoTokenizer, AutoModel
52
+
53
+ def average_pool(last_hidden_states: Tensor,
54
+ attention_mask: Tensor) -> Tensor:
55
+ last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
56
+ return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
57
+
58
+ input_texts = [
59
+ "what is the capital of China?",
60
+ "how to implement quick sort in python?",
61
+ "Beijing",
62
+ "sorting algorithms"
63
+ ]
64
+
65
+ tokenizer = AutoTokenizer.from_pretrained("thenlper/gte-small")
66
+ model = AutoModel.from_pretrained("thenlper/gte-small")
67
+
68
+ # Tokenize the input texts
69
+ batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
70
+
71
+ outputs = model(**batch_dict)
72
+ embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
73
+
74
+ # (Optionally) normalize embeddings
75
+ embeddings = F.normalize(embeddings, p=2, dim=1)
76
+ scores = (embeddings[:1] @ embeddings[1:].T) * 100
77
+ print(scores.tolist())
78
+ ```
79
+
80
+ Use with [sentence-transformers](https://www.sbert.net/):
81
+ ```python
82
+ from sentence_transformers import SentenceTransformer
83
+ from sentence_transformers.util import cos_sim
84
+
85
+ sentences = ['That is a happy person', 'That is a very happy person']
86
+
87
+ model = SentenceTransformer('thenlper/gte-large')
88
+ embeddings = model.encode(sentences)
89
+ print(cos_sim(embeddings[0], embeddings[1]))
90
+ ```
91
+
92
+ ### JavaScript
93
+ This model can be used with JavaScript via [Transformers.js](https://huggingface.co/docs/transformers.js/index).
94
+
95
+ Use with [Deno](https://deno.land/manual/introduction) or [Supabase Edge Functions](https://supabase.com/docs/guides/functions):
96
+
97
+ ```ts
98
+ import { serve } from 'https://deno.land/[email protected]/http/server.ts'
99
+ import { env, pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]'
100
+
101
+ // Configuration for Deno runtime
102
+ env.useBrowserCache = false;
103
+ env.allowLocalModels = false;
104
+
105
+ const pipe = await pipeline(
106
+ 'feature-extraction',
107
+ 'Supabase/gte-small',
108
+ );
109
+
110
+ serve(async (req) => {
111
+ // Extract input string from JSON body
112
+ const { input } = await req.json();
113
+
114
+ // Generate the embedding from the user input
115
+ const output = await pipe(input, {
116
+ pooling: 'mean',
117
+ normalize: true,
118
+ });
119
+
120
+ // Extract the embedding output
121
+ const embedding = Array.from(output.data);
122
+
123
+ // Return the embedding
124
+ return new Response(
125
+ JSON.stringify({ embedding }),
126
+ { headers: { 'Content-Type': 'application/json' } }
127
+ );
128
+ });
129
+ ```
130
+
131
+ Use within the browser ([JavaScript Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)):
132
+
133
+ ```html
134
+ <script type="module">
135
+
136
+ import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
137
+
138
+ const pipe = await pipeline(
139
+ 'feature-extraction',
140
+ 'Supabase/gte-small',
141
+ );
142
+
143
+ // Generate the embedding from text
144
+ const output = await pipe('Hello world', {
145
+ pooling: 'mean',
146
+ normalize: true,
147
+ });
148
+
149
+ // Extract the embedding output
150
+ const embedding = Array.from(output.data);
151
+
152
+ console.log(embedding);
153
+
154
+ </script>
155
+ ```
156
+
157
+ Use within [Node.js](https://nodejs.org/en/docs) or a web bundler ([Webpack](https://webpack.js.org/concepts/), etc):
158
+
159
+ ```js
160
+ import { pipeline } from '@xenova/transformers';
161
+
162
+ const pipe = await pipeline(
163
+ 'feature-extraction',
164
+ 'Supabase/gte-small',
165
+ );
166
+
167
+ // Generate the embedding from text
168
+ const output = await pipe('Hello world', {
169
+ pooling: 'mean',
170
+ normalize: true,
171
+ });
172
+
173
+ // Extract the embedding output
174
+ const embedding = Array.from(output.data);
175
+
176
+ console.log(embedding);
177
+ ```
178
+
179
+ ### Limitation
180
+
181
+ This model exclusively caters to English texts, and any lengthy texts will be truncated to a maximum of 512 tokens.