william1324 commited on
Commit
1b8dd75
·
verified ·
1 Parent(s): e08498d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -1,18 +1,31 @@
1
  from transformers import pipeline
2
  import gradio as gr
3
 
4
- classifier = pipeline("sentiment-analysis")
 
5
 
6
- def analyze(text):
 
7
  result = classifier(text)[0]
8
  label = result["label"]
9
- score = round(result["score"], 3)
10
- return f"情緒:{label}\n信心分數:{score}"
11
 
12
- demo = gr.Interface(fn=analyze,
13
- inputs=gr.Textbox(label="輸入文字"),
14
- outputs="text",
15
- title="情緒分析器",
16
- description="輸入一段文字,判斷其情緒為 Positive Negative")
 
 
 
17
 
18
- demo.launch()
 
 
 
 
 
 
 
 
 
 
1
  from transformers import pipeline
2
  import gradio as gr
3
 
4
+ # 載入 Hugging Face 的中文三分類情緒模型
5
+ classifier = pipeline("text-classification", model="uer/roberta-base-finetuned-dianping-chinese", tokenizer="uer/roberta-base-finetuned-dianping-chinese")
6
 
7
+ # 分析函式
8
+ def analyze_sentiment(text):
9
  result = classifier(text)[0]
10
  label = result["label"]
11
+ score = round(result["score"], 4)
 
12
 
13
+ if label == "0":
14
+ sentiment = "負向情緒"
15
+ elif label == "1":
16
+ sentiment = "中立情緒"
17
+ elif label == "2":
18
+ sentiment = "正向情緒"
19
+ else:
20
+ sentiment = "未知"
21
 
22
+ return f"判斷結果:{sentiment}\n信心分數:{score}"
23
+
24
+ # Gradio 介面
25
+ gr.Interface(
26
+ fn=analyze_sentiment,
27
+ inputs=gr.Textbox(lines=4, placeholder="請輸入中文內容"),
28
+ outputs="text",
29
+ title="中文情緒分析系統(正向/中立/負向)",
30
+ description="輸入一段中文,模型會自動判斷情緒傾向"
31
+ ).launch()