Spaces:
Runtime error
Runtime error
kootaeng2
commited on
Commit
ยท
136c55f
1
Parent(s):
39c894a
Fix: Correct model path discovery for server environment
Browse files- src/emotion_engine.py +15 -20
src/emotion_engine.py
CHANGED
|
@@ -1,37 +1,32 @@
|
|
| 1 |
-
# emotion_engine.py
|
| 2 |
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 5 |
import os
|
| 6 |
|
| 7 |
def load_emotion_classifier():
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
MODEL_PATH = os.path.join(base_path, "korean-emotion-classifier-final")
|
| 13 |
-
|
| 14 |
-
# ๊ฒฝ๋ก๊ฐ ๋ก์ปฌ ๋๋ ํฐ๋ฆฌ์ธ์ง ํ์ธ
|
| 15 |
-
if not os.path.isdir(MODEL_PATH):
|
| 16 |
-
print(f"โ ์ค๋ฅ: ์ง์ ๋ ๊ฒฝ๋ก '{MODEL_PATH}'์ ๋ชจ๋ธ ํด๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
|
| 17 |
-
return None
|
| 18 |
-
|
| 19 |
-
print(f"--- ์ต์ข
๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ: [{MODEL_PATH}] ---")
|
| 20 |
-
print(f"๋ก์ปฌ ์ ๋ ๊ฒฝ๋ก '{MODEL_PATH}'์์ ๋ชจ๋ธ์ ์ง์ ๋ถ๋ฌ์ต๋๋ค...")
|
| 21 |
|
| 22 |
try:
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 27 |
-
|
| 28 |
print("โ
๋ก์ปฌ ๋ชจ๋ธ ํ์ผ ์ง์ ๋ก๋ฉ ์ฑ๊ณต!")
|
| 29 |
|
| 30 |
except Exception as e:
|
| 31 |
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์ค ์ค๋ฅ: {e}")
|
| 32 |
-
# ์ค๋ฅ๊ฐ ๋ฐ์ํ ์์ธ์ ์ ํํ ์ถ๋ ฅํฉ๋๋ค.
|
| 33 |
-
print(f"์์ธ ์ค๋ฅ ๋ฉ์์ง: {e}")
|
| 34 |
return None
|
|
|
|
| 35 |
|
| 36 |
device = 0 if torch.cuda.is_available() else -1
|
| 37 |
emotion_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=device)
|
|
|
|
| 1 |
+
# emotion_engine.py (์์ ํ ์ต์ข
๋ฒ์ )
|
| 2 |
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 5 |
import os
|
| 6 |
|
| 7 |
def load_emotion_classifier():
|
| 8 |
+
# --- ์ด ๋ถ๋ถ์ ์์ ํฉ๋๋ค ---
|
| 9 |
+
# ํ์ฌ ์คํฌ๋ฆฝํธ ํ์ผ์ ์ ๋ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ต๋๋ค. (์: /app/src/emotion_engine.py)
|
| 10 |
+
script_path = os.path.abspath(__file__)
|
| 11 |
+
# ์คํฌ๋ฆฝํธ๊ฐ ์๋ ๋๋ ํฐ๋ฆฌ๋ฅผ ์ฐพ์ต๋๋ค. (์: /app/src)
|
| 12 |
+
src_dir = os.path.dirname(script_path)
|
| 13 |
+
# ๊ทธ ์์ ๋๋ ํฐ๋ฆฌ, ์ฆ ํ๋ก์ ํธ ๋ฃจํธ๋ฅผ ์ฐพ์ต๋๋ค. (์: /app)
|
| 14 |
+
base_dir = os.path.dirname(src_dir)
|
| 15 |
+
# ํ๋ก์ ํธ ๋ฃจํธ์ ๋ชจ๋ธ ํด๋ ์ด๋ฆ์ ํฉ์ณ ์ ํํ ๊ฒฝ๋ก๋ฅผ ๋ง๋ญ๋๋ค.
|
| 16 |
+
MODEL_PATH = os.path.join(base_dir, "korean-emotion-classifier-final")
|
| 17 |
|
| 18 |
+
print(f"--- ๋ฐฐํฌ ํ๊ฒฝ ๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ: [{MODEL_PATH}] ---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
# local_files_only ์ต์
์ ๋ก์ปฌ ๊ฒฝ๋ก๋ฅผ ๋ช
์ํ ๋ ์์ ์ ์ํด ์ ์งํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, local_files_only=True)
|
| 23 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH, local_files_only=True)
|
|
|
|
|
|
|
| 24 |
print("โ
๋ก์ปฌ ๋ชจ๋ธ ํ์ผ ์ง์ ๋ก๋ฉ ์ฑ๊ณต!")
|
| 25 |
|
| 26 |
except Exception as e:
|
| 27 |
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์ค ์ค๋ฅ: {e}")
|
|
|
|
|
|
|
| 28 |
return None
|
| 29 |
+
# --- ์ฌ๊ธฐ๊น์ง ์์ ---
|
| 30 |
|
| 31 |
device = 0 if torch.cuda.is_available() else -1
|
| 32 |
emotion_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=device)
|