|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("正在下載並載入模型,請稍候...") |
|
|
classifier = pipeline("sentiment-analysis", model="uer/roberta-base-finetuned-dianping-chinese") |
|
|
|
|
|
|
|
|
def analyze_text(text): |
|
|
|
|
|
|
|
|
results = classifier(text) |
|
|
label = results[0]['label'] |
|
|
score = results[0]['score'] |
|
|
|
|
|
|
|
|
chinese_label = "正面/滿意 😊" if "positive" in label else "負面/不滿 😡" |
|
|
|
|
|
|
|
|
return {chinese_label: score} |
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=analyze_text, |
|
|
inputs=gr.Textbox(lines=3, placeholder="請輸入一段中文文字(例如:這本書寫得真好!)..."), |
|
|
outputs=gr.Label(num_top_classes=1, label="分析結果"), |
|
|
title="Hugging Face 中文情感分析器", |
|
|
description="輸入一段文字,AI 會自動判斷其情緒傾向。(基於 BERT/RoBERTa 模型)", |
|
|
examples=[ |
|
|
["這本書的內容深入淺出,對我學習 NLP 幫助很大!"], |
|
|
["運送速度太慢了,書角還有點摺痕,失望。"], |
|
|
["地震預警系統的反應速度對於防災至關重要。"] |
|
|
] |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|