browsermt / bergamot-translator

Cross platform C++ library focusing on optimized machine translation on the consumer-grade device.
http://browser.mt
Mozilla Public License 2.0
341 stars 38 forks source link

Simpler, more general Worker #439

Open AmitMY opened 2 years ago

AmitMY commented 2 years ago

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/browsermt@0.0.2/build/bundled/index.js'

const worker = createBergamotWorker('/node_modules/@sign-mt/browsermt/build/esm/worker.js');
// OR createBergamotWorker('https://unpkg.com/@sign-mt/browsermt@0.0.2/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);
kpu commented 2 years ago

Hi, welcome!

Have you taken a look at https://github.com/browsermt/bergamot-translator/pull/437 ?

@jelmervdl is the javascript expert, I defer to him but it does look much simpler.

AmitMY commented 2 years ago

Thanks @kpu I was not aware of that PR, would have saved me some time :)

My code has multiple advantages, namely:

And disadvantages:

I probably did not put as much thought into this as @jelmervdi did, and thus may have not included some extra features / some additional code cleaning, as my goal was having something published, and usable right away.