Transformers.js 文档

pipelines

您正在查看 main 版本,该版本需要从源代码安装。如果您想要常规 npm 安装,请查看最新的稳定版本 (v3.0.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

pipelines

Pipelines 提供了一个高级、易于使用的 API,用于运行机器学习模型。

示例: 使用 pipeline 函数实例化 pipeline。

import { pipeline } from '@huggingface/transformers';

const classifier = await pipeline('sentiment-analysis');
const output = await classifier('I love transformers!');
// [{'label': 'POSITIVE', 'score': 0.999817686}]

pipelines.Pipeline

Pipeline 类是所有 pipeline 继承的类。请参阅此类以获取不同 pipeline 之间共享的方法。

Kind: pipelines 的静态类


new Pipeline(options)

创建一个新的 Pipeline。

参数类型默认值描述
optionsObject

包含以下属性的对象

[options.task]string

pipeline 的任务。用于指定子任务。

[options.model]PreTrainedModel

pipeline 使用的模型。

[options.tokenizer]PreTrainedTokenizer

pipeline 使用的分词器(如果有)。

[options.processor]Processor

pipeline 使用的处理器(如果有)。


pipeline.dispose() : <code> DisposeType </code>

Kind: Pipeline 的实例方法


pipelines.TextClassificationPipeline

文本分类 pipeline,使用任何 ModelForSequenceClassification

示例: 使用 Xenova/distilbert-base-uncased-finetuned-sst-2-english 进行情感分析。

const classifier = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english');
const output = await classifier('I love transformers!');
// [{ label: 'POSITIVE', score: 0.999788761138916 }]

示例: 使用 Xenova/bert-base-multilingual-uncased-sentiment 进行多语言情感分析(并返回前 5 个类别)。

const classifier = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');
const output = await classifier('Le meilleur film de tous les temps.', { top_k: 5 });
// [
//   { label: '5 stars', score: 0.9610759615898132 },
//   { label: '4 stars', score: 0.03323351591825485 },
//   { label: '3 stars', score: 0.0036155181005597115 },
//   { label: '1 star', score: 0.0011325967498123646 },
//   { label: '2 stars', score: 0.0009423971059732139 }
// ]

示例: 使用 Xenova/toxic-bert 进行有毒评论分类(并返回所有类别)。

const classifier = await pipeline('text-classification', 'Xenova/toxic-bert');
const output = await classifier('I hate you!', { top_k: null });
// [
//   { label: 'toxic', score: 0.9593140482902527 },
//   { label: 'insult', score: 0.16187334060668945 },
//   { label: 'obscene', score: 0.03452680632472038 },
//   { label: 'identity_hate', score: 0.0223250575363636 },
//   { label: 'threat', score: 0.019197041168808937 },
//   { label: 'severe_toxic', score: 0.005651099607348442 }
// ]

Kind: pipelines 的静态类


new TextClassificationPipeline(options)

创建一个新的 TextClassificationPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


textClassificationPipeline._call() : <code> TextClassificationPipelineCallback </code>

Kind: TextClassificationPipeline 的实例方法


pipelines.TokenClassificationPipeline

命名实体识别 pipeline,使用任何 ModelForTokenClassification

示例: 使用 Xenova/bert-base-NER 执行命名实体识别。

const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER');
const output = await classifier('My name is Sarah and I live in London');
// [
//   { entity: 'B-PER', score: 0.9980202913284302, index: 4, word: 'Sarah' },
//   { entity: 'B-LOC', score: 0.9994474053382874, index: 9, word: 'London' }
// ]

示例: 使用 Xenova/bert-base-NER 执行命名实体识别(并返回所有标签)。

const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER');
const output = await classifier('Sarah lives in the United States of America', { ignore_labels: [] });
// [
//   { entity: 'B-PER', score: 0.9966587424278259, index: 1, word: 'Sarah' },
//   { entity: 'O', score: 0.9987385869026184, index: 2, word: 'lives' },
//   { entity: 'O', score: 0.9990072846412659, index: 3, word: 'in' },
//   { entity: 'O', score: 0.9988298416137695, index: 4, word: 'the' },
//   { entity: 'B-LOC', score: 0.9995510578155518, index: 5, word: 'United' },
//   { entity: 'I-LOC', score: 0.9990395307540894, index: 6, word: 'States' },
//   { entity: 'I-LOC', score: 0.9986724853515625, index: 7, word: 'of' },
//   { entity: 'I-LOC', score: 0.9975294470787048, index: 8, word: 'America' }
// ]

Kind: pipelines 的静态类


new TokenClassificationPipeline(options)

创建一个新的 TokenClassificationPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


tokenClassificationPipeline._call() : <code> TokenClassificationPipelineCallback </code>

Kind: TokenClassificationPipeline 的实例方法


pipelines.QuestionAnsweringPipeline

问答 pipeline,使用任何 ModelForQuestionAnswering

示例: 使用 Xenova/distilbert-base-uncased-distilled-squad 运行问答。

const answerer = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad');
const question = 'Who was Jim Henson?';
const context = 'Jim Henson was a nice puppet.';
const output = await answerer(question, context);
// {
//   answer: "a nice puppet",
//   score: 0.5768911502526741
// }

Kind: pipelines 的静态类


new QuestionAnsweringPipeline(options)

创建一个新的 QuestionAnsweringPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


Kind: instance method of QuestionAnsweringPipeline


pipelines.FillMaskPipeline

Masked language modeling prediction pipeline using any ModelWithLMHead.

Example: Perform masked language modelling (a.k.a. “fill-mask”) with Xenova/bert-base-uncased.

const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased');
const output = await unmasker('The goal of life is [MASK].');
// [
//   { token_str: 'survival', score: 0.06137419492006302, token: 8115, sequence: 'The goal of life is survival.' },
//   { token_str: 'love', score: 0.03902450203895569, token: 1567, sequence: 'The goal of life is love.' },
//   { token_str: 'happiness', score: 0.03253183513879776, token: 9266, sequence: 'The goal of life is happiness.' },
//   { token_str: 'freedom', score: 0.018736306577920914, token: 4438, sequence: 'The goal of life is freedom.' },
//   { token_str: 'life', score: 0.01859794743359089, token: 1297, sequence: 'The goal of life is life.' }
// ]

Example: Perform masked language modelling (a.k.a. “fill-mask”) with Xenova/bert-base-cased (and return top result).

const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased');
const output = await unmasker('The Milky Way is a [MASK] galaxy.', { top_k: 1 });
// [{ token_str: 'spiral', score: 0.6299987435340881, token: 14061, sequence: 'The Milky Way is a spiral galaxy.' }]

Kind: pipelines 的静态类


new FillMaskPipeline(options)

Create a new FillMaskPipeline.

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


fillMaskPipeline._call() : <code> FillMaskPipelineCallback </code>

Kind: instance method of FillMaskPipeline


pipelines.Text2TextGenerationPipeline

Text2TextGenerationPipeline 类,用于使用执行文本到文本生成任务的模型生成文本。

示例: 文本到文本生成,使用 Xenova/LaMini-Flan-T5-783M

const generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M');
const output = await generator('how can I become more healthy?', {
  max_new_tokens: 100,
});
// [{ generated_text: "To become more healthy, you can: 1. Eat a balanced diet with plenty of fruits, vegetables, whole grains, lean proteins, and healthy fats. 2. Stay hydrated by drinking plenty of water. 3. Get enough sleep and manage stress levels. 4. Avoid smoking and excessive alcohol consumption. 5. Regularly exercise and maintain a healthy weight. 6. Practice good hygiene and sanitation. 7. Seek medical attention if you experience any health issues." }]

Kind: pipelines 的静态类


new Text2TextGenerationPipeline(options)

创建一个新的 Text2TextGenerationPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


text2TextGenerationPipeline._key : <code> ’ generated_text ’ </code>

类型Text2TextGenerationPipeline 的实例属性


text2TextGenerationPipeline._call() : <code> Text2TextGenerationPipelineCallback </code>

类型Text2TextGenerationPipeline 的实例方法


pipelines.SummarizationPipeline

一个用于摘要任务的 pipeline,继承自 Text2TextGenerationPipeline。

示例: 摘要,使用 Xenova/distilbart-cnn-6-6

const generator = await pipeline('summarization', 'Xenova/distilbart-cnn-6-6');
const text = 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, ' +
  'and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. ' +
  'During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest ' +
  'man-made structure in the world, a title it held for 41 years until the Chrysler Building in New ' +
  'York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to ' +
  'the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the ' +
  'Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second ' +
  'tallest free-standing structure in France after the Millau Viaduct.';
const output = await generator(text, {
  max_new_tokens: 100,
});
// [{ summary_text: ' The Eiffel Tower is about the same height as an 81-storey building and the tallest structure in Paris. It is the second tallest free-standing structure in France after the Millau Viaduct.' }]

Kind: pipelines 的静态类


new SummarizationPipeline(options)

创建一个新的 SummarizationPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


summarizationPipeline._key : <code> ’ summary_text ’ </code>

类型SummarizationPipeline 的实例属性


pipelines.TranslationPipeline

将文本从一种语言翻译成另一种语言。

示例: 多语言翻译,使用 Xenova/nllb-200-distilled-600M

有关语言及其对应代码的完整列表,请参阅此处

const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M');
const output = await translator('जीवन एक चॉकलेट बॉक्स की तरह है।', {
  src_lang: 'hin_Deva', // Hindi
  tgt_lang: 'fra_Latn', // French
});
// [{ translation_text: 'La vie est comme une boîte à chocolat.' }]

示例: 多语言翻译,使用 Xenova/m2m100_418M

有关语言及其对应代码的完整列表,请参阅此处

const translator = await pipeline('translation', 'Xenova/m2m100_418M');
const output = await translator('生活就像一盒巧克力。', {
  src_lang: 'zh', // Chinese
  tgt_lang: 'en', // English
});
// [{ translation_text: 'Life is like a box of chocolate.' }]

示例: 多语言翻译,使用 Xenova/mbart-large-50-many-to-many-mmt

有关语言及其对应代码的完整列表,请参阅此处

const translator = await pipeline('translation', 'Xenova/mbart-large-50-many-to-many-mmt');
const output = await translator('संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है', {
  src_lang: 'hi_IN', // Hindi
  tgt_lang: 'fr_XX', // French
});
// [{ translation_text: 'Le chef des Nations affirme qu 'il n 'y a military solution in Syria.' }]

Kind: pipelines 的静态类


new TranslationPipeline(options)

创建一个新的 TranslationPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


translationPipeline._key : <code> ’ translation_text ’ </code>

类型TranslationPipeline 的实例属性


pipelines.TextGenerationPipeline

语言生成 pipeline,使用任何 ModelWithLMHeadModelForCausalLM。此 pipeline 预测指定文本提示后将出现的单词。注意:有关生成参数的完整列表,请参阅GenerationConfig

示例: 文本生成,使用 Xenova/distilgpt2(默认设置)。

const generator = await pipeline('text-generation', 'Xenova/distilgpt2');
const text = 'I enjoy walking with my cute dog,';
const output = await generator(text);
// [{ generated_text: "I enjoy walking with my cute dog, and I love to play with the other dogs." }]

示例: 文本生成,使用 Xenova/distilgpt2(自定义设置)。

const generator = await pipeline('text-generation', 'Xenova/distilgpt2');
const text = 'Once upon a time, there was';
const output = await generator(text, {
  temperature: 2,
  max_new_tokens: 10,
  repetition_penalty: 1.5,
  no_repeat_ngram_size: 2,
  num_beams: 2,
  num_return_sequences: 2,
});
// [{
//   "generated_text": "Once upon a time, there was an abundance of information about the history and activities that"
// }, {
//   "generated_text": "Once upon a time, there was an abundance of information about the most important and influential"
// }]

示例: 运行代码生成,使用 Xenova/codegen-350M-mono

const generator = await pipeline('text-generation', 'Xenova/codegen-350M-mono');
const text = 'def fib(n):';
const output = await generator(text, {
  max_new_tokens: 44,
});
// [{
//   generated_text: 'def fib(n):\n' +
//     '    if n == 0:\n' +
//     '        return 0\n' +
//     '    elif n == 1:\n' +
//     '        return 1\n' +
//     '    else:\n' +
//     '        return fib(n-1) + fib(n-2)\n'
// }]

Kind: pipelines 的静态类


new TextGenerationPipeline(options)

创建一个新的 TextGenerationPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


textGenerationPipeline._call() : <code> TextGenerationPipelineCallback </code>

类型TextGenerationPipeline 的实例方法


pipelines.ZeroShotClassificationPipeline

基于 NLI 的零样本分类 pipeline,使用在 NLI(自然语言推理)任务上训练的 ModelForSequenceClassification。等效于 text-classification pipeline,但这些模型不需要硬编码的潜在类别数量,它们可以在运行时选择。这通常意味着速度较慢,但灵活。

示例: 零样本分类,使用 Xenova/mobilebert-uncased-mnli

const classifier = await pipeline('zero-shot-classification', 'Xenova/mobilebert-uncased-mnli');
const text = 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.';
const labels = [ 'mobile', 'billing', 'website', 'account access' ];
const output = await classifier(text, labels);
// {
//   sequence: 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.',
//   labels: [ 'mobile', 'website', 'billing', 'account access' ],
//   scores: [ 0.5562091040482018, 0.1843621307860853, 0.13942646639336376, 0.12000229877234923 ]
// }

示例: 零样本分类,使用 Xenova/nli-deberta-v3-xsmall(多标签)。

const classifier = await pipeline('zero-shot-classification', 'Xenova/nli-deberta-v3-xsmall');
const text = 'I have a problem with my iphone that needs to be resolved asap!';
const labels = [ 'urgent', 'not urgent', 'phone', 'tablet', 'computer' ];
const output = await classifier(text, labels, { multi_label: true });
// {
//   sequence: 'I have a problem with my iphone that needs to be resolved asap!',
//   labels: [ 'urgent', 'phone', 'computer', 'tablet', 'not urgent' ],
//   scores: [ 0.9958870956360275, 0.9923963400697035, 0.002333537946160235, 0.0015134138567598765, 0.0010699384208377163 ]
// }

Kind: pipelines 的静态类


new ZeroShotClassificationPipeline(options)

创建一个新的 ZeroShotClassificationPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


zeroShotClassificationPipeline.model : <code> any </code>

类型ZeroShotClassificationPipeline 的实例属性


zeroShotClassificationPipeline._call() : <code> ZeroShotClassificationPipelineCallback </code>

类型ZeroShotClassificationPipeline 的实例方法


pipelines.FeatureExtractionPipeline

特征提取 pipeline,不使用模型头部。此 pipeline 从基础 transformer 中提取隐藏状态,可以用作下游任务的特征。

示例: 运行特征提取,使用 bert-base-uncased(不进行池化/归一化)。

const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' });
const output = await extractor('This is a simple test.');
// Tensor {
//   type: 'float32',
//   data: Float32Array [0.05939924716949463, 0.021655935794115067, ...],
//   dims: [1, 8, 768]
// }

示例: 运行特征提取,使用 bert-base-uncased(进行池化/归一化)。

const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' });
const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true });
// Tensor {
//   type: 'float32',
//   data: Float32Array [0.03373778983950615, -0.010106077417731285, ...],
//   dims: [1, 768]
// }

示例: 使用 sentence-transformers 模型计算嵌入。

const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true });
// Tensor {
//   type: 'float32',
//   data: Float32Array [0.09094982594251633, -0.014774246141314507, ...],
//   dims: [1, 384]
// }

示例: 使用 sentence-transformers 模型计算二进制嵌入。

const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
const output = await extractor('This is a simple test.', { pooling: 'mean', quantize: true, precision: 'binary' });
// Tensor {
//   type: 'int8',
//   data: Int8Array [49, 108, 24, ...],
//   dims: [1, 48]
// }

Kind: pipelines 的静态类


new FeatureExtractionPipeline(options)

创建一个新的 FeatureExtractionPipeline。

参数类型描述
optionsTextPipelineConstructorArgs

用于实例化 pipeline 的对象。


featureExtractionPipeline._call() : <code> FeatureExtractionPipelineCallback </code>

类型FeatureExtractionPipeline 的实例方法


pipelines.ImageFeatureExtractionPipeline

图像特征提取 pipeline,不使用模型头部。此 pipeline 从基础 transformer 中提取隐藏状态,可以用作下游任务的特征。

示例: 执行图像特征提取,使用 Xenova/vit-base-patch16-224-in21k

const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/vit-base-patch16-224-in21k');
const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png';
const features = await image_feature_extractor(url);
// Tensor {
//   dims: [ 1, 197, 768 ],
//   type: 'float32',
//   data: Float32Array(151296) [ ... ],
//   size: 151296
// }

示例: 使用 Xenova/clip-vit-base-patch32 计算图像嵌入。

const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/clip-vit-base-patch32');
const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png';
const features = await image_feature_extractor(url);
// Tensor {
//   dims: [ 1, 512 ],
//   type: 'float32',
//   data: Float32Array(512) [ ... ],
//   size: 512
// }

Kind: pipelines 的静态类


new ImageFeatureExtractionPipeline(options)

创建一个新的 ImageFeatureExtractionPipeline。

参数类型描述
optionsImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


imageFeatureExtractionPipeline._call() : <code> ImageFeatureExtractionPipelineCallback </code>

类型ImageFeatureExtractionPipeline 的实例方法


pipelines.AudioClassificationPipeline

音频分类 pipeline,使用任何 AutoModelForAudioClassification。此 pipeline 预测原始波形或音频文件的类别。

示例: 执行音频分类,使用 Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech

const classifier = await pipeline('audio-classification', 'Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
const output = await classifier(url);
// [
//   { label: 'male', score: 0.9981542229652405 },
//   { label: 'female', score: 0.001845747814513743 }
// ]

示例: 执行音频分类,使用 Xenova/ast-finetuned-audioset-10-10-0.4593 并返回前 4 个结果。

const classifier = await pipeline('audio-classification', 'Xenova/ast-finetuned-audioset-10-10-0.4593');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav';
const output = await classifier(url, { top_k: 4 });
// [
//   { label: 'Meow', score: 0.5617874264717102 },
//   { label: 'Cat', score: 0.22365376353263855 },
//   { label: 'Domestic animals, pets', score: 0.1141069084405899 },
//   { label: 'Animal', score: 0.08985692262649536 },
// ]

Kind: pipelines 的静态类


new AudioClassificationPipeline(options)

创建一个新的 AudioClassificationPipeline。

参数类型描述
optionsAudioPipelineConstructorArgs

用于实例化 pipeline 的对象。


audioClassificationPipeline._call() : <code> AudioClassificationPipelineCallback </code>

类型AudioClassificationPipeline 的实例方法


pipelines.ZeroShotAudioClassificationPipeline

零样本音频分类 pipeline,使用 ClapModel。当您提供音频和一组 candidate_labels 时,此 pipeline 预测音频的类别。

示例:执行零样本音频分类,使用 Xenova/clap-htsat-unfused

const classifier = await pipeline('zero-shot-audio-classification', 'Xenova/clap-htsat-unfused');
const audio = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/dog_barking.wav';
const candidate_labels = ['dog', 'vaccum cleaner'];
const scores = await classifier(audio, candidate_labels);
// [
//   { score: 0.9993992447853088, label: 'dog' },
//   { score: 0.0006007603369653225, label: 'vaccum cleaner' }
// ]

Kind: pipelines 的静态类


new ZeroShotAudioClassificationPipeline(options)

创建一个新的 ZeroShotAudioClassificationPipeline。

参数类型描述
optionsTextAudioPipelineConstructorArgs

用于实例化 pipeline 的对象。


zeroShotAudioClassificationPipeline._call() : <code> ZeroShotAudioClassificationPipelineCallback </code>

类型ZeroShotAudioClassificationPipeline 的实例方法


pipelines.AutomaticSpeechRecognitionPipeline

旨在提取音频中包含的口语文本的 Pipeline。

示例: 转录英语。

const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
const output = await transcriber(url);
// { text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." }

示例: 转录英语,带时间戳。

const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
const output = await transcriber(url, { return_timestamps: true });
// {
//   text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country."
//   chunks: [
//     { timestamp: [0, 8],  text: " And so my fellow Americans ask not what your country can do for you" }
//     { timestamp: [8, 11], text: " ask what you can do for your country." }
//   ]
// }

示例: 转录英语,带词级时间戳。

const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
const output = await transcriber(url, { return_timestamps: 'word' });
// {
//   "text": " And so my fellow Americans ask not what your country can do for you ask what you can do for your country.",
//   "chunks": [
//     { "text": " And", "timestamp": [0, 0.78] },
//     { "text": " so", "timestamp": [0.78, 1.06] },
//     { "text": " my", "timestamp": [1.06, 1.46] },
//     ...
//     { "text": " for", "timestamp": [9.72, 9.92] },
//     { "text": " your", "timestamp": [9.92, 10.22] },
//     { "text": " country.", "timestamp": [10.22, 13.5] }
//   ]
// }

示例: 转录法语。

const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3';
const output = await transcriber(url, { language: 'french', task: 'transcribe' });
// { text: " J'adore, j'aime, je n'aime pas, je déteste." }

示例: 将法语翻译成英语。

const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3';
const output = await transcriber(url, { language: 'french', task: 'translate' });
// { text: " I love, I like, I don't like, I hate." }

示例: 转录/翻译超过 30 秒的音频。

const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ted_60.wav';
const output = await transcriber(url, { chunk_length_s: 30, stride_length_s: 5 });
// { text: " So in college, I was a government major, which means [...] So I'd start off light and I'd bump it up" }

Kind: pipelines 的静态类


new AutomaticSpeechRecognitionPipeline(options)

创建一个新的 AutomaticSpeechRecognitionPipeline。

参数类型描述
optionsTextAudioPipelineConstructorArgs

用于实例化 pipeline 的对象。


automaticSpeechRecognitionPipeline._call() : <code> AutomaticSpeechRecognitionPipelineCallback </code>

类型AutomaticSpeechRecognitionPipeline 的实例方法


pipelines.ImageToTextPipeline

使用 AutoModelForVision2Seq 的图像到文本 (Image To Text) 管道。此管道预测给定图像的标题。

示例: 使用 Xenova/vit-gpt2-image-captioning 为图像生成标题。

const captioner = await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const output = await captioner(url);
// [{ generated_text: 'a cat laying on a couch with another cat' }]

示例: 使用 Xenova/trocr-small-handwritten 进行光学字符识别 (OCR)。

const captioner = await pipeline('image-to-text', 'Xenova/trocr-small-handwritten');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/handwriting.jpg';
const output = await captioner(url);
// [{ generated_text: 'Mr. Brown commented icily.' }]

Kind: pipelines 的静态类


new ImageToTextPipeline(options)

创建一个新的 ImageToTextPipeline。

参数类型描述
optionsTextImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


imageToTextPipeline._call() : <code> ImageToTextPipelineCallback </code>

类型: ImageToTextPipeline 的实例方法


pipelines.ImageClassificationPipeline

图像分类管道,使用任何 AutoModelForImageClassification。此管道预测图像的类别。

示例: 分类图像。

const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await classifier(url);
// [
//   { label: 'tiger, Panthera tigris', score: 0.632695734500885 },
// ]

示例: 分类图像并返回前 n 个类别。

const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await classifier(url, { top_k: 3 });
// [
//   { label: 'tiger, Panthera tigris', score: 0.632695734500885 },
//   { label: 'tiger cat', score: 0.3634825646877289 },
//   { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 },
// ]

示例: 分类图像并返回所有类别。

const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await classifier(url, { top_k: 0 });
// [
//   { label: 'tiger, Panthera tigris', score: 0.632695734500885 },
//   { label: 'tiger cat', score: 0.3634825646877289 },
//   { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 },
//   { label: 'jaguar, panther, Panthera onca, Felis onca', score: 0.00035465499968267977 },
//   ...
// ]

Kind: pipelines 的静态类


new ImageClassificationPipeline(options)

创建一个新的 ImageClassificationPipeline。

参数类型描述
optionsImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


imageClassificationPipeline._call() : <code> ImageClassificationPipelineCallback </code>

类型: ImageClassificationPipeline 的实例方法


pipelines.ImageSegmentationPipeline

图像分割管道,使用任何 AutoModelForXXXSegmentation。此管道预测物体的掩码及其类别。

示例: 使用 Xenova/detr-resnet-50-panoptic 执行图像分割。

const segmenter = await pipeline('image-segmentation', 'Xenova/detr-resnet-50-panoptic');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const output = await segmenter(url);
// [
//   { label: 'remote', score: 0.9984649419784546, mask: RawImage { ... } },
//   { label: 'cat', score: 0.9994316101074219, mask: RawImage { ... } }
// ]

Kind: pipelines 的静态类


new ImageSegmentationPipeline(options)

创建一个新的 ImageSegmentationPipeline。

参数类型描述
optionsImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


imageSegmentationPipeline._call() : <code> ImageSegmentationPipelineCallback </code>

类型: ImageSegmentationPipeline 的实例方法


pipelines.BackgroundRemovalPipeline

背景移除管道,使用特定的 AutoModelForXXXSegmentation。此管道移除图像的背景。

示例: 使用 Xenova/modnet 执行背景移除。

const segmenter = await pipeline('background-removal', 'Xenova/modnet');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/portrait-of-woman_small.jpg';
const output = await segmenter(url);
// [
//   RawImage { data: Uint8ClampedArray(648000) [ ... ], width: 360, height: 450, channels: 4 }
// ]

Kind: pipelines 的静态类


new BackgroundRemovalPipeline(options)

创建一个新的 BackgroundRemovalPipeline。

参数类型描述
optionsImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


backgroundRemovalPipeline._call() : <code> BackgroundRemovalPipelineCallback </code>

类型: BackgroundRemovalPipeline 的实例方法


pipelines.ZeroShotImageClassificationPipeline

零样本图像分类管道。当您提供图像和一组 candidate_labels 时,此管道预测图像的类别。

示例: 使用 Xenova/clip-vit-base-patch32 进行零样本图像分类。

const classifier = await pipeline('zero-shot-image-classification', 'Xenova/clip-vit-base-patch32');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await classifier(url, ['tiger', 'horse', 'dog']);
// [
//   { score: 0.9993917942047119, label: 'tiger' },
//   { score: 0.0003519294841680676, label: 'horse' },
//   { score: 0.0002562698791734874, label: 'dog' }
// ]

Kind: pipelines 的静态类


new ZeroShotImageClassificationPipeline(options)

创建一个新的 ZeroShotImageClassificationPipeline。

参数类型描述
optionsTextImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


zeroShotImageClassificationPipeline._call() : <code> ZeroShotImageClassificationPipelineCallback </code>

类型: ZeroShotImageClassificationPipeline 的实例方法


pipelines.ObjectDetectionPipeline

对象检测管道,使用任何 AutoModelForObjectDetection。此管道预测物体的边界框及其类别。

示例: 使用 Xenova/detr-resnet-50 运行对象检测。

const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
const img = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const output = await detector(img, { threshold: 0.9 });
// [{
//   score: 0.9976370930671692,
//   label: "remote",
//   box: { xmin: 31, ymin: 68, xmax: 190, ymax: 118 }
// },
// ...
// {
//   score: 0.9984092116355896,
//   label: "cat",
//   box: { xmin: 331, ymin: 19, xmax: 649, ymax: 371 }
// }]

Kind: pipelines 的静态类


new ObjectDetectionPipeline(options)

创建一个新的 ObjectDetectionPipeline。

参数类型描述
optionsImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


objectDetectionPipeline._call() : <code> ObjectDetectionPipelineCallback </code>

类型: ObjectDetectionPipeline 的实例方法


pipelines.ZeroShotObjectDetectionPipeline

零样本对象检测管道。当您提供图像和一组 candidate_labels 时,此管道预测物体的边界框。

示例: 使用 Xenova/owlvit-base-patch32 进行零样本对象检测。

const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png';
const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag'];
const output = await detector(url, candidate_labels);
// [
//   {
//     score: 0.24392342567443848,
//     label: 'human face',
//     box: { xmin: 180, ymin: 67, xmax: 274, ymax: 175 }
//   },
//   {
//     score: 0.15129457414150238,
//     label: 'american flag',
//     box: { xmin: 0, ymin: 4, xmax: 106, ymax: 513 }
//   },
//   {
//     score: 0.13649864494800568,
//     label: 'helmet',
//     box: { xmin: 277, ymin: 337, xmax: 511, ymax: 511 }
//   },
//   {
//     score: 0.10262022167444229,
//     label: 'rocket',
//     box: { xmin: 352, ymin: -1, xmax: 463, ymax: 287 }
//   }
// ]

示例: 使用 Xenova/owlvit-base-patch32 进行零样本对象检测(返回前 4 个匹配项并设置阈值)。

const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/beach.png';
const candidate_labels = ['hat', 'book', 'sunglasses', 'camera'];
const output = await detector(url, candidate_labels, { top_k: 4, threshold: 0.05 });
// [
//   {
//     score: 0.1606510728597641,
//     label: 'sunglasses',
//     box: { xmin: 347, ymin: 229, xmax: 429, ymax: 264 }
//   },
//   {
//     score: 0.08935828506946564,
//     label: 'hat',
//     box: { xmin: 38, ymin: 174, xmax: 258, ymax: 364 }
//   },
//   {
//     score: 0.08530698716640472,
//     label: 'camera',
//     box: { xmin: 187, ymin: 350, xmax: 260, ymax: 411 }
//   },
//   {
//     score: 0.08349756896495819,
//     label: 'book',
//     box: { xmin: 261, ymin: 280, xmax: 494, ymax: 425 }
//   }
// ]

Kind: pipelines 的静态类


new ZeroShotObjectDetectionPipeline(options)

创建一个新的 ZeroShotObjectDetectionPipeline。

参数类型描述
optionsTextImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


zeroShotObjectDetectionPipeline._call() : <code> ZeroShotObjectDetectionPipelineCallback </code>

类型: ZeroShotObjectDetectionPipeline 的实例方法


pipelines.DocumentQuestionAnsweringPipeline

文档问答管道,使用任何 AutoModelForDocumentQuestionAnswering。输入/输出类似于(抽取式)问答管道;但是,该管道以图像(和可选的 OCR 识别的单词/框)作为输入,而不是文本上下文。

示例: 使用 Xenova/donut-base-finetuned-docvqa 回答有关文档的问题。

const qa_pipeline = await pipeline('document-question-answering', 'Xenova/donut-base-finetuned-docvqa');
const image = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png';
const question = 'What is the invoice number?';
const output = await qa_pipeline(image, question);
// [{ answer: 'us-001' }]

Kind: pipelines 的静态类


new DocumentQuestionAnsweringPipeline(options)

创建一个新的 DocumentQuestionAnsweringPipeline。

参数类型描述
optionsTextImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


documentQuestionAnsweringPipeline._call() : <code> DocumentQuestionAnsweringPipelineCallback </code>

类型: DocumentQuestionAnsweringPipeline 的实例方法


pipelines.TextToAudioPipeline

文本到音频生成管道,使用任何 AutoModelForTextToWaveformAutoModelForTextToSpectrogram。此管道从输入文本和可选的其他条件输入生成音频文件。

示例: 使用 Xenova/speecht5_tts 从文本生成音频。

const synthesizer = await pipeline('text-to-speech', 'Xenova/speecht5_tts', { quantized: false });
const speaker_embeddings = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin';
const out = await synthesizer('Hello, my dog is cute', { speaker_embeddings });
// RawAudio {
//   audio: Float32Array(26112) [-0.00005657337896991521, 0.00020583874720614403, ...],
//   sampling_rate: 16000
// }

然后,您可以使用 wavefile 包将音频保存到 .wav 文件中

import wavefile from 'wavefile';
import fs from 'fs';

const wav = new wavefile.WaveFile();
wav.fromScratch(1, out.sampling_rate, '32f', out.audio);
fs.writeFileSync('out.wav', wav.toBuffer());

示例: 使用 Xenova/mms-tts-fra 进行多语言语音生成。有关可用语言的完整列表(1107 种),请参阅此处

const synthesizer = await pipeline('text-to-speech', 'Xenova/mms-tts-fra');
const out = await synthesizer('Bonjour');
// RawAudio {
//   audio: Float32Array(23808) [-0.00037693005288019776, 0.0003325853613205254, ...],
//   sampling_rate: 16000
// }

Kind: pipelines 的静态类


new TextToAudioPipeline(options)

创建一个新的 TextToAudioPipeline。

参数类型描述
optionsTextToAudioPipelineConstructorArgs

用于实例化 pipeline 的对象。


textToAudioPipeline._call() : <code> TextToAudioPipelineCallback </code>

类型: TextToAudioPipeline 的实例方法


pipelines.ImageToImagePipeline

图像到图像管道,使用任何 AutoModelForImageToImage。此管道基于先前的图像输入生成图像。

示例: 超分辨率,使用 Xenova/swin2SR-classical-sr-x2-64

const upscaler = await pipeline('image-to-image', 'Xenova/swin2SR-classical-sr-x2-64');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg';
const output = await upscaler(url);
// RawImage {
//   data: Uint8Array(786432) [ 41, 31, 24,  43, ... ],
//   width: 512,
//   height: 512,
//   channels: 3
// }

Kind: pipelines 的静态类


new ImageToImagePipeline(options)

创建一个新的 ImageToImagePipeline。

参数类型描述
optionsImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


imageToImagePipeline._call() : <code> ImageToImagePipelineCallback </code>

类型: ImageToImagePipeline 的实例方法


pipelines.DepthEstimationPipeline

深度估计管道,使用任何 AutoModelForDepthEstimation。此管道预测图像的深度。

示例: 深度估计,使用 Xenova/dpt-hybrid-midas

const depth_estimator = await pipeline('depth-estimation', 'Xenova/dpt-hybrid-midas');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const out = await depth_estimator(url);
// {
//   predicted_depth: Tensor {
//     dims: [ 384, 384 ],
//     type: 'float32',
//     data: Float32Array(147456) [ 542.859130859375, 545.2833862304688, 546.1649169921875, ... ],
//     size: 147456
//   },
//   depth: RawImage {
//     data: Uint8Array(307200) [ 86, 86, 86, ... ],
//     width: 640,
//     height: 480,
//     channels: 1
//   }
// }

Kind: pipelines 的静态类


new DepthEstimationPipeline(options)

创建一个新的 DepthEstimationPipeline。

参数类型描述
optionsImagePipelineConstructorArgs

用于实例化 pipeline 的对象。


depthEstimationPipeline._call() : <code> DepthEstimationPipelineCallback </code>

Kind: DepthEstimationPipeline 的实例方法


pipelines.pipeline(task, [model], [options]) ⇒ <code> * </code>

用于构建 Pipeline 对象的实用工厂方法。

Kind: pipelines 的静态方法
Returns: * - 指定任务的 Pipeline 对象。
Throws:

  • Error 如果请求了不支持的管线。
参数类型默认值描述
taskT

定义将返回哪个管线的任务。目前接受的任务有

  • "audio-classification": 将返回 AudioClassificationPipeline
  • "automatic-speech-recognition": 将返回 AutomaticSpeechRecognitionPipeline
  • "depth-estimation": 将返回 DepthEstimationPipeline
  • "document-question-answering": 将返回 DocumentQuestionAnsweringPipeline
  • "feature-extraction": 将返回 FeatureExtractionPipeline
  • "fill-mask": 将返回 FillMaskPipeline
  • "image-classification": 将返回 ImageClassificationPipeline
  • "image-segmentation": 将返回 ImageSegmentationPipeline
  • "image-to-text": 将返回 ImageToTextPipeline
  • "object-detection": 将返回 ObjectDetectionPipeline
  • "question-answering": 将返回 QuestionAnsweringPipeline
  • "summarization": 将返回 SummarizationPipeline
  • "text2text-generation": 将返回 Text2TextGenerationPipeline
  • "text-classification" (别名 "sentiment-analysis" 可用): 将返回 TextClassificationPipeline
  • "text-generation": 将返回 TextGenerationPipeline
  • "token-classification" (别名 "ner" 可用): 将返回 TokenClassificationPipeline
  • "translation": 将返回 TranslationPipeline
  • "translation_xx_to_yy": 将返回 TranslationPipeline
  • "zero-shot-classification": 将返回 ZeroShotClassificationPipeline
  • "zero-shot-audio-classification": 将返回 ZeroShotAudioClassificationPipeline
  • "zero-shot-image-classification": 将返回 ZeroShotImageClassificationPipeline
  • "zero-shot-object-detection": 将返回 ZeroShotObjectDetectionPipeline
[model]stringnull

要使用的预训练模型的名称。如果未指定,将使用任务的默认模型。

[options]*

管线的可选参数。


pipelines~ImagePipelineInputs : <code> string </code> | <code> RawImage </code> | <code> URL </code> | <code> Blob </code> | <code> HTMLCanvasElement </code> | <code> OffscreenCanvas </code>

Kind: pipelines 的内部类型定义


pipelines~AudioPipelineInputs : <code> string </code> | <code> URL </code> | <code> Float32Array </code> | <code> Float64Array </code>

Kind: pipelines 的内部类型定义


pipelines~BoundingBox : <code> Object </code>

Kind: pipelines 的内部类型定义
属性

名称类型描述
xminnumber

边界框的最小 x 坐标。

yminnumber

边界框的最小 y 坐标。

xmaxnumber

边界框的最大 x 坐标。

ymaxnumber

边界框的最大 y 坐标。


pipelines~Disposable ⇒ <code> Promise. < void > </code>

Kind: pipelines 的内部类型定义
Returns: Promise.<void> - 当项目被释放时解析的 Promise。
属性

名称类型描述
disposeDisposeType

当管线被释放时解析的 Promise。


pipelines~TextPipelineConstructorArgs : <code> Object </code>

用于实例化基于文本的管线的对象。

Kind: pipelines 的内部类型定义
属性

名称类型描述
taskstring

pipeline 的任务。用于指定子任务。

modelPreTrainedModel

pipeline 使用的模型。

tokenizerPreTrainedTokenizer

管线使用的分词器。


pipelines~ImagePipelineConstructorArgs : <code> Object </code>

用于实例化基于音频的管线的对象。

Kind: pipelines 的内部类型定义
属性

名称类型描述
taskstring

pipeline 的任务。用于指定子任务。

modelPreTrainedModel

pipeline 使用的模型。

processorProcessor

管线使用的处理器。


pipelines~TextImagePipelineConstructorArgs : <code> Object </code>

用于实例化基于文本和音频的管线的对象。

Kind: pipelines 的内部类型定义
属性

名称类型描述
taskstring

pipeline 的任务。用于指定子任务。

modelPreTrainedModel

pipeline 使用的模型。

tokenizerPreTrainedTokenizer

管线使用的分词器。

processorProcessor

管线使用的处理器。


pipelines~TextClassificationPipelineType ⇒ <code> Promise. < (TextClassificationOutput|Array < TextClassificationOutput > ) > </code>

文本分类管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<(TextClassificationOutput|Array<TextClassificationOutput>)> - 包含预测标签和分数的数组或对象。

参数类型描述
textsstring | Array<string>

要分类的输入文本。

[options]TextClassificationPipelineOptions

用于文本分类的选项。

属性

名称类型默认值描述
labelstring

预测的标签。

scorenumber

相应的概率。

[top_k]number1

要返回的顶部预测的数量。


pipelines~TokenClassificationPipelineType ⇒ <code> Promise. < (TokenClassificationOutput|Array < TokenClassificationOutput > ) > </code>

令牌分类管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<(TokenClassificationOutput|Array<TokenClassificationOutput>)> - 结果。

参数类型描述
textsstring | Array<string>

一个或多个文本(或文本列表)用于令牌分类。

[options]TokenClassificationPipelineOptions

用于令牌分类的选项。

属性

名称类型描述
wordstring

分类的令牌/词。这是通过解码选定的令牌获得的。

scorenumber

entity 的相应概率。

entitystring

为该令牌/词预测的实体。

indexnumber

句子中相应令牌的索引。

[start]number

句子中相应实体的起始索引。

[end]number

句子中相应实体的结束索引。

[ignore_labels]Array.<string>

要忽略的标签列表。


pipelines~QuestionAnsweringPipelineType ⇒ <code> Promise. < (QuestionAnsweringOutput|Array < QuestionAnsweringOutput > ) > </code>

问题回答管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<(QuestionAnsweringOutput|Array<QuestionAnsweringOutput>)> - 包含预测答案和分数的数组或对象。

参数类型描述
questionstring | Array<string>

一个或多个问题(必须与 context 参数结合使用)。

contextstring | Array<string>

与问题关联的一个或多个上下文(必须与 question 参数结合使用)。

[options]QuestionAnsweringPipelineOptions

用于问题回答的选项。

属性

名称类型默认值描述
scorenumber

与答案关联的概率。

[start]number

答案的字符起始索引(在输入的令牌化版本中)。

[end]number

答案的字符结束索引(在输入的令牌化版本中)。

answerstring

问题的答案。

[top_k]number1

要返回的顶部答案预测的数量。


pipelines~FillMaskPipelineType ⇒ <code> Promise. < (FillMaskOutput|Array < FillMaskOutput > ) > </code>

填充掩码管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<(FillMaskOutput|Array<FillMaskOutput>)> - 包含分数、预测令牌、预测令牌字符串以及填充了预测令牌的序列的对象数组,或此类数组的数组(每个输入文本一个)。如果仅给出一个输入文本,则输出将是对象数组。
Throws:

  • Error 当在输入文本中找不到掩码令牌时。
参数类型描述
textsstring | Array<string>

一个或多个带有掩码令牌的文本(或提示列表)。

[options]FillMaskPipelineOptions

用于掩码语言建模的选项。

属性

名称类型默认值描述
sequencestring

带有掩码令牌预测的相应输入。

scorenumber

相应的概率。

tokennumber

预测的令牌 ID(用于替换掩码令牌)。

token_strstring

预测的令牌(用于替换掩码令牌)。

[top_k]number5

当传递时,覆盖要返回的预测数量。


pipelines~Text2TextGenerationPipelineType ⇒ <code> Promise. < (Text2TextGenerationOutput|Array < Text2TextGenerationOutput > ) > </code>

Kind: pipelines 的内部类型定义

参数类型描述
textsstring | Array<string>

编码器的输入文本。

[options]*

要传递给模型的 generate 方法的其他关键字参数。

属性

名称类型描述
generated_textstring

生成的文本。


pipelines~SummarizationPipelineType ⇒ <code> Promise. < (SummarizationOutput|Array < SummarizationOutput > ) > </code>

Kind: pipelines 的内部类型定义

参数类型描述
textsstring | Array<string>

一篇或多篇文章(或文章列表)进行摘要。

[options]*

要传递给模型的 generate 方法的其他关键字参数。

属性

名称类型描述
summary_textstring

摘要文本。


pipelines~TranslationPipelineType ⇒ <code> Promise. < (TranslationOutput|Array < TranslationOutput > ) > </code>

Kind: pipelines 的内部类型定义

参数类型描述
textsstring | Array<string>

要翻译的文本。

[options]*

要传递给模型的 generate 方法的其他关键字参数。

属性

名称类型描述
translation_textstring

翻译后的文本。


pipelines~TextGenerationPipelineType ⇒ <code> Promise. < (TextGenerationOutput|Array < TextGenerationOutput > ) > </code>

文本生成管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<(TextGenerationOutput|Array<TextGenerationOutput>)> - 包含生成文本的数组或对象。

参数类型描述
textsstring | Array<string> | Chat | Array<Chat>

一个或多个提示(或提示列表)以完成。

[options]Partial.<TextGenerationConfig>

要传递给模型的 generate 方法的其他关键字参数。

属性

名称类型默认值描述
generated_textstring | Chat

生成的文本。

[add_special_tokens]boolean

在对序列进行令牌化时是否添加特殊令牌。

[return_full_text]booleantrue

如果设置为 false,则仅返回添加的文本,否则返回完整文本。


pipelines~ZeroShotClassificationPipelineType ⇒ <code> Promise. < (ZeroShotClassificationOutput|Array < ZeroShotClassificationOutput > ) > </code>

零样本分类管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<(ZeroShotClassificationOutput|Array<ZeroShotClassificationOutput>)> - 包含预测标签和分数的数组或对象。

参数类型描述
textsstring | Array<string>

要分类的序列,如果模型输入过大,将被截断。

candidate_labelsstring | Array<string>

要将每个序列分类到的可能类别标签集。可以是单个标签、逗号分隔的标签字符串或标签列表。

[options]ZeroShotClassificationPipelineOptions

用于零样本分类的选项。

属性

名称类型默认值描述
sequencestring

这是其输出的序列。

labelsArray.<string>

按可能性顺序排序的标签。

scoresArray.<number>

每个标签的概率。

[hypothesis_template]string""This example is {}.""

用于将每个候选标签转换为 NLI 风格假设的模板。候选标签将替换 &#123;} 占位符。

[multi_label]booleanfalse

是否可以有多个候选标签为真。如果为 false,则对分数进行归一化,使得每个序列的标签可能性之和为 1。如果为 true,则标签被认为是独立的,并且通过对蕴含分数与矛盾分数进行 softmax 归一化每个候选的概率。


pipelines~FeatureExtractionPipelineType ⇒ <code> Promise. < Tensor > </code>

特征提取管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<Tensor> - 模型计算的特征。

参数类型描述
textsstring | Array<string>

一个或多个文本(或文本列表)以获取其特征。

[options]FeatureExtractionPipelineOptions

用于特征提取的选项。

属性

名称类型默认值描述
[pooling]'none' | 'mean' | 'cls'"none"

要使用的池化方法。

[normalize]booleanfalse

是否在最后一个维度中归一化嵌入。

[quantize]booleanfalse

是否量化嵌入。

[precision]'binary' | 'ubinary''binary'

用于量化的精度。


pipelines~ImageFeatureExtractionPipelineType ⇒ <code> Promise. < Tensor > </code>

图像特征提取管线特有的参数。

Kind: pipelines 的内部类型定义
Returns: Promise.<Tensor> - 模型计算的图像特征。

参数类型描述
imagesImagePipelineInputs

用于获取特征的一张或多张图像(或图像列表)。

[options]ImageFeatureExtractionPipelineOptions

用于图像特征提取的选项。

属性

名称类型默认值描述
[pool]boolean

是否返回池化输出。如果设置为 false,模型将返回原始隐藏状态。


pipelines~AudioClassificationPipelineType ⇒ <code> Promise. < (AudioClassificationOutput|Array < AudioClassificationOutput > ) > </code>

音频分类管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<(AudioClassificationOutput|Array<AudioClassificationOutput>)> - 包含预测标签和分数的数组或对象。

参数类型描述
audioAudioPipelineInputs

要分类的输入音频文件。输入可以是:

  • stringURL,它是音频文件的文件名/URL。该文件将以处理器的采样率读取,以使用 AudioContext API 获取波形。如果 AudioContext 不可用,您应该将原始波形作为形状为 (n, ) 的 Float32Array 传入。
  • 形状为 (n, )Float32ArrayFloat64Array,表示正确采样率的原始音频(不会进行进一步检查)。
[options]AudioClassificationPipelineOptions

用于音频分类的选项。

属性

名称类型默认值描述
labelstring

预测的标签。

scorenumber

相应的概率。

[top_k]number5

管道将返回的顶部标签的数量。如果提供的数字为 null 或高于模型配置中可用的标签数量,则将默认为标签数量。


pipelines~ZeroShotAudioClassificationPipelineType ⇒ <code> Promise. < (Array < ZeroShotAudioClassificationOutput > |Array < Array < ZeroShotAudioClassificationOutput > > ) > </code>

零样本音频分类管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<(Array<ZeroShotAudioClassificationOutput>|Array<Array<ZeroShotAudioClassificationOutput>>)> - 包含预测标签和分数的对象数组。

参数类型描述
audioAudioPipelineInputs

要分类的输入音频文件。输入可以是:

  • stringURL,它是音频文件的文件名/URL。该文件将以处理器的采样率读取,以使用 AudioContext API 获取波形。如果 AudioContext 不可用,您应该将原始波形作为形状为 (n, ) 的 Float32Array 传入。
  • 形状为 (n, )Float32ArrayFloat64Array,表示正确采样率的原始音频(不会进行进一步检查)。
candidate_labelsArray.<string>

此音频的候选标签。

[options]ZeroShotAudioClassificationPipelineOptions

用于零样本音频分类的选项。

属性

名称类型默认值描述
labelstring

模型识别的标签。它是建议的 candidate_label 之一。

scorenumber

模型为此标签赋予的分数(介于 0 和 1 之间)。

[hypothesis_template]string""这是 {} 的声音。""

candidate_labels 结合使用的句子,通过将占位符替换为 candidate_labels 来尝试音频分类。然后使用 logits_per_audio 估计可能性。


pipelines~Chunk : <code> Object </code>

Kind: pipelines 的内部类型定义
属性

名称类型描述
timestamp*

块的起始和结束时间戳,以秒为单位。

textstring

识别的文本。


pipelines~AutomaticSpeechRecognitionPipelineType ⇒ <code> Promise. < (AutomaticSpeechRecognitionOutput|Array < AutomaticSpeechRecognitionOutput > ) > </code>

自动语音识别管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<(AutomaticSpeechRecognitionOutput|Array<AutomaticSpeechRecognitionOutput>)> - 包含转录文本的对象,如果 return_timestampstrue,则可选地包含时间戳。

参数类型描述
audioAudioPipelineInputs

要转录的输入音频文件。输入可以是:

  • stringURL,它是音频文件的文件名/URL。该文件将以处理器的采样率读取,以使用 AudioContext API 获取波形。如果 AudioContext 不可用,您应该将原始波形作为形状为 (n, ) 的 Float32Array 传入。
  • 形状为 (n, )Float32ArrayFloat64Array,表示正确采样率的原始音频(不会进行进一步检查)。
[options]Partial.<AutomaticSpeechRecognitionConfig>

要传递给模型的 generate 方法的其他关键字参数。

属性

名称类型描述
textstring

识别的文本。

[chunks]Array.<Chunk>

当使用 return_timestamps 时,chunks 将成为一个列表,其中包含模型识别的各种文本块。

[return_timestamps]boolean | 'word'

是否返回时间戳。默认为 false

[chunk_length_s]number

要处理的音频块的长度,以秒为单位。默认为 0(不分块)。

[stride_length_s]number

连续音频块之间重叠的长度,以秒为单位。如果未提供,则默认为 chunk_length_s / 6

[force_full_sequences]boolean

是否强制输出完整序列。默认为 false

[language]string

源语言。默认为 null,表示应自动检测。如果已知源语言,请使用此选项以可能提高性能。

[task]string

要执行的任务。默认为 null,表示应自动检测。

[num_frames]number

输入音频中的帧数。


pipelines~ImageToTextPipelineType ⇒ <code> Promise. < (ImageToTextOutput|Array < ImageToTextOutput > ) > </code>

Kind: pipelines 的内部类型定义
返回: Promise.<(ImageToTextOutput|Array<ImageToTextOutput>)> - 包含生成的文本的对象(或对象数组)。

参数类型描述
textsImagePipelineInputs

要添加标题的图像。

[options]*

要传递给模型的 generate 方法的其他关键字参数。

属性

名称类型描述
generated_textstring

生成的文本。


pipelines~ImageClassificationPipelineType ⇒ <code> Promise. < (ImageClassificationOutput|Array < ImageClassificationOutput > ) > </code>

图像分类管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<(ImageClassificationOutput|Array<ImageClassificationOutput>)> - 包含预测标签和分数的数组或对象。

参数类型描述
imagesImagePipelineInputs

要分类的输入图像。

[options]ImageClassificationPipelineOptions

用于图像分类的选项。

属性

名称类型默认值描述
labelstring

模型识别的标签。

scorenumber

模型为此标签赋予的分数。

[top_k]number1

管道将返回的顶部标签的数量。


pipelines~ImageSegmentationPipelineType ⇒ <code> Promise. < Array < ImageSegmentationPipelineOutput > > </code>

图像分割管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<Array<ImageSegmentationPipelineOutput>> - 注释的分割。

参数类型描述
imagesImagePipelineInputs

输入图像。

[options]ImageSegmentationPipelineOptions

用于图像分割的选项。

属性

名称类型默认值描述
labelstring | null

分割的标签。

scorenumber | null

分割的分数。

maskRawImage

分割的掩码。

[threshold]number0.5

用于滤除预测掩码的概率阈值。

[mask_threshold]number0.5

将预测掩码转换为二值时使用的阈值。

[overlap_mask_area_threshold]number0.8

掩码重叠阈值,用于消除小的、不连接的分割。

[subtask]null | string

要执行的分割任务。可以是 [panoptic, instance, 和 semantic] 之一,具体取决于模型能力。如果未设置,管道将尝试按顺序解析(按此顺序)。

[label_ids_to_fuse]Array.<number>

要融合的标签 ID 列表。如果未设置,则不融合任何标签。

[target_sizes]Array.<Array<number>>

输入图像的目标尺寸列表。如果未设置,则使用原始图像尺寸。


pipelines~BackgroundRemovalPipelineType ⇒ <code> Promise. < Array < RawImage > > </code>

图像分割管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<Array<RawImage>> - 背景已移除的图像。

参数类型描述
imagesImagePipelineInputs

输入图像。

[options]BackgroundRemovalPipelineOptions

用于图像分割的选项。


pipelines~ZeroShotImageClassificationPipelineType ⇒ <code> Promise. < (Array < ZeroShotImageClassificationOutput > |Array < Array < ZeroShotImageClassificationOutput > > ) > </code>

零样本图像分类管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<(Array<ZeroShotImageClassificationOutput>|Array<Array<ZeroShotImageClassificationOutput>>)> - 包含预测标签和分数的对象数组。

参数类型描述
imagesImagePipelineInputs

输入图像。

candidate_labelsArray.<string>

此图像的候选标签。

[options]ZeroShotImageClassificationPipelineOptions

用于零样本图像分类的选项。

属性

名称类型默认值描述
labelstring

模型识别的标签。它是建议的 candidate_label 之一。

scorenumber

模型为此标签赋予的分数(介于 0 和 1 之间)。

[hypothesis_template]string""这是一张 {} 的照片""

candidate_labels 结合使用的句子,通过将占位符替换为 candidate_labels 来尝试图像分类。然后使用 logits_per_image 估计可能性。


pipelines~ObjectDetectionPipelineType ⇒ <code> Promise. < (ObjectDetectionPipelineOutput|Array < ObjectDetectionPipelineOutput > ) > </code>

对象检测管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<(ObjectDetectionPipelineOutput|Array<ObjectDetectionPipelineOutput>)> - 对象列表或对象列表的列表。

参数类型描述
imagesImagePipelineInputs

输入图像。

[options]ObjectDetectionPipelineOptions

用于对象检测的选项。

属性

名称类型默认值描述
labelstring

模型识别的类别标签。

scorenumber

模型为此标签赋予的分数。

boxBoundingBox

图像原始尺寸中检测到的对象的边界框,或者如果 percentage 设置为 true,则为百分比形式。

[threshold]number0.9

用于按分数过滤框的阈值。

[percentage]booleanfalse

是否以百分比(true)或像素(false)返回框坐标。


pipelines~ZeroShotObjectDetectionPipelineType ⇒ <code> Promise. < (Array < ZeroShotObjectDetectionOutput > |Array < Array < ZeroShotObjectDetectionOutput > > ) > </code>

零样本对象检测管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<(Array<ZeroShotObjectDetectionOutput>|Array<Array<ZeroShotObjectDetectionOutput>>)> - 包含预测标签、分数和边界框的对象数组。

参数类型描述
imagesImagePipelineInputs

输入图像。

candidate_labelsArray.<string>

模型应在图像中识别的内容。

[options]ZeroShotObjectDetectionPipelineOptions

用于零样本对象检测的选项。

属性

名称类型默认值描述
labelstring

与找到的对象对应的文本查询。

scorenumber

与对象对应的分数(介于 0 和 1 之间)。

boxBoundingBox

图像原始尺寸中检测到的对象的边界框,或者如果 percentage 设置为 true,则为百分比形式。

[threshold]number0.1

进行预测所需的概率。

[top_k]number

管道将返回的顶部预测的数量。如果提供的数字为 null 或高于可用预测的数量,则将默认为预测的数量。

[percentage]booleanfalse

是否以百分比(true)或像素(false)返回框坐标。


pipelines~DocumentQuestionAnsweringPipelineType ⇒ <code> Promise. < (DocumentQuestionAnsweringOutput|Array < DocumentQuestionAnsweringOutput > ) > </code>

Kind: pipelines 的内部类型定义
返回: Promise.<(DocumentQuestionAnsweringOutput|Array<DocumentQuestionAnsweringOutput>)> - 包含答案的对象(或对象数组)。

参数类型描述
imageImageInput

要使用的文档图像。

questionstring

要向文档提出的问题。

[options]*

要传递给模型的 generate 方法的其他关键字参数。

属性

名称类型描述
answerstring

生成的文本。


pipelines~TextToAudioPipelineConstructorArgs : <code> Object </code>

Kind: pipelines 的内部类型定义
属性

名称类型描述
[vocoder]PreTrainedModel

管道使用的声码器(如果模型使用声码器)。如果未提供,则使用默认的 HifiGan 声码器。


pipelines~TextToAudioPipelineType ⇒ <code> Promise. < TextToAudioOutput > </code>

文本到音频管道特有的参数。

Kind: pipelines 的内部类型定义
返回: Promise.<TextToAudioOutput> - 包含生成的音频和采样率的对象。

参数类型描述
textsstring | Array<string>

要生成的文本。

optionsTextToAudioPipelineOptions

传递给模型生成/前向方法的参数。

属性

名称类型默认值描述
audioFloat32Array

生成的音频波形。

sampling_ratenumber

生成的音频波形的采样率。

[speaker_embeddings]Tensor | Float32Array | string | URL

说话人嵌入(如果模型需要)。


pipelines~ImageToImagePipelineType ⇒ <code> Promise. < (RawImage|Array < RawImage > ) > </code>

Kind: pipelines 的内部类型定义
返回: Promise.<(RawImage|Array<RawImage>)> - 转换后的图像或图像列表。

参数类型描述
imagesImagePipelineInputs

要转换的图像。


pipelines~DepthEstimationPipelineType ⇒ <code> Promise. < (DepthEstimationPipelineOutput|Array < DepthEstimationPipelineOutput > ) > </code>

Kind: pipelines 的内部类型定义
返回: Promise.<(DepthEstimationPipelineOutput|Array<DepthEstimationPipelineOutput>)> - 包含结果的图像或图像列表。

参数类型描述
imagesImagePipelineInputs

要计算深度的图像。

属性

名称类型描述
predicted_depthTensor

模型预测的原始深度图。

depthRawImage

处理后的深度图,作为图像(与输入图像大小相同)。


pipelines~AllTasks : <code> * </code>

所有可能的管道类型。

Kind: pipelines 的内部类型定义


< > 在 GitHub 上更新