Spaces:
Running
Running
Update ui.py
Browse files
ui.py
CHANGED
@@ -2,59 +2,34 @@ 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",
|
9 |
-
"英語情緒分析 (DistilBERT)": "distilbert-base-uncased-finetuned-sst-2-english"
|
10 |
-
}
|
11 |
-
|
12 |
-
# 📌 生成信心度視覺化圖表
|
13 |
def plot_confidence(score):
|
14 |
-
fig, ax = plt.subplots()
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
ax.bar(categories, levels, color=["red", "orange", "yellow", "green"])
|
21 |
-
ax.set_ylim([0, 1])
|
22 |
-
ax.set_title(f"信心度分析 ({confidence_category})")
|
23 |
return fig
|
24 |
|
25 |
-
# 📌 產生可下載的分析報告
|
26 |
-
def generate_report(text, result, model_name):
|
27 |
-
report = f"🔍 **分析報告**\n\n📝 **輸入內容**: {text}\n\n📊 **分析結果**:\n{result}\n\n🤖 **使用模型**: {model_name}"
|
28 |
-
return report
|
29 |
-
|
30 |
# 📌 建立 Gradio 介面
|
31 |
def create_ui():
|
32 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
33 |
-
gr.Markdown("# 🎯 多語言情緒分析 AI
|
|
|
34 |
|
35 |
-
|
36 |
-
text_input = gr.Textbox(lines=3, placeholder="請輸入文本(支援多語言)...", label="輸入文本")
|
37 |
-
|
38 |
-
with gr.Row():
|
39 |
-
model_selector = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="多語言推特情緒分析 (XLM-RoBERTa)", label="選擇 AI 模型")
|
40 |
|
41 |
analyze_button = gr.Button("分析情緒")
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
plot_output = gr.Plot(label="信心度圖表")
|
46 |
-
|
47 |
-
report_output = gr.File(label="下載報告")
|
48 |
|
49 |
# 📌 綁定按鈕功能
|
50 |
-
def process_analysis(text
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
plot = plot_confidence(confidence_score)
|
55 |
-
report = generate_report(text, result, model_name)
|
56 |
-
return result, plot, report
|
57 |
|
58 |
-
analyze_button.click(process_analysis, inputs=
|
59 |
|
60 |
return iface
|
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
from sentiment import analyze_sentiment
|
4 |
|
5 |
+
# 📌 生成簡單的信心度條狀圖
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def plot_confidence(score):
|
7 |
+
fig, ax = plt.subplots(figsize=(4, 1)) # 調整圖表尺寸
|
8 |
+
ax.barh(["信心度"], [score], color="blue") # 橫向條狀圖
|
9 |
+
ax.set_xlim([0, 1]) # 設定範圍為 0-1
|
10 |
+
ax.set_xticks([0, 0.25, 0.5, 0.75, 1]) # 設定刻度
|
11 |
+
ax.set_xlabel("信心度(百分比)") # 標題
|
|
|
|
|
|
|
|
|
12 |
return fig
|
13 |
|
|
|
|
|
|
|
|
|
|
|
14 |
# 📌 建立 Gradio 介面
|
15 |
def create_ui():
|
16 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
17 |
+
gr.Markdown("# 🎯 多語言情緒分析 AI")
|
18 |
+
gr.Markdown("請輸入一段文字,AI 會分析其情緒,並提供信心度。")
|
19 |
|
20 |
+
text_input = gr.Textbox(lines=3, placeholder="請輸入文本...", label="輸入文本")
|
|
|
|
|
|
|
|
|
21 |
|
22 |
analyze_button = gr.Button("分析情緒")
|
23 |
|
24 |
+
result_output = gr.Markdown(label="分析結果")
|
25 |
+
plot_output = gr.Plot(label="信心度")
|
|
|
|
|
|
|
26 |
|
27 |
# 📌 綁定按鈕功能
|
28 |
+
def process_analysis(text):
|
29 |
+
result, confidence_score = analyze_sentiment(text) # 調用 API 進行分析
|
30 |
+
plot = plot_confidence(confidence_score) # 生成條狀圖
|
31 |
+
return result, plot
|
|
|
|
|
|
|
32 |
|
33 |
+
analyze_button.click(process_analysis, inputs=text_input, outputs=[result_output, plot_output])
|
34 |
|
35 |
return iface
|