Skip to content

Commit f2a6974

Browse files
committed
add worker for the model
1 parent e7e8f70 commit f2a6974

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

vue/src/worker.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { pipeline, TextStreamer } from '@huggingface/transformers'
2+
3+
class MySummarizationPipeline {
4+
static task = 'summarization'
5+
static model = 'Xenova/distilbart-cnn-6-6'
6+
static instance = null
7+
8+
static async getInstance(progress_callback = null) {
9+
this.instance ??= pipeline(this.task, this.model, {
10+
progress_callback,
11+
dtype: 'q8',
12+
})
13+
return this.instance
14+
}
15+
}
16+
17+
self.addEventListener('message', async (event) => {
18+
const summarizer = await MySummarizationPipeline.getInstance((progressData) => {
19+
self.postMessage(progressData)
20+
})
21+
22+
const streamer = new TextStreamer(summarizer.tokenizer, {
23+
skip_prompt: true,
24+
skip_special_tokens: true,
25+
callback_function: function (text) {
26+
self.postMessage({
27+
status: 'update',
28+
output: text,
29+
})
30+
},
31+
})
32+
33+
const output = await summarizer(event.data.text, {
34+
max_length: 100,
35+
min_length: 10,
36+
37+
streamer,
38+
})
39+
40+
self.postMessage({
41+
status: 'complete',
42+
output,
43+
})
44+
})

0 commit comments

Comments
 (0)