async runWithInputs()

in src/nodes/client/translation.ts [41:95]


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

    const _modelid = modelid?.trim();
    if (!text) {
      // No input node
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: { results: null } })
      );
      return;
    }
    if (this.cachedResult && compareObjects(this.cachedInput, inputs)) {
      this.dispatchEvent(
        new CustomEvent("outputs", {
          detail: this.cachedResult,
        })
      );
      return;
    }
    const translator: TranslationPipeline = await this.getInstance(
      _modelid,
      quantized,
      device
    );

    // TODO: Live updates to UI, then dispatch final result at end
    const result = await translator(text, {
      // @ts-ignore
      src_lang: source_language,
      tgt_lang: target_language,
    });
    const resultSingle = (
      Array.isArray(result) ? result[0] : result
    ) as TranslationSingle;

    // Output.
    this.cachedInput = inputs;
    this.cachedResult = {
      results: resultSingle.translation_text,
      text: text,
    };

    this.dispatchEvent(
      new CustomEvent("outputs", {
        detail: this.cachedResult,
      })
    );
  }