Spaces:
Running
Running
Update sentiment.py
Browse files- sentiment.py +11 -10
sentiment.py
CHANGED
@@ -1,25 +1,26 @@
|
|
1 |
import requests
|
2 |
import logging
|
3 |
-
from config import HEADERS
|
4 |
|
5 |
# 設定 logging
|
6 |
-
logging.basicConfig(level=logging.INFO, format=
|
7 |
|
8 |
-
CURRENT_MODEL =
|
9 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
10 |
|
11 |
-
|
|
|
12 |
global CURRENT_MODEL, API_URL
|
13 |
|
14 |
-
if
|
15 |
-
CURRENT_MODEL =
|
16 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
17 |
-
logging.info(f"
|
18 |
|
19 |
try:
|
20 |
-
logging.info(" 發送 API 請求...")
|
21 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": text})
|
22 |
-
response.raise_for_status()
|
23 |
result = response.json()
|
24 |
logging.info(f"✅ API 回應: {result}")
|
25 |
|
@@ -41,4 +42,4 @@ def analyze_sentiment(text, model_id=None):
|
|
41 |
return f"❌ **字典鍵錯誤**: {str(e)}", 0.0
|
42 |
except Exception as e:
|
43 |
logging.error(f"❌ 未知錯誤: {e}")
|
44 |
-
return f"❌ **未知錯誤**: {str(e)}", 0.0
|
|
|
1 |
import requests
|
2 |
import logging
|
3 |
+
from config import HEADERS, MODEL_OPTIONS, DEFAULT_MODEL
|
4 |
|
5 |
# 設定 logging
|
6 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
7 |
|
8 |
+
CURRENT_MODEL = DEFAULT_MODEL
|
9 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
10 |
|
11 |
+
# 📌 呼叫 Hugging Face API 進行情緒分析
|
12 |
+
def analyze_sentiment(text, model_name=None):
|
13 |
global CURRENT_MODEL, API_URL
|
14 |
|
15 |
+
if model_name and MODEL_OPTIONS[model_name] != CURRENT_MODEL:
|
16 |
+
CURRENT_MODEL = MODEL_OPTIONS[model_name]
|
17 |
API_URL = f"https://api-inference.huggingface.co/models/{CURRENT_MODEL}"
|
18 |
+
logging.info(f"🔄 切換模型: {CURRENT_MODEL}")
|
19 |
|
20 |
try:
|
21 |
+
logging.info("🚀 發送 API 請求...")
|
22 |
response = requests.post(API_URL, headers=HEADERS, json={"inputs": text})
|
23 |
+
response.raise_for_status()
|
24 |
result = response.json()
|
25 |
logging.info(f"✅ API 回應: {result}")
|
26 |
|
|
|
42 |
return f"❌ **字典鍵錯誤**: {str(e)}", 0.0
|
43 |
except Exception as e:
|
44 |
logging.error(f"❌ 未知錯誤: {e}")
|
45 |
+
return f"❌ **未知錯誤**: {str(e)}", 0.0
|