diff --git a/src/utils/hub.js b/src/utils/hub.js index ab67ea8e6..5b3bb7520 100755 --- a/src/utils/hub.js +++ b/src/utils/hub.js @@ -1,7 +1,7 @@ /** * @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models) - * + * * @module utils/hub */ @@ -19,7 +19,7 @@ import { dispatchCallback } from './core.js'; export const MAX_EXTERNAL_DATA_CHUNKS = 100; /** - * @typedef {Object} PretrainedOptions Options for loading a pretrained model. + * @typedef {Object} PretrainedOptions Options for loading a pretrained model. * @property {import('./core.js').ProgressCallback} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates. * @property {import('../configs.js').PretrainedConfig} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when: * - The model is a model provided by the library (loaded with the *model id* string of a pretrained model). @@ -158,7 +158,7 @@ class FileResponse { /** * Reads the contents of the file specified by the filePath property and returns a Promise that * resolves with a parsed JavaScript object containing the file's contents. - * + * * @returns {Promise} A Promise that resolves with a parsed JavaScript object containing the file's contents. * @throws {Error} If the file cannot be read. */ @@ -195,7 +195,7 @@ const REPO_ID_REGEX = /^(\b[\w\-.]+\b\/)?\b[\w\-.]{1,96}\b$/; /** * Tests whether a string is a valid Hugging Face model ID or not. * Adapted from https://github.com/huggingface/huggingface_hub/blob/6378820ebb03f071988a96c7f3268f5bdf8f9449/src/huggingface_hub/utils/_validators.py#L119-L170 - * + * * @param {string} string The string to test * @returns {boolean} True if the string is a valid model ID, false otherwise. */ @@ -214,9 +214,14 @@ function isValidHfModelId(string) { */ export async function getFile(urlOrPath) { - if (env.useFS && !isValidUrl(urlOrPath, ['http:', 'https:', 'blob:'])) { - return new FileResponse(urlOrPath.toString()); - + if (env.useFS && !isValidUrl(urlOrPath, ["http:", "https:", "blob:"])) { + return new FileResponse( + urlOrPath instanceof URL + ? urlOrPath.protocol === "file:" + ? urlOrPath.pathname + : urlOrPath.toString() + : urlOrPath, + ); } else if (typeof process !== 'undefined' && process?.release?.name === 'node') { const IS_CI = !!process.env?.TESTING_REMOTELY; const version = env.version; @@ -280,7 +285,7 @@ function handleError(status, remoteURL, fatal) { class FileCache { /** * Instantiate a `FileCache` object. - * @param {string} path + * @param {string} path */ constructor(path) { this.path = path; @@ -288,7 +293,7 @@ class FileCache { /** * Checks whether the given request is in the cache. - * @param {string} request + * @param {string} request * @returns {Promise} */ async match(request) { @@ -305,8 +310,8 @@ class FileCache { /** * Adds the given response to the cache. - * @param {string} request - * @param {Response} response + * @param {string} request + * @param {Response} response * @param {(data: {progress: number, loaded: number, total: number}) => void} [progress_callback] Optional. * The function to call with progress updates * @returns {Promise} @@ -364,7 +369,7 @@ class FileCache { } /** - * + * * @param {FileCache|Cache} cache The cache to search * @param {string[]} names The names of the item to search for * @returns {Promise} The item from the cache, or undefined if not found.