Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
from transformers import AutoModelForSequenceClassification,AutoTokenizer,pipeline
|
| 5 |
+
model = AutoModelForSequenceClassification.from_pretrained('uer/roberta-base-finetuned-jd-binary-chinese',local_files_only=True)
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained('uer/roberta-base-finetuned-jd-binary-chinese',local_files_only=True)
|
| 7 |
+
sentiment_classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
|
| 8 |
+
examples=["小红正在吃一块美味的蛋糕。","小红在蛋糕里发现了一只苍蝇。"]
|
| 9 |
+
|
| 10 |
+
def classifier(text):
|
| 11 |
+
pred = sentiment_classifier(text)
|
| 12 |
+
print('pred=',pred)
|
| 13 |
+
pred_out = []
|
| 14 |
+
if pred[0]['label'][0:4] == 'posi':
|
| 15 |
+
dict_nega = { 'label' : '消极', 'score':1 - pred[0]['score'], }
|
| 16 |
+
dict_posi = {'label':'积极', 'score':pred[0]['score'],}
|
| 17 |
+
pred_out.append(dict_nega)
|
| 18 |
+
pred_out.append(dict_posi)
|
| 19 |
+
else:
|
| 20 |
+
dict_nega = {'label':'消极', 'score':pred[0]['score'],}
|
| 21 |
+
dict_posi = {'label':'积极', 'score':1-pred[0]['score'],}
|
| 22 |
+
pred_out.append(dict_nega)
|
| 23 |
+
pred_out.append(dict_posi)
|
| 24 |
+
return {p["label"]: p["score"] for p in pred_out}
|
| 25 |
+
|
| 26 |
+
demo = gr.Interface(classifier,
|
| 27 |
+
gr.Textbox(label="Input Text"),
|
| 28 |
+
gr.Label(label="Predicted Sentiment"),
|
| 29 |
+
examples=examples)
|
| 30 |
+
|
| 31 |
+
demo.launch()
|