File size: 1,508 Bytes
7b9c2ab
 
 
 
d2fc8c9
 
7b9c2ab
9a96529
7b9c2ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr 
from transformers import pipeline

from transformers import AutoModelForSequenceClassification,AutoTokenizer,pipeline
model = AutoModelForSequenceClassification.from_pretrained('uer/roberta-base-finetuned-jd-binary-chinese')
tokenizer = AutoTokenizer.from_pretrained('uer/roberta-base-finetuned-jd-binary-chinese')
sentiment_classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
examples=["5月2日,在广东广州,有网友发视频称,自己吃自助烧烤时,发现肉上爬满活蛆,幸好当时没吃,感到特别恶心。","年轻人透过动漫游戏等对传统文化产生深度了解的兴趣,也将反向推动“国潮”创新。"]

def classifier(text):
    pred = sentiment_classifier(text)
    print('pred=',pred)
    pred_out = []
    if pred[0]['label'][0:4] == 'posi':
        dict_nega = { 'label' : '消极',     'score':1 -  pred[0]['score'], }
        dict_posi = {'label':'积极', 'score':pred[0]['score'],}
        pred_out.append(dict_nega)
        pred_out.append(dict_posi)
    else:
        dict_nega = {'label':'消极', 'score':pred[0]['score'],}
        dict_posi = {'label':'积极', 'score':1-pred[0]['score'],}
        pred_out.append(dict_nega)
        pred_out.append(dict_posi)
    return {p["label"]: p["score"] for p in pred_out}

demo = gr.Interface(classifier,
             gr.Textbox(label="Input Text"), 
             gr.Label(label="Predicted Sentiment"),
             examples=examples)

demo.launch()