Skip to content

Commit

Permalink
Add type declaration for progress_callback (#1034)
Browse files Browse the repository at this point in the history
* Add type declaration for `progress_callback`

* export ProgressCallback and ProgressInfo

* Update JSDoc

---------

Co-authored-by: Joshua Lochner <[email protected]>
  • Loading branch information
ocavue and xenova authored Nov 25, 2024
1 parent 82bae5e commit d38134d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ import {
* @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions
*/

/**
* @typedef {import('./utils/core.js').ProgressCallback} ProgressCallback
*/

/**
* @typedef {import('./utils/core.js').ProgressInfo} ProgressInfo
*/

/**
* Loads a config from the specified path.
Expand Down
23 changes: 21 additions & 2 deletions src/utils/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,30 @@
* @module utils/core
*/

/**
* @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.
*/

/**
* A callback function that is called with progress information.
* @callback ProgressCallback
* @param {ProgressInfo} progressInfo
* @returns {void}
*/

/**
* Helper function to dispatch progress callbacks.
*
* @param {Function} progress_callback The progress callback function to dispatch.
* @param {any} data The data to pass to the progress callback function.
* @param {ProgressCallback | null | undefined} progress_callback The progress callback function to dispatch.
* @param {ProgressInfo} data The data to pass to the progress callback function.
* @returns {void}
* @private
*/
Expand Down
3 changes: 2 additions & 1 deletion src/utils/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { dispatchCallback } from './core.js';

/**
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
* @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
* @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).
* - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory.
Expand Down Expand Up @@ -504,6 +504,7 @@ 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,
Expand Down

0 comments on commit d38134d

Please sign in to comment.