Skip to content

Using transformers.js

Hugging Face Transformer.js let’s you run local model directly from your browser or a Node.JS process. We can freely use this library from a GenAIScript.

Installing transformer.js

You will need to install the package locally to reference it in your script.

Terminal window
npm install -D @xenova/transformers

Importing transformers

The summarization pipeline specializes in summarizing text. You typically want to allocate a pipeline once and reuse it over the lifetime of the script.

import { pipeline } from "@xenova/transformers"
const summarizer = await pipeline("summarization")

Using transformers

The summarizer object is a locally running model that we can use to summarize each file.

for (const file of env.files) {
const [summary] = await summarizer(file.content)
// @ts-ignore
def("FILE", { filename: file.filename, content: summary.summary_text })
}

To be continued…

There’s a ton of other transformers that you could also use in your scripts. Happy modeling.