Skip to content

Commit d874c90

Browse files
shrirajhxenova
andauthored
Minimal fixes for type declarations to resolve errors when importing transformers.js via typescript (partially resolves #1093) (#1122)
* Explicitly declare `batch_decode` as a function with polymorphic args/returns Resolves typescript inference errors in `models/mgp_str/processing_mgp_str.js` where it does extend from `Processor` but the override returns an object instead of a string array. * Directly refer to `PretrainedProcessorOptions` in typedec for `AutoProcessor.from_pretrained` Resolves an issue with tsc where using `@type {typeof Processor.from_pretrained}` does not actually import the `PretrainedProcessorOptions` type alias defined in `processing_utils.js`. * Explicitly define third argument for Phi3VProcessor._call Resolves a typescript error where typescript tries to destructure an array directly into padding/truncation/num_crops. Also improves the type safety of calling this function. * Fix decode type * Re-use `from_pretrained` types where possible --------- Co-authored-by: Joshua Lochner <[email protected]>
1 parent c845bb5 commit d874c90

File tree

6 files changed

+13
-45
lines changed

6 files changed

+13
-45
lines changed

src/base/feature_extraction_utils.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ export class FeatureExtractor extends Callable {
1717
}
1818

1919
/**
20-
* Instantiate one of the processor classes of the library from a pretrained model.
20+
* Instantiate one of the feature extractor classes of the library from a pretrained model.
2121
*
22-
* The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy)
23-
* property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
22+
* The feature extractor class to instantiate is selected based on the `feature_extractor_type` property of
23+
* the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
2424
*
2525
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
26-
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.
26+
* - A string, the *model id* of a pretrained feature_extractor hosted inside a model repo on huggingface.co.
2727
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
2828
* user or organization name, like `dbmdz/bert-base-german-cased`.
29-
* - A path to a *directory* containing processor files, e.g., `./my_model_directory/`.
30-
* @param {import('../utils/hub.js').PretrainedOptions} options Additional options for loading the processor.
29+
* - A path to a *directory* containing feature_extractor files, e.g., `./my_model_directory/`.
30+
* @param {import('../utils/hub.js').PretrainedOptions} options Additional options for loading the feature_extractor.
3131
*
32-
* @returns {Promise<FeatureExtractor>} A new instance of the Processor class.
32+
* @returns {Promise<FeatureExtractor>} A new instance of the Feature Extractor class.
3333
*/
3434
static async from_pretrained(pretrained_model_name_or_path, options) {
35-
const preprocessorConfig = await getModelJSON(pretrained_model_name_or_path, FEATURE_EXTRACTOR_NAME, true, options);
36-
return new this(preprocessorConfig);
35+
const config = await getModelJSON(pretrained_model_name_or_path, FEATURE_EXTRACTOR_NAME, true, options);
36+
return new this(config);
3737
}
3838
}
3939

src/base/processing_utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ export class Processor extends Callable {
121121
/**
122122
* Instantiate one of the processor classes of the library from a pretrained model.
123123
*
124-
* The processor class to instantiate is selected based on the `feature_extractor_type` property of the config object
125-
* (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
124+
* The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy)
125+
* property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
126126
*
127127
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
128128
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.

src/models/auto/feature_extraction_auto.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,6 @@ import * as AllFeatureExtractors from '../feature_extractors.js';
66

77
export class AutoFeatureExtractor {
88

9-
/**
10-
* Instantiate one of the feature extractor classes of the library from a pretrained model.
11-
*
12-
* The processor class to instantiate is selected based on the `feature_extractor_type` property of
13-
* the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
14-
*
15-
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
16-
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.
17-
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
18-
* user or organization name, like `dbmdz/bert-base-german-cased`.
19-
* - A path to a *directory* containing processor files, e.g., `./my_model_directory/`.
20-
* @param {import('../../utils/hub.js').PretrainedOptions} options Additional options for loading the processor.
21-
*
22-
* @returns {Promise<AllFeatureExtractors.ImageProcessor>} A new instance of the Processor class.
23-
*/
24-
259
/** @type {typeof FeatureExtractor.from_pretrained} */
2610
static async from_pretrained(pretrained_model_name_or_path, options={}) {
2711

src/models/auto/processing_auto.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ import * as AllFeatureExtractors from '../feature_extractors.js';
4040
*/
4141
export class AutoProcessor {
4242

43-
/**
44-
* Instantiate one of the processor classes of the library from a pretrained model.
45-
*
46-
* The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy)
47-
* property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
48-
*
49-
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
50-
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.
51-
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
52-
* user or organization name, like `dbmdz/bert-base-german-cased`.
53-
* - A path to a *directory* containing processor files, e.g., `./my_model_directory/`.
54-
* @param {import('../../utils/hub.js').PretrainedOptions} options Additional options for loading the processor.
55-
*
56-
* @returns {Promise<Processor>} A new instance of the Processor class.
57-
*/
58-
5943
/** @type {typeof Processor.from_pretrained} */
6044
static async from_pretrained(pretrained_model_name_or_path, options={}) {
6145

src/models/phi3_v/processing_phi3_v.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Phi3VProcessor extends Processor {
1414
*
1515
* @param {string|string[]} text
1616
* @param {RawImage|RawImage[]} images
17-
* @param {...any} args
17+
* @param { { padding?: boolean, truncation?: boolean, num_crops?: number } | undefined } options
1818
* @returns {Promise<any>}
1919
*/
2020
async _call(text, images = null, {

src/pipelines.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,7 @@ export class AutomaticSpeechRecognitionPipeline extends (/** @type {new (options
19161916
const max_new_tokens = Math.floor(aud.length / sampling_rate) * 6;
19171917
const outputs = await this.model.generate({ max_new_tokens, ...kwargs, ...inputs });
19181918

1919-
const text = this.processor.batch_decode(outputs, { skip_special_tokens: true })[0];
1919+
const text = this.processor.batch_decode(/** @type {Tensor} */(outputs), { skip_special_tokens: true })[0];
19201920
toReturn.push({ text });
19211921
}
19221922
return single ? toReturn[0] : toReturn;

0 commit comments

Comments
 (0)