1234 / app.py
william1324's picture
Update app.py
1b8dd75 verified
raw
history blame
982 Bytes
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()