Spaces:
Sleeping
Sleeping
Update context_engine.py
Browse files- context_engine.py +22 -28
context_engine.py
CHANGED
|
@@ -1,50 +1,44 @@
|
|
| 1 |
"""
|
| 2 |
Nexari Context Engine (UPDATED)
|
|
|
|
| 3 |
Improvements:
|
| 4 |
-
-
|
| 5 |
-
-
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
-
from typing import Tuple
|
| 9 |
from transformers import pipeline
|
| 10 |
-
import threading
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
with _emotion_lock:
|
| 19 |
-
if _emotion_checked:
|
| 20 |
-
return
|
| 21 |
-
try:
|
| 22 |
-
_emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
|
| 23 |
-
except Exception as e:
|
| 24 |
-
print(f"Context: Failed to load emotion model: {e}")
|
| 25 |
-
_emotion_classifier = None
|
| 26 |
-
_emotion_checked = True
|
| 27 |
-
|
| 28 |
-
def _safe_emotion_analysis(text: str) -> Tuple[str, float]:
|
| 29 |
-
if not _emotion_checked:
|
| 30 |
-
# spawn a background loader but don't block
|
| 31 |
-
threading.Thread(target=_load_emotion_model, daemon=True).start()
|
| 32 |
-
return ("neutral", 0.0)
|
| 33 |
-
if not _emotion_classifier:
|
| 34 |
return ("neutral", 0.0)
|
| 35 |
try:
|
| 36 |
-
|
|
|
|
| 37 |
if isinstance(res, list) and len(res) > 0:
|
|
|
|
| 38 |
first = res[0]
|
| 39 |
if isinstance(first, dict):
|
| 40 |
return (first.get('label', 'neutral'), float(first.get('score', 0.0)))
|
| 41 |
elif isinstance(first, list) and len(first) > 0 and isinstance(first[0], dict):
|
| 42 |
return (first[0].get('label', 'neutral'), float(first[0].get('score', 0.0)))
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Emotion analysis error: {e}")
|
| 45 |
-
|
| 46 |
|
| 47 |
-
def get_smart_context(user_text
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
label, confidence = _safe_emotion_analysis(user_text)
|
| 50 |
top_emotion = label.lower()
|
|
|
|
| 1 |
"""
|
| 2 |
Nexari Context Engine (UPDATED)
|
| 3 |
+
Author: Piyush
|
| 4 |
Improvements:
|
| 5 |
+
- Robust emotion pipeline usage & error handling
|
| 6 |
+
- Safer fallback when model not available
|
| 7 |
+
- Returns compact psychological profile instruction
|
| 8 |
"""
|
| 9 |
|
|
|
|
| 10 |
from transformers import pipeline
|
|
|
|
| 11 |
|
| 12 |
+
print(">>> Context: Loading Emotion Analysis Model...")
|
| 13 |
+
# load with top_k=1 by default
|
| 14 |
+
try:
|
| 15 |
+
emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Context: Failed to load emotion model: {e}")
|
| 18 |
+
emotion_classifier = None
|
| 19 |
|
| 20 |
+
def _safe_emotion_analysis(text):
|
| 21 |
+
if not emotion_classifier:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return ("neutral", 0.0)
|
| 23 |
try:
|
| 24 |
+
# pipeline may return list of dicts or list-of-lists depending on version
|
| 25 |
+
res = emotion_classifier(text)
|
| 26 |
if isinstance(res, list) and len(res) > 0:
|
| 27 |
+
# res could be [{'label':..., 'score':...}] or [['label',score],...]
|
| 28 |
first = res[0]
|
| 29 |
if isinstance(first, dict):
|
| 30 |
return (first.get('label', 'neutral'), float(first.get('score', 0.0)))
|
| 31 |
elif isinstance(first, list) and len(first) > 0 and isinstance(first[0], dict):
|
| 32 |
return (first[0].get('label', 'neutral'), float(first[0].get('score', 0.0)))
|
| 33 |
+
return ("neutral", 0.0)
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Emotion analysis error: {e}")
|
| 36 |
+
return ("neutral", 0.0)
|
| 37 |
|
| 38 |
+
def get_smart_context(user_text):
|
| 39 |
+
"""
|
| 40 |
+
Analyzes the user's 'Vibe' and returns a short persona instruction.
|
| 41 |
+
"""
|
| 42 |
try:
|
| 43 |
label, confidence = _safe_emotion_analysis(user_text)
|
| 44 |
top_emotion = label.lower()
|