File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments