Spaces:
Sleeping
Sleeping
wenbemi
commited on
Update chat_a.py
Browse files
chat_a.py
CHANGED
|
@@ -1039,22 +1039,23 @@ def override_emotion_if_needed(text):
|
|
| 1039 |
return None
|
| 1040 |
|
| 1041 |
def analyze_emotion(text: str):
|
| 1042 |
-
tokenizer = load_tokenizer()
|
| 1043 |
model = load_sentiment_model()
|
|
|
|
| 1044 |
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
max_length=256,
|
| 1051 |
-
)
|
| 1052 |
with torch.no_grad():
|
| 1053 |
-
logits = model(**inputs).logits
|
| 1054 |
-
probs = F.softmax(logits, dim=1)[0]
|
|
|
|
| 1055 |
top_indices = torch.topk(probs, k=5).indices.tolist()
|
| 1056 |
top_emotions = [(klue_emotions[i], float(probs[i]) * 100) for i in top_indices]
|
| 1057 |
-
top_emotion_groups = list(dict.fromkeys(
|
|
|
|
|
|
|
| 1058 |
return top_emotions, top_emotion_groups
|
| 1059 |
|
| 1060 |
def detect_intent(user_input):
|
|
|
|
| 1039 |
return None
|
| 1040 |
|
| 1041 |
def analyze_emotion(text: str):
|
|
|
|
| 1042 |
model = load_sentiment_model()
|
| 1043 |
+
tokenizer = load_tokenizer()
|
| 1044 |
|
| 1045 |
+
override = override_emotion_if_needed(text)
|
| 1046 |
+
if override:
|
| 1047 |
+
return override
|
| 1048 |
+
|
| 1049 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
|
|
|
|
|
|
| 1050 |
with torch.no_grad():
|
| 1051 |
+
logits = model(**inputs).logits # ← model은 '단일 객체'
|
| 1052 |
+
probs = F.softmax(logits, dim=1)[0]
|
| 1053 |
+
|
| 1054 |
top_indices = torch.topk(probs, k=5).indices.tolist()
|
| 1055 |
top_emotions = [(klue_emotions[i], float(probs[i]) * 100) for i in top_indices]
|
| 1056 |
+
top_emotion_groups = list(dict.fromkeys(
|
| 1057 |
+
[klue_to_general[i] for i in top_indices if probs[i] > 0.05]
|
| 1058 |
+
))
|
| 1059 |
return top_emotions, top_emotion_groups
|
| 1060 |
|
| 1061 |
def detect_intent(user_input):
|