Xenova HF staff commited on
Commit
3f2acba
1 Parent(s): da5f6ea

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +37 -0
README.md CHANGED
@@ -4,4 +4,41 @@ library_name: "transformers.js"
4
 
5
  https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2 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/sentence-transformers/all-MiniLM-L6-v2 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 for computing 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/all-MiniLM-L6-v2');
21
+
22
+ // Compute sentence embeddings
23
+ const sentences = ['This is an example sentence', 'Each sentence is converted'];
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.04592696577310562, 0.07328180968761444, ... ],
30
+ // size: 768
31
+ // }
32
+ ```
33
+
34
+ You can convert this Tensor to a nested JavaScript array using `.tolist()`:
35
+ ```js
36
+ console.log(output.tolist());
37
+ // [
38
+ // [ 0.04592696577310562, 0.07328180968761444, 0.05400655046105385, ... ],
39
+ // [ 0.08188057690858841, 0.10760223120450974, -0.013241755776107311, ... ]
40
+ // ]
41
+ ```
42
+
43
+
44
  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`).