Update README.md
Browse files
README.md
CHANGED
@@ -4,4 +4,51 @@ library_name: transformers.js
|
|
4 |
|
5 |
https://huggingface.co/WhereIsAI/UAE-Large-V1 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/WhereIsAI/UAE-Large-V1 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/UAE-Large-V1', {
|
21 |
+
quantized: true, // Set this to false to use the full (unquantized) model
|
22 |
+
});
|
23 |
+
|
24 |
+
// Compute sentence embeddings
|
25 |
+
const sentences = ['That is a happy person', 'That is a very happy person'];
|
26 |
+
const output = await extractor(sentences, { pooling: 'cls' });
|
27 |
+
console.log(output);
|
28 |
+
// Tensor {
|
29 |
+
// dims: [ 2, 1024 ],
|
30 |
+
// type: 'float32',
|
31 |
+
// data: Float32Array(2048) [ -0.1308155655860901, 0.44334232807159424, ... ],
|
32 |
+
// size: 2048
|
33 |
+
// }
|
34 |
+
```
|
35 |
+
|
36 |
+
Compute cosine similarity between the two sentences:
|
37 |
+
```js
|
38 |
+
import { cos_sim } from '@xenova/transformers';
|
39 |
+
console.log(cos_sim(output[0].data, output[1].data))
|
40 |
+
// 0.9586893906734091
|
41 |
+
```
|
42 |
+
|
43 |
+
You can convert the `output` Tensor to a nested JavaScript array using `.tolist()`:
|
44 |
+
```js
|
45 |
+
console.log(output.tolist());
|
46 |
+
// [
|
47 |
+
// [ -0.1308155655860901, 0.44334232807159424, -0.12212765961885452, ... ],
|
48 |
+
// [ 0.03931744396686554, 0.30553528666496277, -0.19462820887565613, ... ]
|
49 |
+
// ]
|
50 |
+
```
|
51 |
+
|
52 |
+
---
|
53 |
+
|
54 |
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`).
|