lmz radames commited on
Commit
f01442d
1 Parent(s): 6b37b03

force cache using cache api (#6)

Browse files

- force cache using cache api (b00734499ee1c83d967229634a1257b7956246aa)


Co-authored-by: Radamés Ajna <radames@users.noreply.huggingface.co>

Files changed (1) hide show
  1. yoloWorker.js +14 -3
yoloWorker.js CHANGED
@@ -1,6 +1,19 @@
1
  //load the candle yolo wasm module
2
  import init, { Model, ModelPose } from "./build/m.js";
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  class Yolo {
5
  static instance = {};
6
  // Retrieve the YOLO model. When called for the first time,
@@ -11,9 +24,7 @@ class Yolo {
11
  await init();
12
 
13
  self.postMessage({ status: `loading model ${modelID}:${modelSize}` });
14
- const modelRes = await fetch(modelURL);
15
- const yoloArrayBuffer = await modelRes.arrayBuffer();
16
- const weightsArrayU8 = new Uint8Array(yoloArrayBuffer);
17
  if (/pose/.test(modelID)) {
18
  // if pose model, use ModelPose
19
  this.instance[modelID] = new ModelPose(weightsArrayU8, modelSize);
 
1
  //load the candle yolo wasm module
2
  import init, { Model, ModelPose } from "./build/m.js";
3
 
4
+ async function fetchArrayBuffer(url) {
5
+ const cacheName = "yolo-candle-cache";
6
+ const cache = await caches.open(cacheName);
7
+ const cachedResponse = await cache.match(url);
8
+ if (cachedResponse) {
9
+ const data = await cachedResponse.arrayBuffer();
10
+ return new Uint8Array(data);
11
+ }
12
+ const res = await fetch(url, { cache: "force-cache" });
13
+ cache.put(url, res.clone());
14
+ return new Uint8Array(await res.arrayBuffer());
15
+ }
16
+
17
  class Yolo {
18
  static instance = {};
19
  // Retrieve the YOLO model. When called for the first time,
 
24
  await init();
25
 
26
  self.postMessage({ status: `loading model ${modelID}:${modelSize}` });
27
+ const weightsArrayU8 = await fetchArrayBuffer(modelURL);
 
 
28
  if (/pose/.test(modelID)) {
29
  // if pose model, use ModelPose
30
  this.instance[modelID] = new ModelPose(weightsArrayU8, modelSize);