MCPcopy Index your code
hub / github.com/huggingface/transformers.js / loadResourceFile

Function loadResourceFile

packages/transformers/src/utils/hub.js:245–474  ·  view source on GitHub ↗
(
    path_or_repo_id,
    filename,
    fatal = true,
    options = {},
    return_path = false,
    cache = null,
)

Source from the content-addressed store, hash-verified

243 * @returns {Promise<string|Uint8Array|null>} A Promise that resolves with the file content as a Uint8Array if `return_path` is false, or the file path as a string if `return_path` is true.
244 */
245export async function loadResourceFile(
246 path_or_repo_id,
247 filename,
248 fatal = true,
249 options = {},
250 return_path = false,
251 cache = null,
252) {
253 const { requestURL, localPath, remoteURL, proposedCacheKey, validModelId } = buildResourcePaths(
254 path_or_repo_id,
255 filename,
256 options,
257 cache,
258 );
259
260 /** @type {string} */
261 let cacheKey;
262
263 // Whether to cache the final response in the end.
264 let toCacheResponse = false;
265
266 /** @type {Response|import('./hub/FileResponse.js').FileResponse|undefined|string} */
267 let response;
268
269 // Check cache
270 response = await checkCachedResource(cache, localPath, proposedCacheKey);
271
272 const cacheHit = response !== undefined;
273 if (cacheHit) {
274 cacheKey = proposedCacheKey;
275 } else {
276 // Caching not available, or file is not cached, so we perform the request
277
278 if (env.allowLocalModels) {
279 // Accessing local models is enabled, so we try to get the file locally.
280 // If request is a valid HTTP URL, we skip the local file check. Otherwise, we try to get the file locally.
281 const isURL = isValidUrl(requestURL, ['http:', 'https:']);
282 if (!isURL) {
283 try {
284 response = await getFile(localPath);
285 cacheKey = localPath; // Update the cache key to be the local path
286 } catch (e) {
287 // Something went wrong while trying to get the file locally.
288 // NOTE: error handling is done in the next step (since `response` will be undefined)
289 logger.warn(`Unable to load from local path "${localPath}": "${e}"`);
290 }
291 } else if (options.local_files_only) {
292 throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`);
293 } else if (!env.allowRemoteModels) {
294 throw new Error(
295 `\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`,
296 );
297 }
298 }
299
300 if (response === undefined || (typeof response !== 'string' && response.status === 404)) {
301 // File not found locally. This means either:
302 // - The user has disabled local file access (`env.allowLocalModels=false`)

Callers 1

getModelFileFunction · 0.85

Calls 12

isValidUrlFunction · 0.90
handleErrorFunction · 0.90
dispatchCallbackFunction · 0.90
get_file_metadataFunction · 0.90
readResponseFunction · 0.90
buildResourcePathsFunction · 0.85
checkCachedResourceFunction · 0.85
getFileFunction · 0.85
storeCachedResourceFunction · 0.85
arrayBufferMethod · 0.80
getMethod · 0.80
matchMethod · 0.45

Tested by

no test coverage detected