dayuian commited on
Commit
602ea6e
·
verified ·
1 Parent(s): b843c90

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +17 -21
ui.py CHANGED
@@ -1,12 +1,6 @@
1
  import gradio as gr
2
  from sentiment import analyze_sentiment
3
-
4
- # 📌 可選擇的模型
5
- MODEL_OPTIONS = {
6
- "🌎 多語言推特情緒分析 (XLM-RoBERTa)": "cardiffnlp/twitter-xlm-roberta-base-sentiment",
7
- "📖 多語言情緒分析 (BERT)": "nlptown/bert-base-multilingual-uncased-sentiment",
8
- "🇬🇧 英語情緒分析 (DistilBERT)": "distilbert-base-uncased-finetuned-sst-2-english"
9
- }
10
 
11
  # 📌 Gradio 介面說明
12
  intro_text = """
@@ -34,37 +28,39 @@ def create_ui():
34
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
35
  gr.Markdown(intro_text)
36
 
37
- text_input = gr.Textbox(lines=3, placeholder="請輸入文本...", label="輸入文本")
38
 
39
  model_selector = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="🌎 多語言推特情緒分析 (XLM-RoBERTa)", label="選擇 AI 模型")
40
 
41
  analyze_button = gr.Button("分析情緒")
 
42
  progress_bar = gr.Textbox(value="", visible=False, label="模型載入進度")
43
 
44
  result_output = gr.Markdown(label="分析結果")
45
 
46
  # 📌 綁定按鈕功能
47
  def process_analysis(text, model_name):
48
- model_id = MODEL_OPTIONS[model_name]
49
-
50
- # **當使用者切換模型時,顯示進度條**
51
- if model_id != analyze_sentiment.CURRENT_MODEL:
52
- progress_bar.update("🔄 AI 模型載入中,請稍後...", visible=True)
53
-
54
- # 📌 調用 API 進行分析
55
- result, _ = analyze_sentiment(text, model_id)
56
-
57
- # **確保進度條消失**
58
- progress_bar.update("", visible=False)
59
  return result
60
 
61
- # 📌 **修正 Gradio 事件綁定,確保所有輸入 & 輸出正確**
62
  analyze_button.click(
63
  fn=process_analysis,
64
  inputs=[text_input, model_selector],
65
- outputs=[result_output, progress_bar]
 
 
 
 
 
 
66
  )
67
 
68
  gr.Markdown(developer_info)
69
 
70
  return iface
 
 
 
 
 
1
  import gradio as gr
2
  from sentiment import analyze_sentiment
3
+ from config import MODEL_OPTIONS
 
 
 
 
 
 
4
 
5
  # 📌 Gradio 介面說明
6
  intro_text = """
 
28
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
29
  gr.Markdown(intro_text)
30
 
31
+ text_input = gr.Textbox(lines=3, placeholder="請輸入文本...", label="輸入文本", max_lines=5)
32
 
33
  model_selector = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="🌎 多語言推特情緒分析 (XLM-RoBERTa)", label="選擇 AI 模型")
34
 
35
  analyze_button = gr.Button("分析情緒")
36
+ clear_button = gr.Button("清除")
37
  progress_bar = gr.Textbox(value="", visible=False, label="模型載入進度")
38
 
39
  result_output = gr.Markdown(label="分析結果")
40
 
41
  # 📌 綁定按鈕功能
42
  def process_analysis(text, model_name):
43
+ progress_bar.update(value="🔄 AI 模型載入中,請稍後...", visible=True)
44
+ result, _ = analyze_sentiment(text, model_name)
45
+ progress_bar.update(value="", visible=False)
 
 
 
 
 
 
 
 
46
  return result
47
 
 
48
  analyze_button.click(
49
  fn=process_analysis,
50
  inputs=[text_input, model_selector],
51
+ outputs=[result_output]
52
+ )
53
+
54
+ clear_button.click(
55
+ lambda: ("", ""),
56
+ inputs=[],
57
+ outputs=[text_input, result_output]
58
  )
59
 
60
  gr.Markdown(developer_info)
61
 
62
  return iface
63
+
64
+ if __name__ == "__main__":
65
+ ui = create_ui()
66
+ ui.launch()