Skip to content

Commit d38134d

Browse files
ocavuexenova
andauthored
Add type declaration for progress_callback (#1034)
* Add type declaration for `progress_callback` * export ProgressCallback and ProgressInfo * Update JSDoc --------- Co-authored-by: Joshua Lochner <[email protected]>
1 parent 82bae5e commit d38134d

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/configs.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ import {
3636
* @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions
3737
*/
3838

39+
/**
40+
* @typedef {import('./utils/core.js').ProgressCallback} ProgressCallback
41+
*/
42+
43+
/**
44+
* @typedef {import('./utils/core.js').ProgressInfo} ProgressInfo
45+
*/
3946

4047
/**
4148
* Loads a config from the specified path.

src/utils/core.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,30 @@
88
* @module utils/core
99
*/
1010

11+
/**
12+
* @typedef {Object} ProgressInfo
13+
* @property {'initiate' | 'download' | 'progress' | 'done'} status The status of the progress item.
14+
* @property {string} name This can be either:
15+
* - a string, the *model id* of a model repo on huggingface.co.
16+
* - a path to a *directory* potentially containing the file.
17+
* @property {string} file The name of the file
18+
* @property {number} [progress] A number between 0 and 100. Only available for the 'progress' status.
19+
* @property {number} [loaded] The number of bytes loaded. Only available for the 'progress' status.
20+
* @property {number} [total] The total number of bytes to be loaded. Only available for the 'progress' status.
21+
*/
22+
23+
/**
24+
* A callback function that is called with progress information.
25+
* @callback ProgressCallback
26+
* @param {ProgressInfo} progressInfo
27+
* @returns {void}
28+
*/
29+
1130
/**
1231
* Helper function to dispatch progress callbacks.
1332
*
14-
* @param {Function} progress_callback The progress callback function to dispatch.
15-
* @param {any} data The data to pass to the progress callback function.
33+
* @param {ProgressCallback | null | undefined} progress_callback The progress callback function to dispatch.
34+
* @param {ProgressInfo} data The data to pass to the progress callback function.
1635
* @returns {void}
1736
* @private
1837
*/

src/utils/hub.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { dispatchCallback } from './core.js';
1313

1414
/**
1515
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
16-
* @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
16+
* @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.
1717
* @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:
1818
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
1919
* - 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.
@@ -504,6 +504,7 @@ export async function getModelFile(path_or_repo_id, filename, fatal = true, opti
504504
file: filename
505505
})
506506

507+
/** @type {import('./core.js').ProgressInfo} */
507508
const progressInfo = {
508509
status: 'progress',
509510
name: path_or_repo_id,

0 commit comments

Comments
 (0)