File size: 4,269 Bytes
54648ea
 
 
 
 
 
 
 
 
 
 
ee4ce56
 
 
54648ea
 
 
 
 
 
 
 
ee4ce56
 
54648ea
 
 
 
ee4ce56
 
 
 
 
ab1f0c5
 
ee4ce56
 
 
 
 
54648ea
ee4ce56
54648ea
 
 
 
 
 
 
ee4ce56
54648ea
 
 
 
 
ee4ce56
 
54648ea
ee4ce56
54648ea
 
 
 
ee4ce56
54648ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee4ce56
 
 
 
 
54648ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee4ce56
 
 
 
54648ea
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { AutoProcessor, Qwen2VLForConditionalGeneration, RawImage } from "@huggingface/transformers";

const EXAMPLE_URL = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg";

const exampleButton = document.getElementById('example');
const promptInput = document.querySelector('input[type="text"]');
const status = document.getElementById('status');
const thumb = document.getElementById('thumb');
const uploadInput = document.getElementById('upload');
const form = document.getElementById('form');
const output = document.getElementById('llm-output');
const dtypeSelect = document.getElementById('dtype-select');
const loadModelButton = document.getElementById('load-model');
const container = document.getElementById('container');

let currentImage = '';
let currentQuery = '';
const model_id = "onnx-community/Qwen2-VL-2B-Instruct";
let processor;
let model;

async function initializeSessions() {
  loadModelButton.textContent = 'Loading Model...';
  loadModelButton.classList.add('loading');
  container.classList.add('disabled');

  processor = await AutoProcessor.from_pretrained(model_id);

  const dtype = dtypeSelect.value;
  const options = { device: 'webgpu', };
  if (dtype) {
    options.dtype = dtype;
  }
  options['transformers.js_config'] = {};
  options['transformers.js_config']['kv_cache_dtype'] = 'float16';
  model = await Qwen2VLForConditionalGeneration.from_pretrained(model_id, options);

  loadModelButton.textContent = 'Model Ready';
  loadModelButton.classList.remove('loading');
  loadModelButton.classList.add('ready');

  dtypeSelect.disabled = true;
  uploadInput.disabled = false;
  promptInput.disabled = false;
  container.classList.remove('disabled');
}

async function handleQuery(imageUrl, query) {
  try {
    loadModelButton.textContent = 'Processing...';

    const result = await imageTextToText(imageUrl, query, (out) => {
      console.log({ out });
      output.textContent = out;
    });

    loadModelButton.textContent = 'Model Ready';
  } catch (err) {
    loadModelButton.textContent = 'Error';
    console.error(err);
  }
}

async function imageTextToText(
  imagePath,
  query,
  cb,
) {
  const image = await (await RawImage.read(imagePath)).resize(448, 448);
  const conversation = [
    {
      role: "user",
      content: [
        { type: "image" },
        { type: "text", text: query, },
      ],
      images: [image],
    },
  ];
  const text = processor.apply_chat_template(conversation, { add_generation_prompt: true });
  const inputs = await processor(text, image);

  const outputs = await model.generate({
    ...inputs,
    max_new_tokens: 128,
  });

  const decoded = processor.batch_decode(
    outputs.slice(null, [inputs.input_ids.dims.at(-1), null]),
    { skip_special_tokens: true },
  );

  cb(decoded);

  return decoded;
}

async function updatePreview(url) {
  const image = await RawImage.fromURL(url);
  const ar = image.width / image.height;
  const [cw, ch] = (ar > 1) ? [320, 320 / ar] : [320 * ar, 320];
  thumb.style.width = `${cw}px`;
  thumb.style.height = `${ch}px`;
  thumb.style.backgroundImage = `url(${url})`;
  thumb.innerHTML = '';
}

loadModelButton.addEventListener('click', async () => {
  dtypeSelect.disabled = true;
  loadModelButton.disabled = true;
  await initializeSessions();
});

// UI Event Handlers
exampleButton.addEventListener('click', (e) => {
  e.preventDefault();
  currentImage = EXAMPLE_URL;
  updatePreview(currentImage);
});

uploadInput.addEventListener('change', (e) => {
  const file = e.target.files[0];
  if (!file) return;

  const reader = new FileReader();
  reader.onload = (e2) => {
    currentImage = e2.target.result;
    updatePreview(currentImage);
  };
  reader.readAsDataURL(file);
});

promptInput.addEventListener('keypress', (e) => {
  currentQuery = e.target.value;
});

form.addEventListener('submit', (e) => {
  e.preventDefault();

  if (!currentImage || !currentQuery) {
    loadModelButton.textContent = 'Please select an image and type a prompt';
    setTimeout(() => {
      loadModelButton.textContent = 'Model Ready';
    }, 2000);
  } else {
    promptInput.disabled = true;
    uploadInput.disabled = true;
    handleQuery(currentImage, currentQuery);
  }
});