@workglow/huggingface-transformers 0.2.35 → 0.2.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ai.js CHANGED
@@ -736,6 +736,7 @@ var HFT_TEXT_CLASSIFICATION = ["text.classification"];
736
736
  var HFT_TEXT_LANGUAGE_DETECTION = [
737
737
  "text.language-detection"
738
738
  ];
739
+ var HFT_TEXT_RERANKING = ["text.reranking"];
739
740
  var HFT_TEXT_FILL_MASK = ["text.fill-mask"];
740
741
  var HFT_TEXT_NER = ["text.ner"];
741
742
  var HFT_IMAGE_CLASSIFICATION = ["image.classification"];
@@ -764,6 +765,7 @@ var HFT_CAPABILITY_SETS = [
764
765
  HFT_TEXT_EMBEDDING,
765
766
  HFT_TEXT_CLASSIFICATION,
766
767
  HFT_TEXT_LANGUAGE_DETECTION,
768
+ HFT_TEXT_RERANKING,
767
769
  HFT_TEXT_FILL_MASK,
768
770
  HFT_TEXT_NER,
769
771
  HFT_IMAGE_CLASSIFICATION,
@@ -985,21 +987,6 @@ var HFT_BackgroundRemoval = async (input, model, signal, emit) => {
985
987
  // src/ai/common/HFT_Chat.ts
986
988
  init_HFT_Pipeline();
987
989
 
988
- // src/ai/common/HFT_ToolCalling.ts
989
- init_HFT_Pipeline();
990
- import {
991
- buildToolDescription,
992
- filterValidToolCalls,
993
- toTextFlatMessages
994
- } from "@workglow/ai/worker";
995
- import {
996
- adaptParserResult,
997
- forcedToolSelection,
998
- getAvailableParsers,
999
- getGenerationPrefix,
1000
- parseToolCalls
1001
- } from "@workglow/ai/provider-utils";
1002
-
1003
990
  // src/ai/common/HFT_Streaming.ts
1004
991
  function createStreamingTextStreamer(tokenizer, onText, textStreamer) {
1005
992
  return new textStreamer(tokenizer, {
@@ -1023,6 +1010,19 @@ function createTextStreamer(tokenizer, updateProgress, textStreamer) {
1023
1010
  }
1024
1011
 
1025
1012
  // src/ai/common/HFT_ToolCalling.ts
1013
+ init_HFT_Pipeline();
1014
+ import {
1015
+ adaptParserResult,
1016
+ forcedToolSelection,
1017
+ getAvailableParsers,
1018
+ getGenerationPrefix,
1019
+ parseToolCalls
1020
+ } from "@workglow/ai/provider-utils";
1021
+ import {
1022
+ buildToolDescription,
1023
+ filterValidToolCalls,
1024
+ toTextFlatMessages
1025
+ } from "@workglow/ai/worker";
1026
1026
  function getModelTextCandidates(model) {
1027
1027
  return [model.model_id, model.title, model.description, model.provider_config.model_path].filter((value) => typeof value === "string" && value.length > 0).map((value) => value.toLowerCase());
1028
1028
  }
@@ -1442,8 +1442,8 @@ var HFT_ImageClassification = async (input, model, signal, emit) => {
1442
1442
 
1443
1443
  // src/ai/common/HFT_ImageEmbedding.ts
1444
1444
  init_HFT_Pipeline();
1445
- import { getLogger as getLogger3 } from "@workglow/util/worker";
1446
1445
  import { imageValueToBlob as imageValueToBlob3 } from "@workglow/ai/provider-utils";
1446
+ import { getLogger as getLogger3 } from "@workglow/util/worker";
1447
1447
  var HFT_ImageEmbedding = async (input, model, signal, emit) => {
1448
1448
  const logger = getLogger3();
1449
1449
  const timerLabel = `hft:ImageEmbedding:${model?.provider_config.model_path}`;
@@ -1475,6 +1475,29 @@ var HFT_ImageEmbedding = async (input, model, signal, emit) => {
1475
1475
  // src/ai/common/HFT_ImageSegmentation.ts
1476
1476
  init_HFT_Pipeline();
1477
1477
  import { imageValueToBlob as imageValueToBlob4 } from "@workglow/ai/provider-utils";
1478
+ import { imageValueFromBuffer } from "@workglow/util/media";
1479
+ function rawImageToImageValue(image) {
1480
+ const raw = image;
1481
+ const pixelCount = raw.width * raw.height;
1482
+ const rgba = new Uint8Array(pixelCount * 4);
1483
+ for (let pixel = 0;pixel < pixelCount; pixel++) {
1484
+ const sourceOffset = pixel * raw.channels;
1485
+ const targetOffset = pixel * 4;
1486
+ if (raw.channels === 1) {
1487
+ const value = raw.data[sourceOffset] ?? 0;
1488
+ rgba[targetOffset] = value;
1489
+ rgba[targetOffset + 1] = value;
1490
+ rgba[targetOffset + 2] = value;
1491
+ rgba[targetOffset + 3] = 255;
1492
+ } else {
1493
+ rgba[targetOffset] = raw.data[sourceOffset] ?? 0;
1494
+ rgba[targetOffset + 1] = raw.data[sourceOffset + 1] ?? raw.data[sourceOffset] ?? 0;
1495
+ rgba[targetOffset + 2] = raw.data[sourceOffset + 2] ?? raw.data[sourceOffset] ?? 0;
1496
+ rgba[targetOffset + 3] = raw.channels >= 4 ? raw.data[sourceOffset + 3] ?? 255 : 255;
1497
+ }
1498
+ }
1499
+ return imageValueFromBuffer(Buffer.from(rgba), "raw-rgba", raw.width, raw.height);
1500
+ }
1478
1501
  var HFT_ImageSegmentation = async (input, model, signal, emit) => {
1479
1502
  const segmenter = await getPipeline(model, emit, {}, signal);
1480
1503
  const imageArg = await imageValueToBlob4(input.image);
@@ -1483,11 +1506,13 @@ var HFT_ImageSegmentation = async (input, model, signal, emit) => {
1483
1506
  mask_threshold: input.maskThreshold
1484
1507
  });
1485
1508
  const masks = Array.isArray(result) ? result : [result];
1486
- const processedMasks = await Promise.all(masks.map(async (mask) => ({
1487
- label: mask.label || "",
1488
- score: mask.score || 0,
1489
- mask: {}
1490
- })));
1509
+ const processedMasks = masks.map((mask) => {
1510
+ return {
1511
+ label: mask.label || "",
1512
+ score: mask.score || 0,
1513
+ mask: rawImageToImageValue(mask.mask)
1514
+ };
1515
+ });
1491
1516
  emit({
1492
1517
  type: "finish",
1493
1518
  data: {
@@ -1614,7 +1639,7 @@ var HFT_ModelInfo = async (input, model, _signal, emit) => {
1614
1639
  };
1615
1640
 
1616
1641
  // src/ai/common/HFT_ModelSearch.ts
1617
- import { searchHfModels, mapHfModelResult } from "@workglow/ai/provider-utils";
1642
+ import { mapHfModelResult, searchHfModels } from "@workglow/ai/provider-utils";
1618
1643
  var HFT_ModelSearch = async (input, _model, signal, emit) => {
1619
1644
  const entries = await searchHfModels(input.query?.trim() ?? "", { filter: "onnx" }, ["siblings"], signal);
1620
1645
  const results = entries.map((entry) => {
@@ -1959,6 +1984,54 @@ var HFT_TextQuestionAnswer = async (input, model, signal, emit) => {
1959
1984
  emit({ type: "finish", data: { text: answerText } });
1960
1985
  };
1961
1986
 
1987
+ // src/ai/common/HFT_TextReranker.ts
1988
+ init_HFT_Pipeline();
1989
+ import { KbRerankerOutputError } from "@workglow/ai";
1990
+ import { getLogger as getLogger6 } from "@workglow/util/worker";
1991
+ function isScored(v) {
1992
+ return typeof v === "object" && v !== null && typeof v.score === "number";
1993
+ }
1994
+ function truncateShape(value) {
1995
+ try {
1996
+ const json = JSON.stringify(value);
1997
+ if (typeof json !== "string")
1998
+ return String(value);
1999
+ return json.length > 200 ? `${json.slice(0, 200)}…` : json;
2000
+ } catch {
2001
+ return String(value);
2002
+ }
2003
+ }
2004
+ function validateAndExtractRerankerScores(rawResults, modelPath) {
2005
+ if (!Array.isArray(rawResults)) {
2006
+ throw new KbRerankerOutputError(`HFT_TextReranker: unexpected pipeline output shape for model ${modelPath}`, truncateShape(rawResults));
2007
+ }
2008
+ const scores = new Array(rawResults.length);
2009
+ for (let i = 0;i < rawResults.length; i++) {
2010
+ const entry = rawResults[i];
2011
+ const candidate = Array.isArray(entry) ? entry[0] : entry;
2012
+ if (!isScored(candidate)) {
2013
+ throw new KbRerankerOutputError(`HFT_TextReranker: unexpected pipeline output shape for model ${modelPath}`, truncateShape(entry));
2014
+ }
2015
+ scores[i] = candidate.score;
2016
+ }
2017
+ return scores;
2018
+ }
2019
+ var HFT_TextReranker = async (input, model, signal, emit) => {
2020
+ const logger = getLogger6();
2021
+ const modelPath = model?.provider_config.model_path;
2022
+ const timerLabel = `hft:TextReranker:${modelPath}`;
2023
+ logger.time(timerLabel, { docs: input.documents.length });
2024
+ const reranker = await getPipeline(model, emit, {}, signal);
2025
+ const pairs = input.documents.map((doc) => ({ text: input.query, text_pair: doc }));
2026
+ const callable = reranker;
2027
+ const rawResults = await callable(pairs, { top_k: 1 });
2028
+ const scores = validateAndExtractRerankerScores(rawResults, modelPath);
2029
+ const indices = scores.map((score, idx) => ({ score, idx })).sort((a, b) => b.score - a.score).map((p) => p.idx);
2030
+ const limited = typeof input.topK === "number" ? indices.slice(0, input.topK) : indices;
2031
+ logger.timeEnd(timerLabel, { docs: input.documents.length });
2032
+ emit({ type: "finish", data: { scores, indices: limited } });
2033
+ };
2034
+
1962
2035
  // src/ai/common/HFT_TextRewriter.ts
1963
2036
  init_HFT_Pipeline();
1964
2037
  var HFT_TextRewriter = async (input, model, signal, emit) => {
@@ -2034,6 +2107,7 @@ var HFT_RUN_FNS = [
2034
2107
  { serves: HFT_TEXT_EMBEDDING, runFn: HFT_TextEmbedding },
2035
2108
  { serves: HFT_TEXT_CLASSIFICATION, runFn: HFT_TextClassification },
2036
2109
  { serves: HFT_TEXT_LANGUAGE_DETECTION, runFn: HFT_TextLanguageDetection },
2110
+ { serves: HFT_TEXT_RERANKING, runFn: HFT_TextReranker },
2037
2111
  { serves: HFT_TEXT_FILL_MASK, runFn: HFT_TextFillMask },
2038
2112
  { serves: HFT_TEXT_NER, runFn: HFT_TextNamedEntityRecognition },
2039
2113
  { serves: HFT_IMAGE_CLASSIFICATION, runFn: HFT_ImageClassification },
@@ -2076,4 +2150,4 @@ export {
2076
2150
  HF_TRANSFORMERS_ONNX
2077
2151
  };
2078
2152
 
2079
- //# debugId=F0665A2512C7736664756E2164756E21
2153
+ //# debugId=F1F15E5800BCD03B64756E2164756E21