Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
classifier = pipeline("text-classification", model="WJL110/emotion-classifier")
|
| 5 |
+
|
| 6 |
+
label_map = {
|
| 7 |
+
"LABEL_0": "快乐",
|
| 8 |
+
"LABEL_1": "愤怒",
|
| 9 |
+
"LABEL_2": "悲伤"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
def classify_text(text):
|
| 13 |
+
result = classifier(text)[0]
|
| 14 |
+
emotion = label_map.get(result['label'], result['label'])
|
| 15 |
+
# 返回一个字典,Gradio 会自动将其渲染为分类条形图
|
| 16 |
+
return {emotion: result['score']}
|
| 17 |
+
|
| 18 |
+
demo = gr.Interface(
|
| 19 |
+
fn=classify_text,
|
| 20 |
+
inputs=gr.Textbox(label="输入文本"),
|
| 21 |
+
outputs=gr.Label(num_top_classes=1, label="情感预测"),
|
| 22 |
+
examples=[["今天真是太开心了!"], ["这件事让我很生气。"]],
|
| 23 |
+
title="情感分析演示"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
demo.launch()
|