Nexari-Research commited on
Commit
7bcdc81
·
verified ·
1 Parent(s): 323191f

Update context_engine.py

Browse files
Files changed (1) hide show
  1. context_engine.py +28 -29
context_engine.py CHANGED
@@ -1,35 +1,35 @@
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}")
@@ -40,11 +40,10 @@ def get_smart_context(user_text):
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()
45
- confidence = float(confidence)
46
-
47
- word_count = len(user_text.split()) if user_text else 0
48
 
49
  if word_count < 4:
50
  conversation_mode = "Ping-Pong Mode (Fast)"
 
1
+ # context_engine.py - UPDATED (replace original)
2
+ from typing import Tuple
3
+ import threading
 
 
 
 
 
4
 
5
+ _emotion_classifier = None
6
+ _emotion_lock = threading.Lock()
7
 
8
+ def _load_emotion_model():
9
+ global _emotion_classifier
10
+ try:
11
+ from transformers import pipeline
12
+ _emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
13
+ except Exception as e:
14
+ print(f"Context: Failed to load emotion model: {e}")
15
+ _emotion_classifier = None
16
 
17
+ def _ensure_emotion_loaded():
18
+ if _emotion_classifier is None:
19
+ with _emotion_lock:
20
+ if _emotion_classifier is None:
21
+ _load_emotion_model()
22
+
23
+ def _safe_emotion_analysis(text: str) -> Tuple[str, float]:
24
  try:
25
+ _ensure_emotion_loaded()
26
+ if not _emotion_classifier:
27
+ return ("neutral", 0.0)
28
+ res = _emotion_classifier(text)
29
  if isinstance(res, list) and len(res) > 0:
 
30
  first = res[0]
31
  if isinstance(first, dict):
32
  return (first.get('label', 'neutral'), float(first.get('score', 0.0)))
 
 
33
  return ("neutral", 0.0)
34
  except Exception as e:
35
  print(f"Emotion analysis error: {e}")
 
40
  Analyzes the user's 'Vibe' and returns a short persona instruction.
41
  """
42
  try:
43
+ label, confidence = _safe_emotion_analysis(user_text or "")
44
+ top_emotion = label.lower() if label else "neutral"
45
+ confidence = float(confidence or 0.0)
46
+ word_count = len((user_text or "").split())
 
47
 
48
  if word_count < 4:
49
  conversation_mode = "Ping-Pong Mode (Fast)"