Spaces:
Running
Running
Download worker.js from Peter193518/janus-pro-webgpu: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/spaces/Peter193518/janus-pro-webgpu/resolve/main/worker.js
- Command line
-
hf download hf://spaces/Peter193518/janus-pro-webgpu/worker.js
-
curl -L -o worker.js https://huggingface.co/spaces/Peter193518/janus-pro-webgpu/resolve/main/worker.js
4.23 kB
| import { | |
| AutoProcessor, | |
| MultiModalityCausalLM, | |
| BaseStreamer, | |
| InterruptableStoppingCriteria, | |
| } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.2.0/+esm"; | |
| const MODEL_ID = "onnx-community/Janus-Pro-1B-ONNX"; | |
| let fp16_supported = false; | |
| /** | |
| * WebGPU feature detection. `shader-f16` decides whether we can use the | |
| * fp16-flavoured quantized weights; without it we fall back to int4. | |
| */ | |
| async function check() { | |
| try { | |
| if (!navigator.gpu) throw new Error("浏览器不支持 WebGPU"); | |
| const adapter = await navigator.gpu.requestAdapter(); | |
| if (!adapter) throw new Error("未找到 WebGPU 适配器"); | |
| fp16_supported = adapter.features.has("shader-f16"); | |
| self.postMessage({ status: "success", fp16_supported, adapter: adapter.info?.vendor || "" }); | |
| } catch (e) { | |
| self.postMessage({ status: "error", error: e.toString() }); | |
| } | |
| } | |
| /** Singleton pipeline: loads once, reused across generations. */ | |
| class ImageGenerationPipeline { | |
| static model_id = MODEL_ID; | |
| static async getInstance(progress_callback = null) { | |
| this.processor ??= AutoProcessor.from_pretrained(this.model_id, { | |
| progress_callback, | |
| }); | |
| this.model ??= MultiModalityCausalLM.from_pretrained(this.model_id, { | |
| dtype: fp16_supported | |
| ? { | |
| prepare_inputs_embeds: "q4", | |
| language_model: "q4f16", | |
| lm_head: "fp16", | |
| gen_head: "fp16", | |
| gen_img_embeds: "fp16", | |
| image_decode: "fp16", | |
| } | |
| : { | |
| prepare_inputs_embeds: "q4", | |
| language_model: "q4", | |
| lm_head: "fp16", | |
| gen_head: "fp16", | |
| gen_img_embeds: "fp16", | |
| image_decode: "fp16", | |
| }, | |
| device: { | |
| // Stays on WASM: the embedding gather hits a WebGPU bug upstream. | |
| prepare_inputs_embeds: "wasm", | |
| language_model: "webgpu", | |
| lm_head: "webgpu", | |
| gen_head: "webgpu", | |
| gen_img_embeds: "webgpu", | |
| image_decode: "webgpu", | |
| }, | |
| progress_callback, | |
| }); | |
| return Promise.all([this.processor, this.model]); | |
| } | |
| } | |
| /** Reports image-token progress back to the main thread. */ | |
| class ProgressStreamer extends BaseStreamer { | |
| constructor(total, on_progress) { | |
| super(); | |
| this.total = total; | |
| this.on_progress = on_progress; | |
| this.count = null; | |
| this.start_time = null; | |
| } | |
| put() { | |
| if (this.count === null) { | |
| this.count = 0; | |
| this.start_time = performance.now(); | |
| return; | |
| } | |
| this.on_progress({ | |
| count: ++this.count, | |
| total: this.total, | |
| elapsed: performance.now() - this.start_time, | |
| }); | |
| } | |
| end() {} | |
| } | |
| const stopping_criteria = new InterruptableStoppingCriteria(); | |
| async function generate({ prompt, temperature }) { | |
| const [processor, model] = await ImageGenerationPipeline.getInstance(); | |
| self.postMessage({ status: "generating" }); | |
| const inputs = await processor([{ role: "User", content: prompt }], { | |
| chat_template: "text_to_image", | |
| }); | |
| const num_image_tokens = processor.num_image_tokens; | |
| const streamer = new ProgressStreamer(num_image_tokens, (p) => { | |
| self.postMessage({ status: "progress", ...p }); | |
| }); | |
| const outputs = await model.generate_images({ | |
| ...inputs, | |
| min_new_tokens: num_image_tokens, | |
| max_new_tokens: num_image_tokens, | |
| do_sample: true, | |
| temperature, | |
| streamer, | |
| stopping_criteria, | |
| }); | |
| const blob = await outputs[0].toBlob(); | |
| self.postMessage({ status: "done", blob }); | |
| } | |
| async function load() { | |
| self.postMessage({ status: "loading" }); | |
| await ImageGenerationPipeline.getInstance((x) => { | |
| self.postMessage({ status: "loading", detail: x }); | |
| }); | |
| self.postMessage({ status: "ready" }); | |
| } | |
| self.addEventListener("message", async (e) => { | |
| const { type, data } = e.data; | |
| switch (type) { | |
| case "check": | |
| await check(); | |
| break; | |
| case "load": | |
| await load(); | |
| break; | |
| case "generate": | |
| stopping_criteria.reset(); | |
| try { | |
| await generate(data); | |
| } catch (err) { | |
| self.postMessage({ status: "error", error: err.toString() }); | |
| } | |
| break; | |
| case "interrupt": | |
| stopping_criteria.interrupt(); | |
| break; | |
| } | |
| }); | |