async runWithInputs()

in src/nodes/client/text-classification.ts [36:83]


  async runWithInputs(inputs: Inputs) {
    const { text, modelid, device, quantized } = inputs;

    const _modelid = modelid?.trim();
    if (!text) {
      // No input node
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: { results: null, text: text } })
      );
      return;
    }

    if (this.cachedResult && compareObjects(this.cachedInput, inputs)) {
      this.dispatchEvent(
        new CustomEvent("outputs", {
          detail: { results: this.cachedResult, text: text },
        })
      );
      return;
    }

    const classifier: TextClassificationPipeline = await this.getInstance(
      _modelid,
      quantized,
      device
    );

    const result = await classifier(text, {
      topk: 5,
    });

    const resultArray = (
      Array.isArray(result) ? result : [result]
    ) as TextClassificationSingle[];

    const classProb = resultArray.map((e) => ({
      className: e.label,
      probability: e.score,
    }));
    this.cachedInput = inputs;
    this.cachedResult = { classes: classProb };

    this.dispatchEvent(
      new CustomEvent("outputs", {
        detail: { results: { classes: classProb }, text: text },
      })
    );
  }