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

Function getModelFile

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

Source from the content-addressed store, hash-verified

492 * @returns {Promise<string|Uint8Array>} 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.
493 */
494export async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}, return_path = false) {
495 if (!env.allowLocalModels) {
496 // User has disabled local models, so we just make sure other settings are correct.
497
498 if (options.local_files_only) {
499 throw Error(
500 'Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).',
501 );
502 } else if (!env.allowRemoteModels) {
503 throw Error(
504 'Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.',
505 );
506 }
507 }
508
509 dispatchCallback(options.progress_callback, {
510 status: 'initiate',
511 name: path_or_repo_id,
512 file: filename,
513 });
514
515 // Deduplicate concurrent loads of the same file so progress events are
516 // only emitted by a single download. Without this, parallel callers
517 // (e.g. tokenizer + processor in pipeline()) would race on the same file
518 // and produce interleaved progress that breaks monotonic progress_total.
519 const key = `${path_or_repo_id}::${filename}`;
520 let pending = INFLIGHT_LOADS.get(key);
521 if (!pending) {
522 /** @type {import('./cache.js').CacheInterface | null} */
523 const cache = await getCache(options?.cache_dir);
524 pending = loadResourceFile(path_or_repo_id, filename, fatal, options, return_path, cache).then(
525 (result) => {
526 INFLIGHT_LOADS.delete(key);
527 return result;
528 },
529 (err) => {
530 INFLIGHT_LOADS.delete(key);
531 throw err;
532 },
533 );
534 INFLIGHT_LOADS.set(key, pending);
535 }
536 return await pending;
537}
538
539/**
540 * Fetches a text file from a given path and file name.

Callers 4

getCoreModelFileFunction · 0.90
getModelDataFilesFunction · 0.90
cache.test.jsFile · 0.90
getModelTextFunction · 0.85

Calls 5

dispatchCallbackFunction · 0.90
getCacheFunction · 0.90
loadResourceFileFunction · 0.85
getMethod · 0.80
deleteMethod · 0.45

Tested by

no test coverage detected