Incorporo-user commited on
Commit
aaa2328
·
verified ·
1 Parent(s): b0dc8c9

Count every model load, like from_pretrained

Browse files
Files changed (1) hide show
  1. js/agate.js +4 -9
js/agate.js CHANGED
@@ -63,14 +63,9 @@ export async function hasCachedModel() {
63
  export async function clearCache() { try { return await caches.delete(CACHE_NAME); } catch { return false; } }
64
 
65
  // The Hub counts a model download only on a request for the model repo's root config.json; the ONNX files
66
- // come from this Space, so each browser reads that config once (remembered in localStorage) to be counted.
67
- const COUNT_KEY = "agate-preview-001-counted";
68
- function countOnce() {
69
- try { if (localStorage.getItem(COUNT_KEY)) return; } catch { /* storage blocked: still count this load */ }
70
- fetch(COUNT_URL, { cache: "no-store" })
71
- .then((r) => { if (r.ok) { try { localStorage.setItem(COUNT_KEY, String(Date.now())); } catch { /* ignore */ } } })
72
- .catch(() => {});
73
- }
74
 
75
  // ---- the model ---------------------------------------------------------------------------------
76
  export class Agate {
@@ -100,7 +95,7 @@ export class Agate {
100
  bufs[f] = buf;
101
  }
102
  this.stats = { downloadMB: total / 1e6, fromCacheMB: fromCache / 1e6, downloadMs: performance.now() - t0 };
103
- countOnce();
104
  // drop cached files of older model versions (keys carry the sha256)
105
  try {
106
  const cache = await openCache(), keep = new Set(MODEL_FILES.map((f) => new URL(`${this.base}${real(f)}?sha256=${manifest.files[real(f)].sha256}`, location.href).href));
 
63
  export async function clearCache() { try { return await caches.delete(CACHE_NAME); } catch { return false; } }
64
 
65
  // The Hub counts a model download only on a request for the model repo's root config.json; the ONNX files
66
+ // come from this Space, so every model load reads it once -- the same per-load count a Python
67
+ // from_pretrained() produces, cached weights or not.
68
+ function countLoad() { fetch(COUNT_URL, { cache: "no-store" }).catch(() => {}); }
 
 
 
 
 
69
 
70
  // ---- the model ---------------------------------------------------------------------------------
71
  export class Agate {
 
95
  bufs[f] = buf;
96
  }
97
  this.stats = { downloadMB: total / 1e6, fromCacheMB: fromCache / 1e6, downloadMs: performance.now() - t0 };
98
+ countLoad();
99
  // drop cached files of older model versions (keys carry the sha256)
100
  try {
101
  const cache = await openCache(), keep = new Set(MODEL_FILES.map((f) => new URL(`${this.base}${real(f)}?sha256=${manifest.files[real(f)].sha256}`, location.href).href));