File size: 982 Bytes
2cc4dc9
 
 
1b8dd75
 
2cc4dc9
1b8dd75
 
2cc4dc9
 
1b8dd75
2cc4dc9
1b8dd75
 
 
 
 
 
 
 
2cc4dc9
1b8dd75
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from transformers import pipeline
import gradio as gr

# 載入 Hugging Face 的中文三分類情緒模型
classifier = pipeline("text-classification", model="uer/roberta-base-finetuned-dianping-chinese", tokenizer="uer/roberta-base-finetuned-dianping-chinese")

# 分析函式
def analyze_sentiment(text):
    result = classifier(text)[0]
    label = result["label"]
    score = round(result["score"], 4)

    if label == "0":
        sentiment = "負向情緒"
    elif label == "1":
        sentiment = "中立情緒"
    elif label == "2":
        sentiment = "正向情緒"
    else:
        sentiment = "未知"

    return f"判斷結果:{sentiment}\n信心分數:{score}"

# Gradio 介面
gr.Interface(
    fn=analyze_sentiment,
    inputs=gr.Textbox(lines=4, placeholder="請輸入中文內容"),
    outputs="text",
    title="中文情緒分析系統(正向/中立/負向)",
    description="輸入一段中文,模型會自動判斷情緒傾向"
).launch()