dayuian commited on
Commit
0aaced8
·
verified ·
1 Parent(s): 897683b

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +20 -5
ui.py CHANGED
@@ -2,6 +2,13 @@ import gradio as gr
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)) # 調整圖表尺寸
@@ -15,21 +22,29 @@ def plot_confidence(score):
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
 
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(figsize=(4, 1)) # 調整圖表尺寸
 
22
  def create_ui():
23
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
24
  gr.Markdown("# 🎯 多語言情緒分析 AI")
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] # 轉換中文名稱為模型 ID
41
+
42
+ result, confidence_score = analyze_sentiment(text, model_id) # 調用 API 進行分析
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