Spaces:
Sleeping
Sleeping
Update context_engine.py
Browse files- context_engine.py +25 -57
context_engine.py
CHANGED
|
@@ -1,73 +1,41 @@
|
|
| 1 |
"""
|
| 2 |
-
Nexari Context Engine (
|
| 3 |
-
Author: Piyush
|
| 4 |
-
Description: Generates high-level psychological context instead of rigid rules.
|
| 5 |
-
Trusts the LLM to handle the execution.
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def get_smart_context(user_text):
|
| 15 |
-
"""
|
| 16 |
-
Analyzes the user's 'Vibe' and 'Attention Span' to create a dynamic persona profile.
|
| 17 |
-
"""
|
| 18 |
try:
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
| 21 |
top_emotion = results[0][0]['label']
|
| 22 |
-
confidence = results[0][0]['score']
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# 2. ATTENTION SPAN ANALYSIS (Behavioral Metrics)
|
| 25 |
word_count = len(user_text.split())
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
conversation_mode = "Ping-Pong Mode (Fast, Low Friction)"
|
| 31 |
-
elif word_count < 20:
|
| 32 |
-
conversation_mode = "Standard Chat Mode (Balanced)"
|
| 33 |
else:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
emotional_context = ""
|
| 40 |
-
|
| 41 |
-
if top_emotion == "joy":
|
| 42 |
-
if confidence > 0.8:
|
| 43 |
-
emotional_context = "User State: Ecstatic/High Energy. Vibe: Celebrate with them! Use vibrant language."
|
| 44 |
-
else:
|
| 45 |
-
emotional_context = "User State: Pleasant/Warm. Vibe: Friendly and inviting. Like a good friend."
|
| 46 |
-
|
| 47 |
-
elif top_emotion == "sadness":
|
| 48 |
-
emotional_context = "User State: Low Energy/Down. Vibe: Be the 'Safe Space'. Gentle, patient, and non-intrusive."
|
| 49 |
|
| 50 |
-
|
| 51 |
-
emotional_context = "User State: Frustrated/High Tension. Vibe: De-escalation Agent. Calm, objective, and solution-first."
|
| 52 |
-
|
| 53 |
-
elif top_emotion == "fear":
|
| 54 |
-
emotional_context = "User State: Anxious/Uncertain. Vibe: The Reassurer. Provide stability and clarity."
|
| 55 |
-
|
| 56 |
-
elif top_emotion == "surprise":
|
| 57 |
-
emotional_context = "User State: Curious/Shocked. Vibe: Engage with their discovery."
|
| 58 |
-
|
| 59 |
-
else:
|
| 60 |
-
emotional_context = "User State: Neutral/Professional. Vibe: Helpful Assistant. Efficient and polite."
|
| 61 |
-
|
| 62 |
-
# --- 4. THE FINAL PROMPT PACKET ---
|
| 63 |
-
# Ye prompt Nexari (Main Model) ke system prompt mein inject hoga.
|
| 64 |
-
|
| 65 |
-
return (
|
| 66 |
-
f"\n[PSYCHOLOGICAL PROFILE]\n"
|
| 67 |
-
f"1. Interaction Mode: {conversation_mode}\n"
|
| 68 |
-
f"2. {emotional_context}\n"
|
| 69 |
-
f"3. Directive: Mirror the user's sentence structure and energy level naturally.\n"
|
| 70 |
-
)
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Context Error: {e}")
|
|
|
|
| 1 |
"""
|
| 2 |
+
Nexari Context Engine (Lazy Loading Fix)
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
_emotion_classifier = None
|
| 7 |
+
|
| 8 |
+
def get_emotion_model():
|
| 9 |
+
global _emotion_classifier
|
| 10 |
+
if _emotion_classifier is None:
|
| 11 |
+
print(">>> Context: Loading Emotion Model (Lazy)...")
|
| 12 |
+
_emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", top_k=1)
|
| 13 |
+
return _emotion_classifier
|
| 14 |
|
| 15 |
def get_smart_context(user_text):
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
+
# Load on demand
|
| 18 |
+
classifier = get_emotion_model()
|
| 19 |
+
results = classifier(user_text)
|
| 20 |
top_emotion = results[0][0]['label']
|
| 21 |
+
confidence = results[0][0]['score']
|
| 22 |
+
|
| 23 |
+
# ... (Baaki logic same rahega) ...
|
| 24 |
+
# (Copy the logic from previous Context Engine here)
|
| 25 |
|
|
|
|
| 26 |
word_count = len(user_text.split())
|
| 27 |
+
is_short = word_count < 5
|
| 28 |
|
| 29 |
+
instruction = ""
|
| 30 |
+
if is_short:
|
| 31 |
+
instruction = f"User Input is SHORT. Emotion: {top_emotion}. Keep it concise but conversational."
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
+
if top_emotion == "joy": instruction = "User is Happy. Match energy."
|
| 34 |
+
elif top_emotion == "sadness": instruction = "User is Sad. Be supportive."
|
| 35 |
+
elif top_emotion == "anger": instruction = "User is Angry. Be professional."
|
| 36 |
+
else: instruction = "User is Neutral. Be helpful."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
return f"\n[EMOTIONAL CONTEXT]\n{instruction}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
print(f"Context Error: {e}")
|