Update README.md
Browse files
README.md
CHANGED
@@ -4,4 +4,52 @@ library_name: "transformers.js"
|
|
4 |
|
5 |
https://huggingface.co/thenlper/gte-small with ONNX weights to be compatible with Transformers.js.
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
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`).
|
|
|
4 |
|
5 |
https://huggingface.co/thenlper/gte-small with ONNX weights to be compatible with Transformers.js.
|
6 |
|
7 |
+
## Usage (Transformers.js)
|
8 |
+
|
9 |
+
If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@xenova/transformers) using:
|
10 |
+
```bash
|
11 |
+
npm i @xenova/transformers
|
12 |
+
```
|
13 |
+
|
14 |
+
You can then use the model to compute embeddings like this:
|
15 |
+
|
16 |
+
```js
|
17 |
+
import { pipeline } from '@xenova/transformers';
|
18 |
+
|
19 |
+
// Create a feature-extraction pipeline
|
20 |
+
const extractor = await pipeline('feature-extraction', 'Xenova/gte-small');
|
21 |
+
|
22 |
+
// Compute sentence embeddings
|
23 |
+
const sentences = ['That is a happy person', 'That is a very happy person'];
|
24 |
+
const output = await extractor(sentences, { pooling: 'mean', normalize: true });
|
25 |
+
console.log(output);
|
26 |
+
// Tensor {
|
27 |
+
// dims: [ 2, 384 ],
|
28 |
+
// type: 'float32',
|
29 |
+
// data: Float32Array(768) [ -0.053555335849523544, 0.00843878649175167, ... ],
|
30 |
+
// size: 768
|
31 |
+
// }
|
32 |
+
|
33 |
+
// Compute cosine similarity
|
34 |
+
import { cos_sim } from '@xenova/transformers';
|
35 |
+
console.log(cos_sim(output[0].data, output[1].data))
|
36 |
+
// 0.9798319649182318
|
37 |
+
```
|
38 |
+
|
39 |
+
You can convert this Tensor to a nested JavaScript array using `.tolist()`:
|
40 |
+
```js
|
41 |
+
console.log(output.tolist());
|
42 |
+
// [
|
43 |
+
// [ -0.053555335849523544, 0.00843878649175167, 0.06234041228890419, ... ],
|
44 |
+
// [ -0.049980051815509796, 0.03879701718688011, 0.07510733604431152, ... ]
|
45 |
+
// ]
|
46 |
+
```
|
47 |
+
|
48 |
+
By default, an 8-bit quantized version of the model is used, but you can choose to use the full-precision (fp32) version by specifying `{ quantized: false }` in the `pipeline` function:
|
49 |
+
```js
|
50 |
+
const extractor = await pipeline('feature-extraction', 'Xenova/gte-small', { quantized: false });
|
51 |
+
```
|
52 |
+
|
53 |
+
---
|
54 |
+
|
55 |
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`).
|