Xenova HF staff commited on
Commit
015fa98
·
verified ·
1 Parent(s): cdc76d2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +61 -0
README.md CHANGED
@@ -1,8 +1,69 @@
1
  ---
2
  library_name: transformers.js
3
  pipeline_tag: image-text-to-text
 
4
  ---
5
 
6
  https://huggingface.co/vikhyatk/moondream2 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`).
 
1
  ---
2
  library_name: transformers.js
3
  pipeline_tag: image-text-to-text
4
+ license: apache-2.0
5
  ---
6
 
7
  https://huggingface.co/vikhyatk/moondream2 with ONNX weights to be compatible with Transformers.js.
8
 
9
+
10
+ ## Usage (Transformers.js)
11
+
12
+ > [!IMPORTANT]
13
+ > NOTE: Moondream support is experimental and requires you to install Transformers.js [v3](https://github.com/xenova/transformers.js/tree/v3) from source.
14
+
15
+ If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [GitHub](https://github.com/xenova/transformers.js/tree/v3) using:
16
+ ```bash
17
+ npm install xenova/transformers.js#v3
18
+ ```
19
+
20
+ **Example:**
21
+ ```js
22
+ import { AutoProcessor, AutoTokenizer, Moondream1ForConditionalGeneration, RawImage } from '@xenova/transformers';
23
+
24
+ // Load processor, tokenizer and model
25
+ const model_id = 'Xenova/moondream2';
26
+ const processor = await AutoProcessor.from_pretrained(model_id);
27
+ const tokenizer = await AutoTokenizer.from_pretrained(model_id);
28
+ const model = await Moondream1ForConditionalGeneration.from_pretrained(model_id, {
29
+ dtype: {
30
+ embed_tokens: 'fp16', // or 'fp32'
31
+ vision_encoder: 'fp16', // or 'q8'
32
+ decoder_model_merged: 'q4', // or 'q4f16' or 'q8'
33
+ },
34
+ device: 'webgpu',
35
+ });
36
+
37
+ // Prepare text inputs
38
+ const prompt = 'Describe this image.';
39
+ const text = `<image>\n\nQuestion: ${prompt}\n\nAnswer:`;
40
+ const text_inputs = tokenizer(text);
41
+
42
+ // Prepare vision inputs
43
+ const url = 'https://huggingface.co/vikhyatk/moondream1/resolve/main/assets/demo-1.jpg';
44
+ const image = await RawImage.fromURL(url);
45
+ const vision_inputs = await processor(image);
46
+
47
+ // Generate response
48
+ const output = await model.generate({
49
+ ...text_inputs,
50
+ ...vision_inputs,
51
+ do_sample: false,
52
+ max_new_tokens: 64,
53
+ });
54
+ const decoded = tokenizer.batch_decode(output, { skip_special_tokens: false });
55
+ console.log(decoded);
56
+ // [
57
+ // '<|endoftext|><image>\n\n' +
58
+ // 'Question: Describe this image.\n\n' +
59
+ // 'Answer: A hand is holding a white book titled "The Little Book of Deep Learning" against a backdrop of a balcony with a railing and a view of a building and trees.<|endoftext|>'
60
+ // ]
61
+ ```
62
+
63
+ We also released an online demo, which you can try yourself: https://huggingface.co/spaces/Xenova/experimental-moondream-webgpu
64
+
65
+ <video controls autoplay src="https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/9q6LTQIYiI3qKrKfAb4D8.mp4"></video>
66
+
67
+ ---
68
+
69
  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`).