49
49
senta = Taskflow("sentiment_analysis")
50
50
senta("怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片")
51
51
'''
52
- [{'text': '怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片', 'label': 'negative'}]
52
+ [{'text': '怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片', 'label': 'negative', 'score': 0.6691398620605469 }]
53
53
'''
54
54
55
55
senta(["怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片",
56
56
"作为老的四星酒店,房间依然很整洁,相当不错。机场接机服务很好,可以在车上办理入住手续,节省时间"])
57
57
'''
58
- [{'text': '怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片', 'label': 'negative'},
59
- {'text': '作为老的四星酒店,房间依然很整洁,相当不错。机场接机服务很好,可以在车上办理入住手续,节省时间', 'label': 'positive'}
58
+ [{'text': '怀着十分激动的心情放映,可是看着看着发现,在放映完毕后,出现一集米老鼠的动画片', 'label': 'negative', 'score': 0.6691398620605469 },
59
+ {'text': '作为老的四星酒店,房间依然很整洁,相当不错。机场接机服务很好,可以在车上办理入住手续,节省时间', 'label': 'positive', 'score': 0.9857505559921265 }
60
60
]
61
61
'''
62
62
63
63
senta = Taskflow("sentiment_analysis", model="skep_ernie_1.0_large_ch")
64
64
senta("作为老的四星酒店,房间依然很整洁,相当不错。机场接机服务很好,可以在车上办理入住手续,节省时间。")
65
65
'''
66
- [{'text': '作为老的四星酒店,房间依然很整洁,相当不错。机场接机服务很好,可以在车上办理入住手续,节省时间。', 'label': 'positive'}]
66
+ [{'text': '作为老的四星酒店,房间依然很整洁,相当不错。机场接机服务很好,可以在车上办理入住手续,节省时间。', 'label': 'positive', 'score': 0.984320878982544 }]
67
67
'''
68
68
"""
69
69
@@ -115,7 +115,7 @@ def _construct_model(self, model):
115
115
padding_idx = pad_token_id ,
116
116
pooling_type = 'max' )
117
117
model_path = download_file (self ._task_path , model + ".pdparams" ,
118
- URLS [model ][0 ], URLS [model ][1 ], model )
118
+ URLS [model ][0 ], URLS [model ][1 ])
119
119
120
120
# Load the model parameter for the predict
121
121
state_dict = paddle .load (model_path )
@@ -126,7 +126,7 @@ def _construct_tokenizer(self, model):
126
126
"""
127
127
Construct the tokenizer for the predictor.
128
128
"""
129
- full_name = download_file (self .model , "senta_word_dict.txt" ,
129
+ full_name = download_file (self ._task_path , "senta_word_dict.txt" ,
130
130
URLS ['bilstm_vocab' ][0 ],
131
131
URLS ['bilstm_vocab' ][1 ])
132
132
vocab = Vocab .load_vocabulary (
@@ -181,28 +181,34 @@ def _run_model(self, inputs):
181
181
Run the task model from the outputs of the `_tokenize` function.
182
182
"""
183
183
results = []
184
+ scores = []
184
185
with static_mode_guard ():
185
186
for batch in inputs ['data_loader' ]:
186
187
ids , lens = self .batchify_fn (batch )
187
188
self .input_handles [0 ].copy_from_cpu (ids )
188
189
self .input_handles [1 ].copy_from_cpu (lens )
189
190
self .predictor .run ()
190
191
idx = self .output_handle [0 ].copy_to_cpu ().tolist ()
192
+ probs = self .output_handle [1 ].copy_to_cpu ().tolist ()
191
193
labels = [self ._label_map [i ] for i in idx ]
194
+ score = [max (prob ) for prob in probs ]
192
195
results .extend (labels )
196
+ scores .extend (score )
193
197
194
198
inputs ['result' ] = results
199
+ inputs ['score' ] = scores
195
200
return inputs
196
201
197
202
def _postprocess (self , inputs ):
198
203
"""
199
204
This function will convert the model output to raw text.
200
205
"""
201
206
final_results = []
202
- for text , label in zip (inputs ['text' ], inputs ['result' ]):
207
+ for text , label , score in zip (inputs ['text' ], inputs ['result' ], inputs [ 'score ' ]):
203
208
result = {}
204
209
result ['text' ] = text
205
210
result ['label' ] = label
211
+ result ['score' ] = score
206
212
final_results .append (result )
207
213
return final_results
208
214
@@ -302,27 +308,33 @@ def _run_model(self, inputs):
302
308
Run the task model from the outputs of the `_tokenize` function.
303
309
"""
304
310
results = []
311
+ scores = []
305
312
with static_mode_guard ():
306
313
for batch in inputs ['data_loader' ]:
307
314
ids , segment_ids = self ._batchify_fn (batch )
308
315
self .input_handles [0 ].copy_from_cpu (ids )
309
316
self .input_handles [1 ].copy_from_cpu (segment_ids )
310
317
self .predictor .run ()
311
318
idx = self .output_handle [0 ].copy_to_cpu ().tolist ()
319
+ probs = self .output_handle [1 ].copy_to_cpu ().tolist ()
312
320
labels = [self ._label_map [i ] for i in idx ]
321
+ score = [max (prob ) for prob in probs ]
313
322
results .extend (labels )
323
+ scores .extend (score )
314
324
315
325
inputs ['result' ] = results
326
+ inputs ['score' ] = scores
316
327
return inputs
317
328
318
329
def _postprocess (self , inputs ):
319
330
"""
320
331
The model output is tag ids, this function will convert the model output to raw text.
321
332
"""
322
333
final_results = []
323
- for text , label in zip (inputs ['text' ], inputs ['result' ]):
334
+ for text , label , score in zip (inputs ['text' ], inputs ['result' ], inputs [ 'score ' ]):
324
335
result = {}
325
336
result ['text' ] = text
326
337
result ['label' ] = label
338
+ result ['score' ] = score
327
339
final_results .append (result )
328
340
return final_results
0 commit comments