|
1 | 1 | import type { InterruptableStoppingCriteria } from "@huggingface/transformers"; |
2 | 2 | import { |
| 3 | + AutoModelForImageTextToText, |
3 | 4 | AutoProcessor, |
4 | 5 | pipeline, |
5 | 6 | Qwen3_5ForConditionalGeneration, |
@@ -178,33 +179,57 @@ export const createModelSession = (postMessageToUi: WorkerMessagePoster) => { |
178 | 179 | }; |
179 | 180 |
|
180 | 181 | const loadVisionResources = async (model: ModelDescriptor) => { |
181 | | - if (model.runtime.visionLoaderKind !== "qwen3_5") { |
182 | | - throw new Error( |
183 | | - "This vision loader is not supported in the browser worker yet.", |
184 | | - ); |
| 182 | + const progressHandler = getProgressHandler(model.id); |
| 183 | + |
| 184 | + if (model.runtime.visionLoaderKind === "qwen3_5") { |
| 185 | + const [nextProcessor, nextVisionModel] = await Promise.all([ |
| 186 | + AutoProcessor.from_pretrained(model.hf.modelId, { |
| 187 | + progress_callback: progressHandler, |
| 188 | + }), |
| 189 | + Qwen3_5ForConditionalGeneration.from_pretrained(model.hf.modelId, { |
| 190 | + dtype: { |
| 191 | + embed_tokens: "q4", |
| 192 | + vision_encoder: "fp16", |
| 193 | + decoder_model_merged: "q4", |
| 194 | + }, |
| 195 | + device: "webgpu", |
| 196 | + progress_callback: progressHandler, |
| 197 | + }), |
| 198 | + ]); |
| 199 | + |
| 200 | + return { |
| 201 | + textGenerator: null, |
| 202 | + processor: nextProcessor, |
| 203 | + visionModel: nextVisionModel as VisionModelInstance, |
| 204 | + } satisfies LoadResources; |
185 | 205 | } |
186 | 206 |
|
187 | | - const progressHandler = getProgressHandler(model.id); |
188 | | - const [nextProcessor, nextVisionModel] = await Promise.all([ |
189 | | - AutoProcessor.from_pretrained(model.hf.modelId, { |
190 | | - progress_callback: progressHandler, |
191 | | - }), |
192 | | - Qwen3_5ForConditionalGeneration.from_pretrained(model.hf.modelId, { |
193 | | - dtype: { |
194 | | - embed_tokens: "q4", |
195 | | - vision_encoder: "fp16", |
196 | | - decoder_model_merged: "q4", |
197 | | - }, |
198 | | - device: "webgpu", |
199 | | - progress_callback: progressHandler, |
200 | | - }), |
201 | | - ]); |
| 207 | + if (model.runtime.visionLoaderKind === "lfm2_5_vl") { |
| 208 | + const [nextProcessor, nextVisionModel] = await Promise.all([ |
| 209 | + AutoProcessor.from_pretrained(model.hf.modelId, { |
| 210 | + progress_callback: progressHandler, |
| 211 | + }), |
| 212 | + AutoModelForImageTextToText.from_pretrained(model.hf.modelId, { |
| 213 | + dtype: { |
| 214 | + embed_tokens: "fp16", |
| 215 | + vision_encoder: "fp16", |
| 216 | + decoder_model_merged: "q4", |
| 217 | + }, |
| 218 | + device: "webgpu", |
| 219 | + progress_callback: progressHandler, |
| 220 | + }), |
| 221 | + ]); |
| 222 | + |
| 223 | + return { |
| 224 | + textGenerator: null, |
| 225 | + processor: nextProcessor, |
| 226 | + visionModel: nextVisionModel as VisionModelInstance, |
| 227 | + } satisfies LoadResources; |
| 228 | + } |
202 | 229 |
|
203 | | - return { |
204 | | - textGenerator: null, |
205 | | - processor: nextProcessor, |
206 | | - visionModel: nextVisionModel, |
207 | | - } satisfies LoadResources; |
| 230 | + throw new Error( |
| 231 | + "This vision loader is not supported in the browser worker yet.", |
| 232 | + ); |
208 | 233 | }; |
209 | 234 |
|
210 | 235 | const ensureModelReady = async (model: ModelDescriptor) => { |
|
0 commit comments