dayuian commited on
Commit
ad81ace
·
verified ·
1 Parent(s): a3452de

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +50 -34
ui.py CHANGED
@@ -1,44 +1,60 @@
1
  import gradio as gr
 
2
  from sentiment import analyze_sentiment
3
 
4
- # Gradio 介面說明
5
- intro_text = """
6
- # 🎯 多語言情緒分析 AI
7
- 本應用使用 Hugging Face 的 `XLM-RoBERTa` 模型來進行**多語言情緒分析**。
8
- 輸入任何語言的文本,AI 會自動判斷其**情緒分類(正向 / 中立 / 負向)**,並提供 AI 判斷的**信心度(%)**。
9
-
10
- ## 🔹 **功能特色**
11
- **支援多語言**(繁體中文、英文、法文、日文等)
12
- **即時分析**,不需下載模型
13
- **提供信心度**,結果更透明
14
-
15
- ## 📌 **如何使用**
16
- 1️⃣ **輸入一句話或一段文本**(可輸入中文、英文、日文等)
17
- 2️⃣ **點擊「分析情緒」**
18
- 3️⃣ **查看結果,包括情緒分類 & 信心度**
19
-
20
- ## ⚠️ **使用須知**
21
- - 目前模型適合**一般文本**,但對**諷刺、幽默**等語句可能不準確。
22
- - 若遇到分析錯誤,請**重新輸入文本或稍後再試**。
23
- """
24
-
25
- developer_info = """
26
- ## 👨‍💻 開發資訊
27
- - **開發者**: 余彦志 (大宇 / ian)
28
- - **模型來源**: [Hugging Face](https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment)
29
- - **技術棧**: `Gradio`、`FastAPI`、`Hugging Face API`
30
- - **聯絡方式**: [[email protected]]
31
- """
32
-
33
- # 建立 Gradio 介面
34
  def create_ui():
35
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
36
- gr.Markdown(intro_text)
 
37
  with gr.Row():
38
  text_input = gr.Textbox(lines=3, placeholder="請輸入文本(支援多語言)...", label="輸入文本")
 
 
 
 
39
  analyze_button = gr.Button("分析情緒")
40
- result_output = gr.Markdown(label="分析結果")
41
- analyze_button.click(analyze_sentiment, inputs=text_input, outputs=result_output)
42
- gr.Markdown(developer_info)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  return iface
 
1
  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
+ categories = ["低", "中等", "高", "極高"]
16
+ levels = [0.3, 0.5, 0.75, 0.9]
17
+
18
+ confidence_category = next((cat for cat, lvl in zip(categories, levels) if score < lvl), "極高")
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\n請輸入一段文字,選擇模型,AI 會分析其情緒。")
34
+
35
  with gr.Row():
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
+ with gr.Row():
44
+ result_output = gr.Markdown(label="分析結果")
45
+ plot_output = gr.Plot(label="信心度圖表")
46
+
47
+ report_output = gr.File(label="下載報告")
48
+
49
+ # 📌 綁定按鈕功能
50
+ def process_analysis(text, model_name):
51
+ model_id = MODEL_OPTIONS[model_name] # 轉換中文名稱為模型 ID
52
+ result = analyze_sentiment(text, model_id) # 調用 API 進行分析
53
+ confidence_score = float(result.split("信心度為**: ")[1].split("%")[0]) / 100
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=[text_input, model_selector], outputs=[result_output, plot_output, report_output])
59
 
60
  return iface