dayuian commited on
Commit
35158c1
·
verified ·
1 Parent(s): 51639f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -13
app.py CHANGED
@@ -5,9 +5,12 @@ import os
5
  # 設定 Hugging Face API
6
  API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-xlm-roberta-base-sentiment"
7
 
8
- # 從環境變數讀取 API 金鑰(建議這樣做以保護隱私)
9
- HF_API_KEY = os.getenv("analyze_sentiment_key") # 你可以在 .bashrc 或 .zshrc 設定環境變數
10
- HEADERS = {"Authorization": f"Bearer {analyze_sentiment_key}"}
 
 
 
11
 
12
  # 轉換英文分類為中文
13
  def translate_sentiment(label):
@@ -18,9 +21,9 @@ def translate_sentiment(label):
18
  else:
19
  return "😡 **負向**"
20
 
21
- # 轉換信心度為更直觀的等級
22
  def convert_confidence(score):
23
- percentage = round(score * 100) # 轉換為百分比
24
  if score >= 0.90:
25
  return f"🌟 **極高信心** ({percentage}%)"
26
  elif score >= 0.75:
@@ -50,15 +53,57 @@ def analyze_sentiment(text):
50
  except Exception as e:
51
  return f"❌ **錯誤**: {str(e)}"
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # 建立 Gradio 介面
54
- iface = gr.Interface(
55
- fn=analyze_sentiment,
56
- inputs=gr.Textbox(lines=2, placeholder="請輸入文本(支援多語言)..."),
57
- outputs=gr.Markdown(label="分析結果"),
58
- title="多語言情緒分析 AI",
59
- description="請輸入一段話,AI 會分析它的情緒(正向 / 中立 / 負向),並提供信心度。",
60
- theme="compact"
61
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # 啟動 Web 應用
64
  iface.launch()
 
5
  # 設定 Hugging Face API
6
  API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-xlm-roberta-base-sentiment"
7
 
8
+ # 從環境變數讀取 API Key
9
+ HF_API_KEY = os.getenv("HF_API_KEY")
10
+ if HF_API_KEY is None:
11
+ raise ValueError("❌ API Key 未設定,請檢查 Hugging Face Secrets!")
12
+
13
+ HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
14
 
15
  # 轉換英文分類為中文
16
  def translate_sentiment(label):
 
21
  else:
22
  return "😡 **負向**"
23
 
24
+ # 轉換信心度為更直觀的等級(加上百分比)
25
  def convert_confidence(score):
26
+ percentage = round(score * 100)
27
  if score >= 0.90:
28
  return f"🌟 **極高信心** ({percentage}%)"
29
  elif score >= 0.75:
 
53
  except Exception as e:
54
  return f"❌ **錯誤**: {str(e)}"
55
 
56
+ # Gradio 介面說明
57
+ intro_text = """
58
+ # 🎯 多語言情緒分析 AI
59
+ 本應用使用 Hugging Face 的 `XLM-RoBERTa` 模型來進行**多語言情緒分析**。
60
+ 輸入任何語言的文本,AI 會自動判斷其**情緒分類(正向 / 中立 / 負向)**,並提供**信心度(%)**。
61
+
62
+ ## 🔹 **功能特色**
63
+ ✅ **支援多語言**(繁體中文、英文、法文、日文等)
64
+ ✅ **即時分析**,不需下載模型
65
+ ✅ **提供信心度**,結果更透明
66
+
67
+ ## 📌 **如何使用**
68
+ 1️⃣ **輸入一句話或一段文本**(可輸入中文、英文、日文等)
69
+ 2️⃣ **點擊「分析情緒」**
70
+ 3️⃣ **查看結果,包括情緒分類 & 信心度**
71
+
72
+ ## ⚠️ **使用須知**
73
+ - 目前模型適合**一般文本**,但對**諷刺、幽默**等語句可能不準確。
74
+ - 若遇到分析錯誤,請**重新輸入文本或稍後再試**。
75
+ """
76
+
77
+ developer_info = """
78
+ ## 👨‍💻 開發資訊
79
+ - **開發者**: 余彦志 (大宇 / ian)
80
+ - **模型來源**: [Hugging Face](https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment)
81
+ - **技術棧**: `Gradio`、`FastAPI`、`Hugging Face API`
82
+ - **聯絡方式**: [[email protected]]
83
+ """
84
+
85
  # 建立 Gradio 介面
86
+ with gr.Blocks(theme=gr.themes.Soft()) as iface:
87
+ # 介面標題與介紹
88
+ gr.Markdown(intro_text)
89
+
90
+ # 文字輸入框
91
+ with gr.Row():
92
+ text_input = gr.Textbox(
93
+ lines=3, placeholder="請輸入文本(支援多語言)...", label="輸入文本"
94
+ )
95
+
96
+ # 按鈕
97
+ analyze_button = gr.Button("分析情緒")
98
+
99
+ # 結果顯示區
100
+ result_output = gr.Markdown(label="分析結果")
101
+
102
+ # 事件綁定(點擊按鈕後執行分析)
103
+ analyze_button.click(analyze_sentiment, inputs=text_input, outputs=result_output)
104
+
105
+ # 顯示開發資訊
106
+ gr.Markdown(developer_info)
107
 
108
  # 啟動 Web 應用
109
  iface.launch()