Spaces:
Sleeping
Sleeping
Update ui.py
Browse files
ui.py
CHANGED
@@ -9,42 +9,73 @@ MODEL_OPTIONS = {
|
|
9 |
"🇬🇧 英語情緒分析 (DistilBERT)": "distilbert-base-uncased-finetuned-sst-2-english"
|
10 |
}
|
11 |
|
12 |
-
# 📌
|
13 |
def plot_confidence(score):
|
14 |
-
fig, ax = plt.subplots(figsize=(4, 1))
|
15 |
-
ax.barh(["信心度"], [score], color="blue")
|
16 |
-
ax.set_xlim([0, 1])
|
17 |
-
ax.set_xticks([0, 0.25, 0.5, 0.75, 1])
|
18 |
-
ax.set_xlabel("信心度(百分比)")
|
19 |
return fig
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# 📌 建立 Gradio 介面
|
22 |
def create_ui():
|
23 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
24 |
-
gr.Markdown(
|
25 |
-
gr.Markdown("請輸入一段文字,選擇 AI 模型,AI 會分析其情緒,並提供信心度。")
|
26 |
|
27 |
text_input = gr.Textbox(lines=3, placeholder="請輸入文本...", label="輸入文本")
|
28 |
|
29 |
model_selector = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="🌎 多語言推特情緒分析 (XLM-RoBERTa)", label="選擇 AI 模型")
|
30 |
|
31 |
analyze_button = gr.Button("分析情緒")
|
32 |
-
progress_bar = gr.Textbox(visible=False) # 進度條(文字顯示)
|
33 |
|
34 |
result_output = gr.Markdown(label="分析結果")
|
35 |
plot_output = gr.Plot(label="信心度")
|
36 |
|
37 |
# 📌 綁定按鈕功能
|
38 |
def process_analysis(text, model_name):
|
39 |
-
progress_bar.update("🔄 AI 模型載入中,請稍後...", visible=True)
|
40 |
-
model_id = MODEL_OPTIONS[model_name]
|
41 |
|
42 |
-
result, confidence_score = analyze_sentiment(text, model_id)
|
43 |
|
44 |
-
plot = plot_confidence(confidence_score)
|
45 |
-
progress_bar.update("", visible=False)
|
46 |
return result, plot
|
47 |
|
48 |
analyze_button.click(process_analysis, inputs=[text_input, model_selector], outputs=[result_output, plot_output, progress_bar])
|
49 |
|
|
|
|
|
50 |
return iface
|
|
|
9 |
"🇬🇧 英語情緒分析 (DistilBERT)": "distilbert-base-uncased-finetuned-sst-2-english"
|
10 |
}
|
11 |
|
12 |
+
# 📌 生成信心度條狀圖
|
13 |
def plot_confidence(score):
|
14 |
+
fig, ax = plt.subplots(figsize=(4, 1))
|
15 |
+
ax.barh(["信心度"], [score], color="blue")
|
16 |
+
ax.set_xlim([0, 1])
|
17 |
+
ax.set_xticks([0, 0.25, 0.5, 0.75, 1])
|
18 |
+
ax.set_xlabel("信心度(百分比)")
|
19 |
return fig
|
20 |
|
21 |
+
# 📌 Gradio 介面說明
|
22 |
+
intro_text = """
|
23 |
+
# 🎯 多語言情緒分析 AI
|
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 = """
|
44 |
+
## 👨💻 開發資訊
|
45 |
+
- **開發者**: 余彦志 (大宇 / ian)
|
46 |
+
- **模型來源**: [Hugging Face](https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment)
|
47 |
+
- **技術棧**: `Gradio`、`FastAPI`、`Hugging Face API`
|
48 |
+
- **聯絡方式**: [[email protected]]
|
49 |
+
"""
|
50 |
+
|
51 |
# 📌 建立 Gradio 介面
|
52 |
def create_ui():
|
53 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
54 |
+
gr.Markdown(intro_text)
|
|
|
55 |
|
56 |
text_input = gr.Textbox(lines=3, placeholder="請輸入文本...", label="輸入文本")
|
57 |
|
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])
|
78 |
|
79 |
+
gr.Markdown(developer_info)
|
80 |
+
|
81 |
return iface
|