Simpler, more general Worker
AmitMY opened this issue · 2 comments
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);
Hi, welcome!
Have you taken a look at #437 ?
@jelmervdl is the javascript expert, I defer to him but it does look much simpler.
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.