web-transformers
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

web-transformers

Build and test

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.

Package Sidebar

Install

npm i web-transformers

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

41.6 kB

Total Files

27

Last publish

Collaborators

  • praeclarum