Update README.md
Browse files
README.md
CHANGED
@@ -5,4 +5,49 @@ base_model: google/paligemma2-3b-pt-896
|
|
5 |
|
6 |
https://huggingface.co/google/paligemma2-3b-pt-896 with ONNX weights to be compatible with Transformers.js.
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
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`).
|
|
|
5 |
|
6 |
https://huggingface.co/google/paligemma2-3b-pt-896 with ONNX weights to be compatible with Transformers.js.
|
7 |
|
8 |
+
## Usage (Transformers.js)
|
9 |
+
|
10 |
+
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/@huggingface/transformers) using:
|
11 |
+
```bash
|
12 |
+
npm i @huggingface/transformers
|
13 |
+
```
|
14 |
+
|
15 |
+
**Example:** Image captioning with `onnx-community/paligemma2-3b-pt-896`.
|
16 |
+
```js
|
17 |
+
import { AutoProcessor, PaliGemmaForConditionalGeneration, load_image } from '@huggingface/transformers';
|
18 |
+
|
19 |
+
// Load processor and model
|
20 |
+
const model_id = 'onnx-community/paligemma2-3b-pt-896';
|
21 |
+
const processor = await AutoProcessor.from_pretrained(model_id);
|
22 |
+
const model = await PaliGemmaForConditionalGeneration.from_pretrained(model_id, {
|
23 |
+
dtype: {
|
24 |
+
embed_tokens: 'fp16', // or 'q8'
|
25 |
+
vision_encoder: 'q4', // or 'fp16', 'q8'
|
26 |
+
decoder_model_merged: 'q4', // or 'q4f16'
|
27 |
+
},
|
28 |
+
});
|
29 |
+
|
30 |
+
// Prepare inputs
|
31 |
+
const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg'
|
32 |
+
const raw_image = await load_image(url);
|
33 |
+
const prompt = '<image>'; // Caption, by default
|
34 |
+
const inputs = await processor(raw_image, prompt);
|
35 |
+
|
36 |
+
// Generate a response
|
37 |
+
const output = await model.generate({
|
38 |
+
...inputs,
|
39 |
+
max_new_tokens: 100,
|
40 |
+
})
|
41 |
+
|
42 |
+
const generated_ids = output.slice(null, [inputs.input_ids.dims[1], null]);
|
43 |
+
const answer = processor.batch_decode(
|
44 |
+
generated_ids,
|
45 |
+
{ skip_special_tokens: true },
|
46 |
+
);
|
47 |
+
console.log(answer[0]);
|
48 |
+
// a classic car parked in front of a house
|
49 |
+
```
|
50 |
+
|
51 |
+
---
|
52 |
+
|
53 |
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`).
|