-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.worker.js
40 lines (33 loc) · 1.07 KB
/
translate.worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { pipeline } from '@xenova/transformers';
class MyTranslationPipeline {
static task = 'translation';
static model = 'Xenova/nllb-200-distilled-600M';
static instance = null;
static async getInstance(progress_callback = null) {
if (this.instance === null) {
this.instance = pipeline(this.task, this.model, { progress_callback });
}
return this.instance;
}
}
self.addEventListener('message', async (event) => {
let translator = await MyTranslationPipeline.getInstance(x => {
self.postMessage(x)
})
console.log(event.data)
let output = await translator(event.data.text, {
tgt_lang: event.data.tgt_lang,
src_lang: event.data.src_lang,
callback_function: x => {
self.postMessage({
status: 'update',
output: translator.tokenizer.decode(x[0].output_token_ids, { skip_special_tokens: true })
})
}
})
console.log('HEHEHHERERE', output)
self.postMessage({
status: 'complete',
output
})
})