Skip to content

Commit

Permalink
Fix caching for LFS files from the Hugging Face Hub (#251)
Browse files Browse the repository at this point in the history
* Fix model caching for LFS files from the HF Hub

* Ignore local model check on demo site
  • Loading branch information
xenova authored Aug 22, 2023
1 parent f61cc66 commit 9c449c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion examples/demo-site/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// Needed to ensure the UI thread is not blocked when running //
/////////////////////////////////////////////////////////////////

import { pipeline } from "@xenova/transformers";
import { pipeline, env } from "@xenova/transformers";
env.allowLocalModels = false;

// Define task function mapping
const TASK_FUNCTION_MAPPING = {
Expand Down
24 changes: 13 additions & 11 deletions src/utils/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
let cacheKey;
let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL;

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

/** @type {Response|FileResponse|undefined} */
let response;
Expand Down Expand Up @@ -475,14 +475,14 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
cacheKey = proposedCacheKey;
}


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


// Start downloading
dispatchCallback(options.progress_callback, {
status: 'download',
Expand All @@ -499,16 +499,18 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
})
})


if (
// Only cache web responses
// i.e., do not cache FileResponses (prevents duplication)
responseToCache && cacheKey
toCacheResponse && cacheKey
&&
// Check again whether request is in cache. If not, we add the response to the cache
(await cache.match(cacheKey) === undefined)
) {
await cache.put(cacheKey, responseToCache)
// NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files
await cache.put(cacheKey, new Response(buffer, {
headers: response.headers
}))
.catch(err => {
// Do not crash if unable to add to cache (e.g., QuotaExceededError).
// Rather, log a warning and proceed with execution.
Expand Down

0 comments on commit 9c449c1

Please sign in to comment.