Spaces:
Running
Running
Read the model config once per browser for the download counter
Browse files- js/agate.js +11 -3
js/agate.js
CHANGED
|
@@ -62,6 +62,16 @@ export async function hasCachedModel() {
|
|
| 62 |
|
| 63 |
export async function clearCache() { try { return await caches.delete(CACHE_NAME); } catch { return false; } }
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
// ---- the model ---------------------------------------------------------------------------------
|
| 66 |
export class Agate {
|
| 67 |
constructor({ base = "./models/", ep = "webgpu", variant = null, optLevel = "all" } = {}) {
|
|
@@ -90,9 +100,7 @@ export class Agate {
|
|
| 90 |
bufs[f] = buf;
|
| 91 |
}
|
| 92 |
this.stats = { downloadMB: total / 1e6, fromCacheMB: fromCache / 1e6, downloadMs: performance.now() - t0 };
|
| 93 |
-
|
| 94 |
-
// come from this Space, so a first (uncached) load pings it once to show up on the model page.
|
| 95 |
-
if (fromCache < total) fetch(COUNT_URL, { method: "HEAD", cache: "no-store" }).catch(() => {});
|
| 96 |
// drop cached files of older model versions (keys carry the sha256)
|
| 97 |
try {
|
| 98 |
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));
|
|
|
|
| 62 |
|
| 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 {
|
| 77 |
constructor({ base = "./models/", ep = "webgpu", variant = null, optLevel = "all" } = {}) {
|
|
|
|
| 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));
|