Skip to content

Commit 9c449c1

Browse files
authored
Fix caching for LFS files from the Hugging Face Hub (#251)
* Fix model caching for LFS files from the HF Hub * Ignore local model check on demo site
1 parent f61cc66 commit 9c449c1

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

examples/demo-site/src/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// Needed to ensure the UI thread is not blocked when running //
55
/////////////////////////////////////////////////////////////////
66

7-
import { pipeline } from "@xenova/transformers";
7+
import { pipeline, env } from "@xenova/transformers";
8+
env.allowLocalModels = false;
89

910
// Define task function mapping
1011
const TASK_FUNCTION_MAPPING = {

src/utils/hub.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
410410
let cacheKey;
411411
let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL;
412412

413-
/** @type {Response|undefined} */
414-
let responseToCache;
413+
// Whether to cache the final response in the end.
414+
let toCacheResponse = false;
415415

416416
/** @type {Response|FileResponse|undefined} */
417417
let response;
@@ -475,14 +475,14 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
475475
cacheKey = proposedCacheKey;
476476
}
477477

478-
479-
if (cache && response instanceof Response && response.status === 200) {
480-
// only clone if cache available, and response is valid
481-
responseToCache = response.clone();
482-
}
478+
// Only cache the response if:
479+
toCacheResponse =
480+
cache // 1. A caching system is available
481+
&& typeof Response !== 'undefined' // 2. `Response` is defined (i.e., we are in a browser-like environment)
482+
&& response instanceof Response // 3. result is a `Response` object (i.e., not a `FileResponse`)
483+
&& response.status === 200 // 4. request was successful (status code 200)
483484
}
484485

485-
486486
// Start downloading
487487
dispatchCallback(options.progress_callback, {
488488
status: 'download',
@@ -499,16 +499,18 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
499499
})
500500
})
501501

502-
503502
if (
504503
// Only cache web responses
505504
// i.e., do not cache FileResponses (prevents duplication)
506-
responseToCache && cacheKey
505+
toCacheResponse && cacheKey
507506
&&
508507
// Check again whether request is in cache. If not, we add the response to the cache
509508
(await cache.match(cacheKey) === undefined)
510509
) {
511-
await cache.put(cacheKey, responseToCache)
510+
// NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files
511+
await cache.put(cacheKey, new Response(buffer, {
512+
headers: response.headers
513+
}))
512514
.catch(err => {
513515
// Do not crash if unable to add to cache (e.g., QuotaExceededError).
514516
// Rather, log a warning and proceed with execution.

0 commit comments

Comments
 (0)