Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,109 +1,6 @@
|
|
1 |
-
|
2 |
-
import requests
|
3 |
-
import os
|
4 |
-
|
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):
|
17 |
-
if "positive" in label.lower():
|
18 |
-
return "😃 **正向**"
|
19 |
-
elif "neutral" in label.lower():
|
20 |
-
return "😐 **中立**"
|
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:
|
30 |
-
return f"✅ **高信心** ({percentage}%)"
|
31 |
-
elif score >= 0.50:
|
32 |
-
return f"⚠️ **中等信心** ({percentage}%)"
|
33 |
-
elif score >= 0.30:
|
34 |
-
return f"❓ **低信心** ({percentage}%)"
|
35 |
-
else:
|
36 |
-
return f"❌ **極低信心(建議忽略)** ({percentage}%)"
|
37 |
-
|
38 |
-
# 調用 Hugging Face API 進行情緒分析
|
39 |
-
def analyze_sentiment(text):
|
40 |
-
try:
|
41 |
-
response = requests.post(API_URL, headers=HEADERS, json={"inputs": text})
|
42 |
-
result = response.json()
|
43 |
-
|
44 |
-
if isinstance(result, list) and len(result) > 0:
|
45 |
-
sentiment = translate_sentiment(result[0]["label"]) # 翻譯情緒
|
46 |
-
confidence = result[0]["score"]
|
47 |
-
confidence_label = convert_confidence(confidence) # 轉換信心度
|
48 |
-
|
49 |
-
return f"**情緒分類**: {sentiment}\n**AI判斷的信心度為**: {confidence_label}"
|
50 |
-
else:
|
51 |
-
return "⚠️ **無法分析文本,請稍後再試**"
|
52 |
-
|
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 會自動判斷其**情緒分類(正向 / 中立 / 負向)**,並提供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 |
-
|
|
|
|
|
|
1 |
+
from ui import create_ui
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
# 啟動 Web 應用
|
4 |
+
if __name__ == "__main__":
|
5 |
+
iface = create_ui()
|
6 |
+
iface.launch()
|