Spaces:
Sleeping
Sleeping
Update sentiment.py
Browse files- sentiment.py +20 -8
sentiment.py
CHANGED
|
@@ -1,24 +1,27 @@
|
|
| 1 |
import requests
|
|
|
|
| 2 |
from config import HEADERS
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
| 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 |
-
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": text})
|
|
|
|
| 20 |
result = response.json()
|
| 21 |
-
|
| 22 |
|
| 23 |
if isinstance(result, list) and len(result) > 0:
|
| 24 |
sentiment = result[0]["label"]
|
|
@@ -26,7 +29,16 @@ def analyze_sentiment(text, model_id=None):
|
|
| 26 |
return f"**情緒分類**: {sentiment}\n**AI 信心度**: {confidence*100:.2f}%", confidence
|
| 27 |
else:
|
| 28 |
return "⚠️ **無法分析文本,請稍後再試**", 0.0
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
|
| 32 |
-
return f"❌
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import logging
|
| 3 |
from config import HEADERS
|
| 4 |
|
| 5 |
+
# 設定 logging
|
| 6 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 7 |
+
|
| 8 |
CURRENT_MODEL = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
|
| 9 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
| 10 |
|
| 11 |
def analyze_sentiment(text, model_id=None):
|
| 12 |
global CURRENT_MODEL, API_URL
|
| 13 |
|
|
|
|
| 14 |
if model_id and model_id != CURRENT_MODEL:
|
| 15 |
CURRENT_MODEL = model_id
|
| 16 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
| 17 |
+
logging.info(f" 重新載入模型: {CURRENT_MODEL}")
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
logging.info(" 發送 API 請求...")
|
| 21 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": text})
|
| 22 |
+
response.raise_for_status() # 檢查 HTTP 狀態碼
|
| 23 |
result = response.json()
|
| 24 |
+
logging.info(f"✅ API 回應: {result}")
|
| 25 |
|
| 26 |
if isinstance(result, list) and len(result) > 0:
|
| 27 |
sentiment = result[0]["label"]
|
|
|
|
| 29 |
return f"**情緒分類**: {sentiment}\n**AI 信心度**: {confidence*100:.2f}%", confidence
|
| 30 |
else:
|
| 31 |
return "⚠️ **無法分析文本,請稍後再試**", 0.0
|
| 32 |
+
|
| 33 |
+
except requests.exceptions.RequestException as e:
|
| 34 |
+
logging.error(f"❌ API 請求錯誤: {e}")
|
| 35 |
+
return f"❌ **API 請求錯誤**: {str(e)}", 0.0
|
| 36 |
+
except ValueError as e:
|
| 37 |
+
logging.error(f"❌ JSON 解碼錯誤: {e}")
|
| 38 |
+
return f"❌ **JSON 解碼錯誤**: {str(e)}", 0.0
|
| 39 |
+
except KeyError as e:
|
| 40 |
+
logging.error(f"❌ 字典鍵錯誤: {e}")
|
| 41 |
+
return f"❌ **字典鍵錯誤**: {str(e)}", 0.0
|
| 42 |
except Exception as e:
|
| 43 |
+
logging.error(f"❌ 未知錯誤: {e}")
|
| 44 |
+
return f"❌ **未知錯誤**: {str(e)}", 0.0
|