async runWithInputs()

in src/nodes/server/fill-mask.ts [29:84]


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

    const _modelid = modelid?.trim();
    if (this.hf && apikey) {
      this.hf = new HfInference(apikey);
    }

    if (!text) {
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: { results: null } })
      );
      return;
    }

    if (this.cachedOutput && compareObjects(this.cachedInputs, inputs)) {
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: this.cachedOutput })
      );
      return;
    }

    try {
      const fillMasksRes = await this.hf?.fillMask({
        model: _modelid,
        inputs: text,
      });
      if (!fillMasksRes) {
        throw new Error("Invalid response");
      }
      // remap to visualblocks classification result
      const result = fillMasksRes.map((e) => ({
        className: e.token_str,
        probability: e.score,
      }));

      const output: Outputs = {
        results: { classes: result },
      };
      this.cachedOutput = output;
      this.cachedInputs = inputs;

      this.dispatchEvent(new CustomEvent("outputs", { detail: output }));
    } catch (error: any) {
      this.dispatchEvent(
        new CustomEvent("outputs", {
          detail: {
            error: {
              title: "Error",
              message: error.message,
            },
          },
        })
      );
    }
  }