Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler, more general Worker #439

Open
AmitMY opened this issue Aug 11, 2022 · 2 comments
Open

Simpler, more general Worker #439

AmitMY opened this issue Aug 11, 2022 · 2 comments

Comments

@AmitMY
Copy link

AmitMY commented Aug 11, 2022

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);
@kpu
Copy link
Member

kpu commented Aug 11, 2022

Hi, welcome!

Have you taken a look at #437 ?

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

@AmitMY
Copy link
Author

AmitMY commented Aug 12, 2022

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

My code has multiple advantages, namely:

  • control about every file's location (workers, models, etc)
  • types support (written in typescript, and heavily typed)
  • minimally changes your implementation in this repository
  • supports worker threads in node.js (now tested)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants