Spaces:
Runtime error
Runtime error
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() | |