Spaces:
Running
Running
Update sentiment.py
Browse files- sentiment.py +7 -29
sentiment.py
CHANGED
@@ -5,50 +5,28 @@ from config import HEADERS
|
|
5 |
CURRENT_MODEL = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
6 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
7 |
|
8 |
-
# 轉換英文分類為中文
|
9 |
-
def translate_sentiment(label):
|
10 |
-
if "positive" in label.lower():
|
11 |
-
return "😃 **正向**"
|
12 |
-
elif "neutral" in label.lower():
|
13 |
-
return "😐 **中立**"
|
14 |
-
else:
|
15 |
-
return "😡 **負向**"
|
16 |
-
|
17 |
-
# 轉換信心度
|
18 |
-
def convert_confidence(score):
|
19 |
-
percentage = round(score * 100)
|
20 |
-
if score >= 0.90:
|
21 |
-
return f"🌟 **極高信心** ({percentage}%)"
|
22 |
-
elif score >= 0.75:
|
23 |
-
return f"✅ **高信心** ({percentage}%)"
|
24 |
-
elif score >= 0.50:
|
25 |
-
return f"⚠️ **中等信心** ({percentage}%)"
|
26 |
-
elif score >= 0.30:
|
27 |
-
return f"❓ **低信心** ({percentage}%)"
|
28 |
-
else:
|
29 |
-
return f"❌ **極低信心(建議忽略)** ({percentage}%)"
|
30 |
-
|
31 |
-
# 📌 調用 Hugging Face API 進行情緒分析
|
32 |
def analyze_sentiment(text, model_id=None):
|
33 |
global CURRENT_MODEL, API_URL
|
34 |
|
35 |
-
# **只在模型變更時更新 API URL**
|
36 |
if model_id and model_id != CURRENT_MODEL:
|
37 |
CURRENT_MODEL = model_id
|
38 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
|
|
39 |
|
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 =
|
46 |
confidence = result[0]["score"]
|
47 |
-
|
48 |
-
|
49 |
-
return f"**情緒分類**: {sentiment}\n**AI 判斷的信心度**: {confidence_label}", confidence
|
50 |
else:
|
51 |
return "⚠️ **無法分析文本,請稍後再試**", 0.0
|
52 |
|
53 |
except Exception as e:
|
|
|
54 |
return f"❌ **錯誤**: {str(e)}", 0.0
|
|
|
5 |
CURRENT_MODEL = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
6 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def analyze_sentiment(text, model_id=None):
|
9 |
global CURRENT_MODEL, API_URL
|
10 |
|
11 |
+
# 📌 **只在模型變更時更新 API URL**
|
12 |
if model_id and model_id != CURRENT_MODEL:
|
13 |
CURRENT_MODEL = model_id
|
14 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
15 |
+
print(f"🔄 重新載入模型: {CURRENT_MODEL}")
|
16 |
|
17 |
try:
|
18 |
+
print("🚀 發送 API 請求...")
|
19 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": text})
|
20 |
result = response.json()
|
21 |
+
print(f"✅ API 回應: {result}")
|
22 |
|
23 |
if isinstance(result, list) and len(result) > 0:
|
24 |
+
sentiment = result[0]["label"]
|
25 |
confidence = result[0]["score"]
|
26 |
+
return f"**情緒分類**: {sentiment}\n**AI 信心度**: {confidence*100:.2f}%", confidence
|
|
|
|
|
27 |
else:
|
28 |
return "⚠️ **無法分析文本,請稍後再試**", 0.0
|
29 |
|
30 |
except Exception as e:
|
31 |
+
print(f"❌ API 錯誤: {e}")
|
32 |
return f"❌ **錯誤**: {str(e)}", 0.0
|