Spaces:
Running
Running
Update ui.py
Browse files
ui.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
from sentiment import analyze_sentiment
|
4 |
|
5 |
-
# 📌
|
6 |
MODEL_OPTIONS = {
|
7 |
"🌎 多語言推特情緒分析 (XLM-RoBERTa)": "cardiffnlp/twitter-xlm-roberta-base-sentiment",
|
8 |
"📖 多語言情緒分析 (BERT)": "nlptown/bert-base-multilingual-uncased-sentiment",
|
@@ -24,20 +24,11 @@ intro_text = """
|
|
24 |
本應用使用 Hugging Face 的 `XLM-RoBERTa`、`BERT`、`DistilBERT` 模型來進行**多語言情緒分析**。
|
25 |
輸入任何語言的文本,AI 會自動判斷其**情緒分類(正向 / 中立 / 負向)**,並提供**信心度(%)**。
|
26 |
|
27 |
-
##
|
28 |
-
✅ **支援多語言**(繁體中文、英文、法文、日文等)
|
29 |
-
✅ **即時分析**,不需下載模型
|
30 |
-
✅ **提供信心度**,結果更透明
|
31 |
-
|
32 |
-
## 📌 **如何使用**
|
33 |
1️⃣ **輸入一句話或一段文本**(可輸入中文、英文、日文等)
|
34 |
-
2️⃣ **選擇 AI
|
35 |
3️⃣ **點擊「分析情緒」**
|
36 |
4️⃣ **查看結果,包括情緒分類 & 信心度**
|
37 |
-
|
38 |
-
## ⚠️ **使用須知**
|
39 |
-
- 目前模型適合**一般文本**,但對**諷刺、幽默**等語句可能不準確。
|
40 |
-
- 若遇到分析錯誤,請**重新輸入文本或稍後再試**。
|
41 |
"""
|
42 |
|
43 |
developer_info = """
|
@@ -58,20 +49,23 @@ def create_ui():
|
|
58 |
model_selector = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="🌎 多語言推特情緒分析 (XLM-RoBERTa)", label="選擇 AI 模型")
|
59 |
|
60 |
analyze_button = gr.Button("分析情緒")
|
61 |
-
progress_bar = gr.Textbox(visible=False, label="模型載入進度")
|
62 |
|
63 |
result_output = gr.Markdown(label="分析結果")
|
64 |
plot_output = gr.Plot(label="信心度")
|
65 |
|
66 |
# 📌 綁定按鈕功能
|
67 |
def process_analysis(text, model_name):
|
68 |
-
progress_bar.update("🔄 AI 模型載入中,請稍後...", visible=True)
|
69 |
model_id = MODEL_OPTIONS[model_name]
|
|
|
|
|
|
|
|
|
70 |
|
71 |
result, confidence_score = analyze_sentiment(text, model_id)
|
72 |
-
|
73 |
plot = plot_confidence(confidence_score)
|
74 |
-
progress_bar.update("", visible=False)
|
75 |
return result, plot
|
76 |
|
77 |
analyze_button.click(process_analysis, inputs=[text_input, model_selector], outputs=[result_output, plot_output, progress_bar])
|
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
from sentiment import analyze_sentiment
|
4 |
|
5 |
+
# 📌 可選擇的模型
|
6 |
MODEL_OPTIONS = {
|
7 |
"🌎 多語言推特情緒分析 (XLM-RoBERTa)": "cardiffnlp/twitter-xlm-roberta-base-sentiment",
|
8 |
"📖 多語言情緒分析 (BERT)": "nlptown/bert-base-multilingual-uncased-sentiment",
|
|
|
24 |
本應用使用 Hugging Face 的 `XLM-RoBERTa`、`BERT`、`DistilBERT` 模型來進行**多語言情緒分析**。
|
25 |
輸入任何語言的文本,AI 會自動判斷其**情緒分類(正向 / 中立 / 負向)**,並提供**信心度(%)**。
|
26 |
|
27 |
+
## 📌 **使用方式**
|
|
|
|
|
|
|
|
|
|
|
28 |
1️⃣ **輸入一句話或一段文本**(可輸入中文、英文、日文等)
|
29 |
+
2️⃣ **選擇 AI 模型(預設為 `XLM-RoBERTa`)**
|
30 |
3️⃣ **點擊「分析情緒」**
|
31 |
4️⃣ **查看結果,包括情緒分類 & 信心度**
|
|
|
|
|
|
|
|
|
32 |
"""
|
33 |
|
34 |
developer_info = """
|
|
|
49 |
model_selector = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="🌎 多語言推特情緒分析 (XLM-RoBERTa)", label="選擇 AI 模型")
|
50 |
|
51 |
analyze_button = gr.Button("分析情緒")
|
52 |
+
progress_bar = gr.Textbox(visible=False, label="模型載入進度")
|
53 |
|
54 |
result_output = gr.Markdown(label="分析結果")
|
55 |
plot_output = gr.Plot(label="信心度")
|
56 |
|
57 |
# 📌 綁定按鈕功能
|
58 |
def process_analysis(text, model_name):
|
|
|
59 |
model_id = MODEL_OPTIONS[model_name]
|
60 |
+
|
61 |
+
# **只有當選擇的模型與目前使用的模型不一致時,才顯示進度條**
|
62 |
+
if model_id != analyze_sentiment.CURRENT_MODEL:
|
63 |
+
progress_bar.update("🔄 AI 模型載入中,請稍後...", visible=True)
|
64 |
|
65 |
result, confidence_score = analyze_sentiment(text, model_id)
|
66 |
+
|
67 |
plot = plot_confidence(confidence_score)
|
68 |
+
progress_bar.update("", visible=False) # 隱藏進度條
|
69 |
return result, plot
|
70 |
|
71 |
analyze_button.click(process_analysis, inputs=[text_input, model_selector], outputs=[result_output, plot_output, progress_bar])
|