From 7dffb9ae910e6dd398d4af02e8580fd62cdbdbd9 Mon Sep 17 00:00:00 2001 From: ocavue Date: Sun, 8 Dec 2024 04:40:25 +1100 Subject: [PATCH] Add missing 'ready' status in the ProgressInfo type (#1070) --- src/utils/core.js | 48 ++++++++++++++++++++++++++++++++++++++--------- src/utils/hub.js | 20 ++++++++------------ 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/utils/core.js b/src/utils/core.js index aa745af4b..858721409 100644 --- a/src/utils/core.js +++ b/src/utils/core.js @@ -9,15 +9,45 @@ */ /** - * @typedef {Object} ProgressInfo - * @property {'initiate' | 'download' | 'progress' | 'done'} status The status of the progress item. - * @property {string} name This can be either: - * - a string, the *model id* of a model repo on huggingface.co. - * - a path to a *directory* potentially containing the file. - * @property {string} file The name of the file - * @property {number} [progress] A number between 0 and 100. Only available for the 'progress' status. - * @property {number} [loaded] The number of bytes loaded. Only available for the 'progress' status. - * @property {number} [total] The total number of bytes to be loaded. Only available for the 'progress' status. + * @typedef {Object} InitiateProgressInfo + * @property {'initiate'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + */ + +/** + * @typedef {Object} DownloadProgressInfo + * @property {'download'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + */ + +/** + * @typedef {Object} ProgressStatusInfo + * @property {'progress'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + * @property {number} progress A number between 0 and 100. + * @property {number} loaded The number of bytes loaded. + * @property {number} total The total number of bytes to be loaded. + */ + +/** + * @typedef {Object} DoneProgressInfo + * @property {'done'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + */ + +/** + * @typedef {Object} ReadyProgressInfo + * @property {'ready'} status + * @property {string} task The loaded task. + * @property {string} model The loaded model. + */ + +/** + * @typedef {InitiateProgressInfo | DownloadProgressInfo | ProgressStatusInfo | DoneProgressInfo | ReadyProgressInfo} ProgressInfo */ /** diff --git a/src/utils/hub.js b/src/utils/hub.js index 52e53ad25..c5fcfacf1 100755 --- a/src/utils/hub.js +++ b/src/utils/hub.js @@ -504,13 +504,6 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti file: filename }) - /** @type {import('./core.js').ProgressInfo} */ - const progressInfo = { - status: 'progress', - name: path_or_repo_id, - file: filename - } - /** @type {Uint8Array} */ let buffer; @@ -530,7 +523,9 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti // For completeness, we still fire the final progress callback dispatchCallback(options.progress_callback, { - ...progressInfo, + status: 'progress', + name: path_or_repo_id, + file: filename, progress: 100, loaded: buffer.length, total: buffer.length, @@ -538,7 +533,9 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti } else { buffer = await readResponse(response, data => { dispatchCallback(options.progress_callback, { - ...progressInfo, + status: 'progress', + name: path_or_repo_id, + file: filename, ...data, }) }) @@ -595,12 +592,11 @@ export async function getModelJSON(modelPath, fileName, fatal = true, options = return JSON.parse(jsonData); } - /** * Read and track progress when reading a Response object * - * @param {any} response The Response object to read - * @param {function} progress_callback The function to call with progress updates + * @param {Response|FileResponse} response The Response object to read + * @param {(data: {progress: number, loaded: number, total: number}) => void} progress_callback The function to call with progress updates * @returns {Promise} A Promise that resolves with the Uint8Array buffer */ async function readResponse(response, progress_callback) {