Transformers.js 文件

管道

您正在檢視的是需要從原始碼安裝。如果您想透過常規的 npm install 進行安裝,請檢視最新的穩定版本(v3.0.0)。
Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

管道

管道(Pipelines)為執行機器學習模型提供了一個高階、易於使用的 API。

示例:使用 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 類是所有管道繼承的基類。不同管道之間共享的方法請參考此類。

型別pipelines 的靜態類


new Pipeline(options)

建立一個新的 Pipeline。

引數量型別預設描述
選項物件

包含以下屬性的物件

[options.task]字串

管道的任務。用於指定子任務。

[options.model]PreTrainedModel

管道使用的模型。

[options.tokenizer]PreTrainedTokenizer

管道使用的分詞器(如果有)。

[options.processor]處理器

管道使用的處理器(如果有)。


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

型別Pipeline 的例項方法


pipelines.TextClassificationPipeline

使用任何 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 }
// ]

型別pipelines 的靜態類


new TextClassificationPipeline(options)

建立一個新的 TextClassificationPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別TextClassificationPipeline 的例項方法


pipelines.TokenClassificationPipeline

使用任何 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' }
// ]

型別pipelines 的靜態類


new TokenClassificationPipeline(options)

建立一個新的 TokenClassificationPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別TokenClassificationPipeline 的例項方法


pipelines.QuestionAnsweringPipeline

使用任何 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
// }

型別pipelines 的靜態類


new QuestionAnsweringPipeline(options)

建立一個新的 QuestionAnsweringPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


questionAnsweringPipeline._call() : <code> QuestionAnsweringPipelineCallback </code>

型別QuestionAnsweringPipeline 的例項方法


pipelines.FillMaskPipeline

使用任何 ModelWithLMHead 的掩碼語言建模預測管道。

示例:使用 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.' }
// ]

示例:使用 Xenova/bert-base-cased 進行掩碼語言建模(也稱為“填空”)(並返回最佳結果)。

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.' }]

型別pipelines 的靜態類


new FillMaskPipeline(options)

建立一個新的 FillMaskPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別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." }]

型別pipelines 的靜態類


new Text2TextGenerationPipeline(options)

建立一個新的 Text2TextGenerationPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別Text2TextGenerationPipeline 的例項屬性


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

型別Text2TextGenerationPipeline 的例項方法


pipelines.SummarizationPipeline

一個用於摘要任務的管道,繼承自 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.' }]

型別pipelines 的靜態類


new SummarizationPipeline(options)

建立一個新的 SummarizationPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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.' }]

型別pipelines 的靜態類


new TranslationPipeline(options)

建立一個新的 TranslationPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別TranslationPipeline 的例項屬性


pipelines.TextGenerationPipeline

使用任何 ModelWithLMHeadModelForCausalLM 的語言生成管道。此管道預測指定文字提示後的詞語。注意:有關生成引數的完整列表,請參閱 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'
// }]

型別pipelines 的靜態類


new TextGenerationPipeline(options)

建立一個新的 TextGenerationPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別TextGenerationPipeline 的例項方法


pipelines.ZeroShotClassificationPipeline

基於自然語言推理(NLI)的零樣本分類管道,使用在 NLI 任務上訓練的 ModelForSequenceClassification。等同於 text-classification 管道,但這些模型不需要硬編碼的潛在類別數量,可以在執行時選擇。這通常意味著它速度較慢,但靈活性要強得多

示例:使用 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 ]
// }

型別pipelines 的靜態類


new ZeroShotClassificationPipeline(options)

建立一個新的 ZeroShotClassificationPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別ZeroShotClassificationPipeline 的例項屬性


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

型別ZeroShotClassificationPipeline 的例項方法


pipelines.FeatureExtractionPipeline

不使用模型頭的特徵提取管道。此管道從基礎 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]
// }

型別pipelines 的靜態類


new FeatureExtractionPipeline(options)

建立一個新的 FeatureExtractionPipeline。

引數量型別描述
選項TextPipelineConstructorArgs

用於例項化管道的物件。


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

型別FeatureExtractionPipeline 的例項方法


pipelines.ImageFeatureExtractionPipeline

不使用模型頭的影像特徵提取管道。此管道從基礎 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
// }

型別pipelines 的靜態類


new ImageFeatureExtractionPipeline(options)

建立一個新的 ImageFeatureExtractionPipeline。

引數量型別描述
選項ImagePipelineConstructorArgs

用於例項化管道的物件。


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

型別ImageFeatureExtractionPipeline 的例項方法


pipelines.AudioClassificationPipeline

使用任何 AutoModelForAudioClassification 的音訊分類管道。此管道預測原始波形或音訊檔案的類別。

示例:使用 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 },
// ]

型別pipelines 的靜態類


new AudioClassificationPipeline(options)

建立一個新的 AudioClassificationPipeline。

引數量型別描述
選項AudioPipelineConstructorArgs

用於例項化管道的物件。


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

型別AudioClassificationPipeline 的例項方法


pipelines.ZeroShotAudioClassificationPipeline

使用 ClapModel 的零樣本音訊分類管道。當你提供一個音訊和一組 candidate_labels 時,此管道會預測音訊的類別。

示例:使用 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' }
// ]

型別pipelines 的靜態類


new ZeroShotAudioClassificationPipeline(options)

建立一個新的 ZeroShotAudioClassificationPipeline。

引數量型別描述
選項TextAudioPipelineConstructorArgs

用於例項化管道的物件。


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

型別ZeroShotAudioClassificationPipeline 的例項方法


pipelines.AutomaticSpeechRecognitionPipeline

旨在從音訊中提取口語文字的管道。

示例:轉錄英語。

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" }

型別pipelines 的靜態類


new AutomaticSpeechRecognitionPipeline(options)

建立一個新的 AutomaticSpeechRecognitionPipeline。

引數量型別描述
選項TextAudioPipelineConstructorArgs

用於例項化管道的物件。


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

型別AutomaticSpeechRecognitionPipeline 的例項方法


pipelines.ImageToTextPipeline

使用 AutoModelForVision2Seq 的影像到文字管道。此管道為給定影像預測標題。

示例:使用 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.' }]

型別pipelines 的靜態類


new ImageToTextPipeline(options)

建立一個新的 ImageToTextPipeline。

引數量型別描述
選項TextImagePipelineConstructorArgs

用於例項化管道的物件。


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 },
//   ...
// ]

型別pipelines 的靜態類


new ImageClassificationPipeline(options)

建立一個新的 ImageClassificationPipeline。

引數量型別描述
選項ImagePipelineConstructorArgs

用於例項化管道的物件。


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 { ... } }
// ]

型別pipelines 的靜態類


new ImageSegmentationPipeline(options)

建立一個新的 ImageSegmentationPipeline。

引數量型別描述
選項ImagePipelineConstructorArgs

用於例項化管道的物件。


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 }
// ]

型別pipelines 的靜態類


new BackgroundRemovalPipeline(options)

建立一個新的 BackgroundRemovalPipeline。

引數量型別描述
選項ImagePipelineConstructorArgs

用於例項化管道的物件。


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' }
// ]

型別pipelines 的靜態類


new ZeroShotImageClassificationPipeline(options)

建立一個新的 ZeroShotImageClassificationPipeline。

引數量型別描述
選項TextImagePipelineConstructorArgs

用於例項化管道的物件。


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 }
// }]

型別pipelines 的靜態類


new ObjectDetectionPipeline(options)

建立一個新的 ObjectDetectionPipeline。

引數量型別描述
選項ImagePipelineConstructorArgs

用於例項化管道的物件。


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 }
//   }
// ]

型別pipelines 的靜態類


new ZeroShotObjectDetectionPipeline(options)

建立一個新的 ZeroShotObjectDetectionPipeline。

引數量型別描述
選項TextImagePipelineConstructorArgs

用於例項化管道的物件。


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' }]

型別pipelines 的靜態類


new DocumentQuestionAnsweringPipeline(options)

建立一個新的 DocumentQuestionAnsweringPipeline。

引數量型別描述
選項TextImagePipelineConstructorArgs

用於例項化管道的物件。


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
// }

型別pipelines 的靜態類


new TextToAudioPipeline(options)

建立一個新的 TextToAudioPipeline。

引數量型別描述
選項TextToAudioPipelineConstructorArgs

用於例項化管道的物件。


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
// }

型別pipelines 的靜態類


new ImageToImagePipeline(options)

建立一個新的 ImageToImagePipeline。

引數量型別描述
選項ImagePipelineConstructorArgs

用於例項化管道的物件。


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
//   }
// }

型別pipelines 的靜態類


new DepthEstimationPipeline(options)

建立一個新的 DepthEstimationPipeline。

引數量型別描述
選項ImagePipelineConstructorArgs

用於例項化管道的物件。


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

種類: DepthEstimationPipeline 的例項方法


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

用於構建 Pipeline 物件的實用工廠方法。

種類: pipelines 的靜態方法
返回: * - 針對指定任務的 Pipeline 物件。
丟擲:

  • Error 如果請求了不支援的流水線。
引數量型別預設描述
任務任務

定義將返回哪個流水線的任務。當前接受的任務有:

  • "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]字串null

要使用的預訓練模型的名稱。如果未指定,將使用該任務的預設模型。

[options]*

流水線的可選引數。


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

種類: pipelines 的內部型別定義


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

種類: pipelines 的內部型別定義


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

種類: pipelines 的內部型別定義
屬性

名稱型別描述
xmin數字

邊界框的最小 x 座標。

ymin數字

邊界框的最小 y 座標。

xmax數字

邊界框的最大 x 座標。

ymax數字

邊界框的最大 y 座標。


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

種類: pipelines 的內部型別定義
返回: Promise.<void> - 一個在專案被釋放時解析的 Promise。
屬性

名稱型別描述
disposeDisposeType

一個在流水線被釋放時解析的 Promise。


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

用於例項化基於文字的流水線的物件。

種類: pipelines 的內部型別定義
屬性

名稱型別描述
任務字串

管道的任務。用於指定子任務。

模型PreTrainedModel

管道使用的模型。

tokenizerPreTrainedTokenizer

流水線使用的分詞器。


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

用於例項化基於音訊的流水線的物件。

種類: pipelines 的內部型別定義
屬性

名稱型別描述
任務字串

管道的任務。用於指定子任務。

模型PreTrainedModel

管道使用的模型。

processor處理器

流水線使用的處理器。


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

用於例項化基於文字和音訊的流水線的物件。

種類: pipelines 的內部型別定義
屬性

名稱型別描述
任務字串

管道的任務。用於指定子任務。

模型PreTrainedModel

管道使用的模型。

tokenizerPreTrainedTokenizer

流水線使用的分詞器。

processor處理器

流水線使用的處理器。


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

特定於文字分類流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(TextClassificationOutput|Array<TextClassificationOutput>)> - 包含預測標籤和分數的陣列或物件。

引數量型別描述
textsstring | Array<string>

要分類的輸入文字。

[options]TextClassificationPipelineOptions

用於文字分類的選項。

屬性

名稱型別預設描述
label字串

預測的標籤。

score數字

對應的機率。

[top_k]數字1

要返回的最高預測數量。


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

特定於詞元分類流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(TokenClassificationOutput|Array<TokenClassificationOutput>)> - 結果。

引數量型別描述
textsstring | Array<string>

一個或多個用於詞元分類的文字(或一個文字列表)。

[options]TokenClassificationPipelineOptions

用於詞元分類的選項。

屬性

名稱型別描述
word字串

被分類的詞元/單詞。這是透過解碼所選詞元獲得的。

score數字

entity 對應的機率。

entity字串

為該詞元/單詞預測的實體。

索引數字

相應詞元在句子中的索引。

[start]數字

相應實體在句子中的起始索引。

[end]數字

相應實體在句子中的結束索引。

[ignore_labels]Array.<string>

要忽略的標籤列表。


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

特定於問答流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(QuestionAnsweringOutput|Array<QuestionAnsweringOutput>)> - 包含預測答案和分數的陣列或物件。

引數量型別描述
questionstring | Array<string>

一個或多個問題(必須與 context 引數一起使用)。

contextstring | Array<string>

與問題相關聯的一個或多個上下文(必須與 question 引數一起使用)。

[options]QuestionAnsweringPipelineOptions

用於問答的選項。

屬性

名稱型別預設描述
score數字

與答案相關的機率。

[start]數字

答案的字元起始索引(在輸入的詞元化版本中)。

[end]數字

答案的字元結束索引(在輸入的詞元化版本中)。

答案字串

問題的答案。

[top_k]數字1

要返回的最高答案預測數量。


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

特定於填空流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(FillMaskOutput|Array<FillMaskOutput>)> - 一個包含分數、預測詞元、預測詞元字串和已填充預測詞元序列的物件陣列,或者此類陣列的陣列(每個輸入文字一個)。如果只給出一個輸入文字,輸出將是一個物件陣列。
丟擲:

  • Error 當在輸入文字中找不到掩碼詞元時。
引數量型別描述
textsstring | Array<string>

一個或多個帶有掩碼詞元的文字(或一個提示列表)。

[options]FillMaskPipelineOptions

用於掩碼語言建模的選項。

屬性

名稱型別預設描述
sequence字串

帶有掩碼詞元預測的相應輸入。

score數字

對應的機率。

token數字

預測的詞元ID(用於替換掩碼詞元)。

token_str字串

預測的詞元(用於替換掩碼詞元)。

[top_k]數字5

當傳遞時,覆蓋要返回的預測數量。


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

種類: pipelines 的內部型別定義

引數量型別描述
textsstring | Array<string>

編碼器的輸入文字。

[options]*

傳遞給模型 generate 方法的其他關鍵字引數。

屬性

名稱型別描述
generated_text字串

生成的文字。


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

種類: pipelines 的內部型別定義

引數量型別描述
textsstring | Array<string>

要摘要的一篇或多篇文章(或一個文章列表)。

[options]*

傳遞給模型 generate 方法的其他關鍵字引數。

屬性

名稱型別描述
summary_text字串

摘要文字。


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

種類: pipelines 的內部型別定義

引數量型別描述
textsstring | Array<string>

待翻譯的文字。

[options]*

傳遞給模型 generate 方法的其他關鍵字引數。

屬性

名稱型別描述
translation_text字串

翻譯後的文字。


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

特定於文字生成流水線的引數。

種類: pipelines 的內部型別定義
返回: 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>

特定於零樣本分類流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(ZeroShotClassificationOutput|Array<ZeroShotClassificationOutput>)> - 包含預測標籤和分數的陣列或物件。

引數量型別描述
textsstring | Array<string>

要分類的序列,如果模型輸入過大,將被截斷。

candidate_labelsstring | Array<string>

用於將每個序列分類的可能類別標籤集合。可以是一個標籤、一個逗號分隔的標籤字串或一個標籤列表。

[options]ZeroShotClassificationPipelineOptions

用於零樣本分類的選項。

屬性

名稱型別預設描述
sequence字串

此輸出對應的序列。

labelsArray.<string>

按可能性順序排序的標籤。

scoresArray.<number>

每個標籤的機率。

[hypothesis_template]字串""這個例子是{}。""

用於將每個候選標籤轉換為 NLI 風格假設的模板。候選標籤將替換 &#123;} 佔位符。

[multi_label]booleanfalse

是否可以有多個候選標籤為真。如果為 false,則分數將被歸一化,使得每個序列的標籤似然總和為1。如果為 true,則標籤被視為獨立的,並透過對蘊含分數與矛盾分數進行 softmax 來為每個候選標籤歸一化機率。


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

特定於特徵提取流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<Tensor> - 模型計算出的特徵。

引數量型別描述
textsstring | Array<string>

一個或多個要提取特徵的文字(或一個文字列表)。

[options]FeatureExtractionPipelineOptions

用於特徵提取的選項。

屬性

名稱型別預設描述
[pooling]'none' | 'mean' | 'cls' | 'first_token' | 'eos' | 'last_token'"none"

要使用的池化方法。

[normalize]booleanfalse

是否在最後一個維度上對嵌入進行歸一化。

[quantize]booleanfalse

是否對嵌入進行量化。

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

用於量化的精度。


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

特定於影像特徵提取流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<Tensor> - 模型計算出的影像特徵。

引數量型別描述
imagesImagePipelineInputs

一個或多個要提取特徵的影像(或一個影像列表)。

[options]ImageFeatureExtractionPipelineOptions

用於影像特徵提取的選項。

屬性

名稱型別預設描述
[pool]boolean

是否返回池化後的輸出。如果設定為 false,模型將返回原始的隱藏狀態。


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

特定於音訊分類流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(AudioClassificationOutput|Array<AudioClassificationOutput>)> - 包含預測標籤和分數的陣列或物件。

引數量型別描述
音訊AudioPipelineInputs

要分類的輸入音訊檔案。輸入可以是

  • stringURL,即音訊檔案的檔名/URL,檔案將以處理器的取樣率讀取,使用 AudioContext API 獲取波形。如果 AudioContext 不可用,您應以形狀為 (n, ) 的 Float32Array 形式傳入原始波形。
  • 形狀為 (n, )Float32ArrayFloat64Array,表示正確取樣率下的原始音訊(不會進行進一步檢查)。
[options]AudioClassificationPipelineOptions

用於音訊分類的選項。

屬性

名稱型別預設描述
label字串

預測的標籤。

score數字

對應的機率。

[top_k]數字5

流水線將返回的最高標籤數量。如果提供的數量為 null 或高於模型配置中可用的標籤數量,則將預設為標籤數量。


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

特定於零樣本音訊分類流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(Array<ZeroShotAudioClassificationOutput>|Array<Array<ZeroShotAudioClassificationOutput>>)> - 包含預測標籤和分數的物件陣列。

引數量型別描述
音訊AudioPipelineInputs

要分類的輸入音訊檔案。輸入可以是

  • stringURL,即音訊檔案的檔名/URL,檔案將以處理器的取樣率讀取,使用 AudioContext API 獲取波形。如果 AudioContext 不可用,您應以形狀為 (n, ) 的 Float32Array 形式傳入原始波形。
  • 形狀為 (n, )Float32ArrayFloat64Array,表示正確取樣率下的原始音訊(不會進行進一步檢查)。
candidate_labelsArray.<string>

此音訊的候選標籤。

[options]ZeroShotAudioClassificationPipelineOptions

用於零樣本音訊分類的選項。

屬性

名稱型別預設描述
label字串

模型識別的標籤。它是建議的 candidate_label 之一。

score數字

模型為該標籤賦予的分數(介於0和1之間)。

[hypothesis_template]字串""這是一個{}的聲音。""

candidate_labels 結合使用的句子,透過用候選標籤替換佔位符來嘗試進行音訊分類。然後使用 logits_per_audio 估計可能性。


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

種類: pipelines 的內部型別定義
屬性

名稱型別描述
時間戳*

塊的開始和結束時間戳(以秒為單位)。

text字串

識別出的文字。


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

特定於自動語音識別流水線的引數。

種類: pipelines 的內部型別定義
返回: Promise.<(AutomaticSpeechRecognitionOutput|Array<AutomaticSpeechRecognitionOutput>)> - 包含轉錄文字的物件,如果 return_timestampstrue,則還包含時間戳。

引數量型別描述
音訊AudioPipelineInputs

待轉錄的輸入音訊檔案。

  • stringURL,即音訊檔案的檔名/URL,檔案將以處理器的取樣率讀取,使用 AudioContext API 獲取波形。如果 AudioContext 不可用,您應以形狀為 (n, ) 的 Float32Array 形式傳入原始波形。
  • 形狀為 (n, )Float32ArrayFloat64Array,表示正確取樣率下的原始音訊(不會進行進一步檢查)。
[options]Partial.<AutomaticSpeechRecognitionConfig>

傳遞給模型 generate 方法的其他關鍵字引數。

屬性

名稱型別描述
text字串

識別出的文字。

[chunks]Array.<Chunk>

當使用 return_timestamps 時,chunks 將成為一個包含模型識別出的所有不同文字塊的列表。

[return_timestamps]boolean | 'word'

是否返回時間戳。預設為 false

[chunk_length_s]數字

要處理的音訊塊的長度(以秒為單位)。預設為 0(不分塊)。

[stride_length_s]數字

連續音訊塊之間的重疊長度(以秒為單位)。如果未提供,則預設為 chunk_length_s / 6

[force_full_sequences]boolean

是否強制輸出完整序列。預設為 false

[language]字串

源語言。預設為 null,表示應自動檢測。如果源語言已知,使用此選項可能會提高效能。

[task]字串

要執行的任務。預設為 null,表示應自動檢測。

[num_frames]數字

輸入音訊中的幀數。


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

種類: pipelines 的內部型別定義
返回: Promise.<(ImageToTextOutput|Array<ImageToTextOutput>)> - 包含生成文字的物件(或物件陣列)。

引數量型別描述
textsImagePipelineInputs

待加標題的影像。

[options]*

傳遞給模型 generate 方法的其他關鍵字引數。

屬性

名稱型別描述
generated_text字串

生成的文字。


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

特定於影像分類管道的引數。

種類: pipelines 的內部型別定義
返回Promise.<(ImageClassificationOutput|Array<ImageClassificationOutput>)> - 包含預測標籤和分數的陣列或物件。

引數量型別描述
imagesImagePipelineInputs

要分類的輸入影像。

[options]ImageClassificationPipelineOptions

用於影像分類的選項。

屬性

名稱型別預設描述
label字串

模型識別的標籤。

score數字

模型為該標籤賦予的分數。

[top_k]數字1

管道將返回的排名靠前的標籤數量。


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

特定於影像分割管道的引數。

種類: pipelines 的內部型別定義
返回Promise.<Array<ImageSegmentationPipelineOutput>> - 帶標註的分割區域。

引數量型別描述
imagesImagePipelineInputs

輸入影像。

[options]ImageSegmentationPipelineOptions

用於影像分割的選項。

屬性

名稱型別預設描述
labelstring | null

分割區域的標籤。

scorenumber | null

分割區域的分數。

蒙版RawImage

分割區域的掩碼。

[threshold]數字0.5

用於過濾預測掩碼的機率閾值。

[mask_threshold]數字0.5

將預測掩碼轉換為二進位制值時使用的閾值。

[overlap_mask_area_threshold]數字0.8

用於消除小的、不連通分割區域的掩碼重疊閾值。

[subtask]null | string

要執行的分割任務。根據模型的能力,可以是 [panopticinstancesemantic] 中的一種。如果未設定,管道將按此順序嘗試解析。

[label_ids_to_fuse]Array.<number>

要融合的標籤 ID 列表。如果未設定,則不融合任何標籤。

[target_sizes]Array.<Array<number>>

輸入影像的目標尺寸列表。如果未設定,則使用原始影像尺寸。


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

特定於影像分割管道的引數。

種類: pipelines 的內部型別定義
返回Promise.<Array<RawImage>> - 已移除背景的影像。

引數量型別描述
imagesImagePipelineInputs

輸入影像。

[options]BackgroundRemovalPipelineOptions

用於影像分割的選項。


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

特定於零樣本影像分類管道的引數。

種類: pipelines 的內部型別定義
返回Promise.<(Array<ZeroShotImageClassificationOutput>|Array<Array<ZeroShotImageClassificationOutput>>)> - 包含預測標籤和分數的物件陣列。

引數量型別描述
imagesImagePipelineInputs

輸入影像。

candidate_labelsArray.<string>

此影像的候選標籤。

[options]ZeroShotImageClassificationPipelineOptions

用於零樣本影像分類的選項。

屬性

名稱型別預設描述
label字串

模型識別的標籤。它是建議的 candidate_label 之一。

score數字

模型為該標籤賦予的分數(介於0和1之間)。

[hypothesis_template]字串""這是一張{}的照片""

candidate_labels 結合使用的句子,透過將佔位符替換為候選標籤來嘗試進行影像分類。然後使用 logits_per_image 估計可能性。


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

特定於目標檢測管道的引數。

種類: pipelines 的內部型別定義
返回Promise.<(ObjectDetectionPipelineOutput|Array<ObjectDetectionPipelineOutput>)> - 物件列表或物件列表的列表。

引數量型別描述
imagesImagePipelineInputs

輸入影像。

[options]ObjectDetectionPipelineOptions

用於目標檢測的選項。

屬性

名稱型別預設描述
label字串

模型識別的類別標籤。

score數字

模型為該標籤賦予的分數。

boxBoundingBox

檢測到的物件在影像原始尺寸中的邊界框,如果 percentage 設定為 true,則為百分比。

[threshold]數字0.9

用於按分數篩選邊界框的閾值。

[percentage]booleanfalse

是否以百分比(true)或畫素(false)返回邊界框座標。


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

特定於零樣本目標檢測管道的引數。

種類: pipelines 的內部型別定義
返回Promise.<(Array<ZeroShotObjectDetectionOutput>|Array<Array<ZeroShotObjectDetectionOutput>>)> - 包含預測標籤、分數和邊界框的物件陣列。

引數量型別描述
imagesImagePipelineInputs

輸入影像。

candidate_labelsArray.<string>

模型應在影像中識別的內容。

[options]ZeroShotObjectDetectionPipelineOptions

用於零樣本目標檢測的選項。

屬性

名稱型別預設描述
label字串

與找到的物件對應的文字查詢。

score數字

與物件對應的分數(介於 0 和 1 之間)。

boxBoundingBox

檢測到的物件在影像原始尺寸中的邊界框,如果 percentage 設定為 true,則為百分比。

[threshold]數字0.1

做出預測所需的機率。

[top_k]數字

管道將返回的排名靠前的預測數量。如果提供的數字為 null 或高於可用預測的數量,則將預設為預測的總數。

[percentage]booleanfalse

是否以百分比(true)或畫素(false)返回邊界框座標。


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

種類: pipelines 的內部型別定義
返回Promise.<(DocumentQuestionAnsweringOutput|Array<DocumentQuestionAnsweringOutput>)> - 包含答案的物件(或物件陣列)。

引數量型別描述
imageImageInput

要使用的文件影像。

question字串

要對文件提出的問題。

[options]*

傳遞給模型 generate 方法的其他關鍵字引數。

屬性

名稱型別描述
答案字串

生成的文字。


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

種類: pipelines 的內部型別定義
屬性

名稱型別描述
[vocoder]PreTrainedModel

管道使用的聲碼器(如果模型使用)。如果未提供,則使用預設的 HifiGan 聲碼器。


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

特定於文字到音訊管道的引數。

種類: pipelines 的內部型別定義
返回Promise.<TextToAudioOutput> - 包含生成的音訊和取樣率的物件。

引數量型別描述
textsstring | Array<string>

要生成的文字。

選項TextToAudioPipelineOptions

傳遞給模型生成/前向方法的引數。

屬性

名稱型別預設描述
音訊Float32Array

生成的音訊波形。

sampling_rate數字

生成的音訊波形的取樣率。

[speaker_embeddings]Tensor | Float32Array | string | URL

說話人嵌入(如果模型需要)。


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

種類: pipelines 的內部型別定義
返回Promise.<(RawImage|Array<RawImage>)> - 轉換後的影像或影像列表。

引數量型別描述
imagesImagePipelineInputs

要轉換的影像。


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

種類: pipelines 的內部型別定義
返回Promise.<(DepthEstimationPipelineOutput|Array<DepthEstimationPipelineOutput>)> - 包含結果的影像或影像列表。

引數量型別描述
imagesImagePipelineInputs

要計算深度的影像。

屬性

名稱型別描述
predicted_depth張量

模型預測的原始深度圖。

depthRawImage

處理後的深度圖,作為影像(與輸入影像大小相同)。


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

所有可能的管道型別。

種類: pipelines 的內部型別定義


< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.