Coder Social home page Coder Social logo

web-transformers's Introduction

web-transformers

Build and Test

This library enables you to run huggingface transformer models directly in the browser. It accomplishes this by running the models using the ONNX Runtime JavaScript API and by implementing its own JavaScript-only tokenization library.

At the moment, it is compatible with Google's T5 models, but it was designed to be expanded. I hope to support GPT2, Roberta, and InCoder in the future.

Usage

The following example code shows how to load a tokenizer and a transformer model. It then uses those objects to create a generate functions which generates output text from input text.

import { AutoTokenizer, T5ForConditionalGeneration } from 'web-transformers';

// Load the tokenizer and model
const tokenizer = AutoTokenizer.fromPretrained(modelId, modelsPath);
const model = new T5ForConditionalGeneration(modelId, modelsPath);

// Generate text using Seq2Seq
async function generate(inputText: string) {
    // Tokenize the text
    const inputTokenIds = await tokenizer.encode(inputText);
    // Generate output
    const generationOptions = {
        "maxLength": 50,
        "topK": 10,
    };
    const outputTokenIds = await model.generate(inputTokenIds, generationOptions);
    // Convert output tokens into text
    const outputText = await tokenizer.decode(finalOutputTokenIds, true);
    return outputText;
};

// Translate
console.log(await generate("translate English to French: Hello World!"));

Sample Web App

This repo contains a sample nextjs app that translates text using a transformer neural network.

First build the library:

git clone [email protected]:praeclarum/web-transformers.git
cd web-transformers
npm i

Now, build the sample:

cd sample
npm i

Before running the sample, you need to download the model files that will be executed. You can do this by running the following command:

npm run download_models

Then you can run the sample with:

npm run dev

Models

Currently only the T5 network is supported.

Sampling

The neural network outputs the logarithm of the probability of each token. In order to get a token, a probabilistic sample has to be taken. The following algorithms are implemented:

  • Greedy: Take the token with the highest probability.
  • Top-k: Take the top-k tokens with the highest probability.

Sponsorship

This library was made possible thanks to the financial support of Reflect. Thanks Reflect!

License

Copyright © 2022 Frank A. Krueger. This project is MIT licensed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.