File size: 2,495 Bytes
271d7b5
 
 
 
 
 
 
 
 
 
 
 
92e81aa
271d7b5
 
 
 
 
 
 
 
 
 
 
 
a81d482
 
271d7b5
 
 
 
 
 
 
 
 
 
 
052a6c5
 
a81d482
1771dba
9ae14c1
271d7b5
 
 
 
848d0f5
271d7b5
 
cd99f6b
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
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
# file: deploy_demo.py
# time: 2021/10/10
# author: yangheng <[email protected]>
# github: https://github.com/yangheng95
# Copyright (C) 2021. All Rights Reserved.

import gradio as gr
import pandas as pd

from pyabsa import APCCheckpointManager

sentiment_classifier = APCCheckpointManager.get_sentiment_classifier(checkpoint='multilingual2',
                                                                     auto_device=True  # False means load model on CPU
                                                                     )


def inference(text):
    result = sentiment_classifier.infer(text=text,
                                        print_result=True,
                                        clear_input_samples=True)

    result = pd.DataFrame({
        'aspect': result[0]['aspect'],
        'sentiment': result[0]['sentiment'],
        'confidence': [round(c, 3) for c in result[0]['confidence']],
        'ref_sentiment': ['' if ref == '-999' else ref for ref in result[0]['ref_sentiment']],
        'is_correct': result[0]['ref_check'],
    })

    return result


if __name__ == '__main__':
    iface = gr.Interface(
        fn=inference,
        inputs=["text"],
        examples=[
            ['前面老师[ASP]讲课[ASP]很好,后面的[ASP]分享课[ASP]过快了,显得很紧张,听不太清,一点都没有分享的意境,流水帐似的一带而过的味道。希望[ASP]分享课[ASP]改进一下,谢谢大家'],
            ['听了老师的[ASP]讲解[ASP]受益匪浅,老师的[ASP]讲解形式[ASP]唯美听之让人陶醉真正做到了寓教于乐。我喜欢这种[ASP]授课方式[ASP]'],
            ['I have had my [ASP]computer[ASP] for 2 weeks already and it [ASP]works[ASP] perfectly . !sent!  Positive, Positive'],
            ['Strong build though which really adds to its [ASP]durability[ASP] .'],  # !sent! Positive
            ['Use [ASP] aspect [ASP] to wrap target aspects. And you can use "!sent!" to tell the model the true sentiment'],
            ['This demo is trained on the laptop and restaurant and other review datasets from [ASP]ABSADatasets[ASP] (https://github.com/yangheng95/ABSADatasets)'],
            ['To fit on your data, please train the model on your own data, see the [ASP]PyABSA[ASP] (https://github.com/yangheng95/PyABSA)'],
        ],
        outputs="dataframe",
        title='Multilingual Aspect Sentiment Classification for Short Texts (powered by PyABSA)'
    )

    iface.launch()