diff options
author | Radamés Ajna <radamajna@gmail.com> | 2023-09-05 20:53:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-06 05:53:31 +0200 |
commit | 16bf44f6e9ed89d68260baa7914277fa269dcaee (patch) | |
tree | 07d4752fa7f2a9e1f3248418253ab19baacfac6a /candle-wasm-examples/yolo | |
parent | a4f40f3dc881802daa973c8f4f89d133cfa25e2b (diff) | |
download | candle-16bf44f6e9ed89d68260baa7914277fa269dcaee.tar.gz candle-16bf44f6e9ed89d68260baa7914277fa269dcaee.tar.bz2 candle-16bf44f6e9ed89d68260baa7914277fa269dcaee.zip |
force model cache (#751)
Diffstat (limited to 'candle-wasm-examples/yolo')
-rw-r--r-- | candle-wasm-examples/yolo/yoloWorker.js | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/candle-wasm-examples/yolo/yoloWorker.js b/candle-wasm-examples/yolo/yoloWorker.js index 93097372..8b5ef8b9 100644 --- a/candle-wasm-examples/yolo/yoloWorker.js +++ b/candle-wasm-examples/yolo/yoloWorker.js @@ -1,6 +1,19 @@ //load the candle yolo wasm module import init, { Model, ModelPose } from "./build/m.js"; +async function fetchArrayBuffer(url) { + const cacheName = "yolo-candle-cache"; + const cache = await caches.open(cacheName); + const cachedResponse = await cache.match(url); + if (cachedResponse) { + const data = await cachedResponse.arrayBuffer(); + return new Uint8Array(data); + } + const res = await fetch(url, { cache: "force-cache" }); + cache.put(url, res.clone()); + return new Uint8Array(await res.arrayBuffer()); +} + class Yolo { static instance = {}; // Retrieve the YOLO model. When called for the first time, @@ -11,9 +24,7 @@ class Yolo { await init(); self.postMessage({ status: `loading model ${modelID}:${modelSize}` }); - const modelRes = await fetch(modelURL); - const yoloArrayBuffer = await modelRes.arrayBuffer(); - const weightsArrayU8 = new Uint8Array(yoloArrayBuffer); + const weightsArrayU8 = await fetchArrayBuffer(modelURL); if (/pose/.test(modelID)) { // if pose model, use ModelPose this.instance[modelID] = new ModelPose(weightsArrayU8, modelSize); |