Skip to content

Simpler, more general Worker #439

@AmitMY

Description

@AmitMY

Running the test_page example in this repository left me wowed, but also with a little bad taste, as for the complexity of how much code one needs to write/copy in order to use it.
Additionally, all paths in that example are relative, requiring every user to adjust the worker code to their directory structure.

I replicated that example fully here - https://github.com/sign/browsermt/tree/main/example/src
And wrote an npm module to work with bergamot models

From your app, usage becomes extremely simple:

import {createBergamotWorker} from '@sign-mt/browsermt';
// OR import {createBergamotWorker} from 'https://unpkg.com/@sign-mt/[email protected]/build/bundled/index.js'

const worker = createBergamotWorker('/node_modules/@sign-mt/browsermt/build/esm/worker.js');
// OR createBergamotWorker('https://unpkg.com/@sign-mt/[email protected]/build/bundled/worker.js')


// Copy these artifacts to your deployed folder
await worker.importBergamotWorker(
  'browsermt/bergamot-translator-worker.js',
  'browsermt/bergamot-translator-worker.wasm',
);

// Create object with URLs to the mode files
const modelRegistry = {
  enru: {
    model: {name: "/models/enru/model.enru.intgemm.alphas.bin"},
    lex: {name: "/models/enru/lex.50.50.enru.s2t.bin"},
    vocab: {name: "/models/enru/vocab.enru.spm"}
  }
};

await worker.loadModel('en', 'ru', modelRegistry);

const translations = await worker.translate('en', 'ru', ['test sentence', 'other sentence'], {isHtml: false});
console.log(translations);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions