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=["小红正在吃一块美味的蛋糕。","小红在蛋糕里发现了一只苍蝇。"] 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()