web-transformers
Transformer neural networks in the browser.
Usage
import { AutoTokenizer, T5ForConditionalGeneration } from 'web-transformers';
// Load the tokenizer and model
const tokenizer = AutoTokenizer.fromPretrained(modelId, modelsPath);
const model = new T5ForConditionalGeneration(modelId, modelsPath, async progress => {
console.log(`Loading the neural network... ${Math.round(progress * 100)}%`);
});
// Generate text using Seq2Seq
async function generate(inputText: string) {
const generationOptions = {
"maxLength": 50,
"topK": 0,
};
const inputTokenIds = await tokenizer.encode(inputText);
async function generateProgress(outputTokenIds: number[], forInputIds: number[]) {
let shouldContinue = true;
return shouldContinue;
}
const finalOutputTokenIds = await model.generate(inputTokenIds, generationOptions, generateProgress);
const finalOutput = (await tokenizer.decode(finalOutputTokenIds, true)).trim();
console.log(finalOutput);
};
Sample Web App
This repo contains a sample nextjs app that translates text using a transformer neural network.
Before you can run the sample, you need to download the model files. You can do this by running the following command:
cd sample
npm run download_models
Then you can run the sample with:
npm run dev
License
Copyright © 2022 Frank A. Krueger.
This project is MIT licensed.