Xenova HF staff commited on
Commit
8738cb3
1 Parent(s): 7d73361

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +71 -0
README.md CHANGED
@@ -4,4 +4,75 @@ library_name: transformers.js
4
 
5
  https://huggingface.co/google/siglip-large-patch16-256 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/google/siglip-large-patch16-256 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
+ **Example:** Zero-shot image classification w/ `Xenova/siglip-large-patch16-256`:
15
+ ```js
16
+ import { pipeline } from '@xenova/transformers';
17
+
18
+ const classifier = await pipeline('zero-shot-image-classification', 'Xenova/siglip-large-patch16-256');
19
+ const url = 'http://images.cocodataset.org/val2017/000000039769.jpg';
20
+ const output = await classifier(url, ['2 cats', '2 dogs'], {
21
+ hypothesis_template: 'a photo of {}',
22
+ });
23
+ console.log(output);
24
+ // [
25
+ // { score: 0.3086719512939453, label: '2 cats' },
26
+ // { score: 0.0000623430241830647, label: '2 dogs' }
27
+ // ]
28
+ ```
29
+
30
+ **Example:** Compute text embeddings with `SiglipTextModel`.
31
+
32
+ ```javascript
33
+ import { AutoTokenizer, SiglipTextModel } from '@xenova/transformers';
34
+
35
+ // Load tokenizer and text model
36
+ const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-large-patch16-256');
37
+ const text_model = await SiglipTextModel.from_pretrained('Xenova/siglip-large-patch16-256');
38
+
39
+ // Run tokenization
40
+ const texts = ['a photo of 2 cats', 'a photo of 2 dogs'];
41
+ const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true });
42
+
43
+ // Compute embeddings
44
+ const { pooler_output } = await text_model(text_inputs);
45
+ // Tensor {
46
+ // dims: [ 2, 768 ],
47
+ // type: 'float32',
48
+ // data: Float32Array(1536) [ ... ],
49
+ // size: 1536
50
+ // }
51
+ ```
52
+
53
+ **Example:** Compute vision embeddings with `SiglipVisionModel`.
54
+
55
+ ```javascript
56
+ import { AutoProcessor, SiglipVisionModel, RawImage} from '@xenova/transformers';
57
+
58
+ // Load processor and vision model
59
+ const processor = await AutoProcessor.from_pretrained('Xenova/siglip-large-patch16-256');
60
+ const vision_model = await SiglipVisionModel.from_pretrained('Xenova/siglip-large-patch16-256');
61
+
62
+ // Read image and run processor
63
+ const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
64
+ const image_inputs = await processor(image);
65
+
66
+ // Compute embeddings
67
+ const { pooler_output } = await vision_model(image_inputs);
68
+ // Tensor {
69
+ // dims: [ 1, 768 ],
70
+ // type: 'float32',
71
+ // data: Float32Array(768) [ ... ],
72
+ // size: 768
73
+ // }
74
+ ```
75
+
76
+ ---
77
+
78
  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`).