Download worker.js from linoyts/Jeveration-WebGPU-StyleTransfer: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/spaces/linoyts/Jeveration-WebGPU-StyleTransfer/resolve/main/worker.js
- Command line
-
hf download hf://spaces/linoyts/Jeveration-WebGPU-StyleTransfer/worker.js
-
curl -L -o worker.js https://huggingface.co/spaces/linoyts/Jeveration-WebGPU-StyleTransfer/resolve/main/worker.js
2.81 kB
| // Runs the whole pipeline off the main thread. Protocol: check -> load -> (image) -> generate, with status/progress | |
| // messages. Generate requests are "latest wins": while one runs, newer requests replace each other, so live editing never | |
| // builds a backlog. The style-transfer photo is sent once ("image") and kept here, so its visual tokens are reused. | |
| import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.bundle.min.mjs"; | |
| import { HubFiles } from "./hub.js"; | |
| import { JeverationPipeline } from "./jeveration.js"; | |
| const MODEL_ID = "linoyts/Jeveration-int8-ONNX", REVISION = "i2i"; | |
| ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/"; | |
| ort.env.webgpu.powerPreference = "high-performance"; | |
| let pipe = null, busy = false, pending = null, photo = null, skipped = 0; | |
| const post = (m) => self.postMessage(m); | |
| async function check() { | |
| if (!navigator.gpu) throw new Error("WebGPU is not supported by this browser."); | |
| const adapter = await navigator.gpu.requestAdapter({ powerPreference: "high-performance" }); | |
| if (!adapter) throw new Error("WebGPU is not supported (no adapter found)."); | |
| if (!adapter.features.has("shader-f16")) throw new Error("This GPU does not support fp16 shaders (shader-f16), which this model needs."); | |
| const info = adapter.info ?? {}; | |
| return { vendor: info.vendor, architecture: info.architecture, maxBufferSize: adapter.limits.maxBufferSize }; | |
| } | |
| async function drain() { | |
| if (busy) return; | |
| busy = true; | |
| while (pending) { | |
| const req = pending; pending = null; | |
| try { | |
| const out = req.mode === "i2i" ? await pipe.imageToImage(photo, req.task, { seed: req.seed }) | |
| : await pipe.generate(req.prompt, { seed: req.seed }); | |
| post({ status: "image", data: { rgb: out.rgb, timings: out.timings, mode: req.mode, id: req.id, skipped: req.skipped } }); | |
| } catch (e) { | |
| post({ status: "error", data: String(e?.message ?? e) }); | |
| } | |
| } | |
| busy = false; | |
| } | |
| self.onmessage = async ({ data: { type, data } }) => { | |
| try { | |
| if (type === "check") post({ status: "checked", data: await check() }); | |
| if (type === "load") { | |
| const t0 = performance.now(); | |
| pipe = await JeverationPipeline.load(new HubFiles(MODEL_ID, { revision: REVISION, cacheName: "jeveration-webgpu" }), { | |
| ort, device: "webgpu", backbone: "backbone_int8_long/manifest.json", i2i: true, progress: (e) => post(e), | |
| }); | |
| post({ status: "ready", data: { seconds: (performance.now() - t0) / 1000 } }); | |
| } | |
| if (type === "image") photo = data.rgb; | |
| if (type === "generate") { | |
| if (pending) skipped++; | |
| pending = { ...data, skipped }; | |
| drain(); | |
| } | |
| } catch (e) { | |
| post({ status: "error", data: String(e?.message ?? e) }); | |
| } | |
| }; | |