Spaces:
Sleeping
Sleeping
Update context_engine.py
Browse files- context_engine.py +45 -9
context_engine.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# context_engine.py - UPDATED
|
| 2 |
from typing import Tuple
|
| 3 |
import threading
|
| 4 |
|
|
@@ -35,13 +35,18 @@ def _safe_emotion_analysis(text: str) -> Tuple[str, float]:
|
|
| 35 |
print(f"Emotion analysis error: {e}")
|
| 36 |
return ("neutral", 0.0)
|
| 37 |
|
| 38 |
-
def get_smart_context(user_text):
|
| 39 |
"""
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
label, confidence = _safe_emotion_analysis(user_text or "")
|
| 44 |
-
top_emotion =
|
| 45 |
confidence = float(confidence or 0.0)
|
| 46 |
word_count = len((user_text or "").split())
|
| 47 |
|
|
@@ -52,25 +57,56 @@ def get_smart_context(user_text):
|
|
| 52 |
else:
|
| 53 |
conversation_mode = "Deep Dive Mode (Detailed)"
|
| 54 |
|
|
|
|
| 55 |
if top_emotion == "joy":
|
| 56 |
-
emotional_context = "User: Positive/Energetic. Vibe:
|
|
|
|
|
|
|
| 57 |
elif top_emotion == "sadness":
|
| 58 |
-
emotional_context = "User: Low Energy. Vibe: Supportive β
|
|
|
|
|
|
|
| 59 |
elif top_emotion == "anger":
|
| 60 |
-
emotional_context = "User: Frustrated. Vibe: De-escalate β calm, solution-first."
|
|
|
|
|
|
|
| 61 |
elif top_emotion == "fear":
|
| 62 |
emotional_context = "User: Anxious. Vibe: Reassure and clarify."
|
|
|
|
|
|
|
| 63 |
elif top_emotion == "surprise":
|
| 64 |
emotional_context = "User: Curious/Alert. Vibe: Engage and explain."
|
|
|
|
|
|
|
| 65 |
else:
|
| 66 |
emotional_context = "User: Neutral/Professional. Vibe: Helpful and efficient."
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return (
|
| 69 |
f"\n[PSYCHOLOGICAL PROFILE]\n"
|
| 70 |
f"1. Interaction Mode: {conversation_mode}\n"
|
| 71 |
f"2. {emotional_context}\n"
|
| 72 |
-
f"3.
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Context Error: {e}")
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# context_engine.py - UPDATED to include emoji suggestions & guidance
|
| 2 |
from typing import Tuple
|
| 3 |
import threading
|
| 4 |
|
|
|
|
| 35 |
print(f"Emotion analysis error: {e}")
|
| 36 |
return ("neutral", 0.0)
|
| 37 |
|
| 38 |
+
def get_smart_context(user_text: str):
|
| 39 |
"""
|
| 40 |
+
Returns a short persona instruction block including:
|
| 41 |
+
- Conversation Mode (Ping-Pong / Standard / Deep Dive)
|
| 42 |
+
- Emotional context (tone + directive)
|
| 43 |
+
- Emoji suggestions (range and examples)
|
| 44 |
+
- Hint for minimum verbosity (guideline only)
|
| 45 |
+
This string is safe to inject into system prompt.
|
| 46 |
"""
|
| 47 |
try:
|
| 48 |
label, confidence = _safe_emotion_analysis(user_text or "")
|
| 49 |
+
top_emotion = (label or "neutral").lower()
|
| 50 |
confidence = float(confidence or 0.0)
|
| 51 |
word_count = len((user_text or "").split())
|
| 52 |
|
|
|
|
| 57 |
else:
|
| 58 |
conversation_mode = "Deep Dive Mode (Detailed)"
|
| 59 |
|
| 60 |
+
# Emoji mapping and style hints
|
| 61 |
if top_emotion == "joy":
|
| 62 |
+
emotional_context = "User: Positive/Energetic. Vibe: Upbeat β be warm and slightly playful."
|
| 63 |
+
emoji_examples = "π π π"
|
| 64 |
+
emoji_range = (1, 2)
|
| 65 |
elif top_emotion == "sadness":
|
| 66 |
+
emotional_context = "User: Low Energy. Vibe: Supportive β be gentle and empathetic."
|
| 67 |
+
emoji_examples = "π€ π€οΈ"
|
| 68 |
+
emoji_range = (0, 1)
|
| 69 |
elif top_emotion == "anger":
|
| 70 |
+
emotional_context = "User: Frustrated. Vibe: De-escalate β calm, solution-first, avoid sarcasm."
|
| 71 |
+
emoji_examples = "π π οΈ"
|
| 72 |
+
emoji_range = (0, 1)
|
| 73 |
elif top_emotion == "fear":
|
| 74 |
emotional_context = "User: Anxious. Vibe: Reassure and clarify."
|
| 75 |
+
emoji_examples = "π€ π‘οΈ"
|
| 76 |
+
emoji_range = (0, 1)
|
| 77 |
elif top_emotion == "surprise":
|
| 78 |
emotional_context = "User: Curious/Alert. Vibe: Engage and explain."
|
| 79 |
+
emoji_examples = "π€ β¨"
|
| 80 |
+
emoji_range = (0, 2)
|
| 81 |
else:
|
| 82 |
emotional_context = "User: Neutral/Professional. Vibe: Helpful and efficient."
|
| 83 |
+
emoji_examples = "π‘ π"
|
| 84 |
+
emoji_range = (0, 2)
|
| 85 |
|
| 86 |
+
# Verbosity hint (guideline)
|
| 87 |
+
if "Deep Dive" in conversation_mode:
|
| 88 |
+
min_words_hint = 70
|
| 89 |
+
elif "Standard" in conversation_mode:
|
| 90 |
+
min_words_hint = 30
|
| 91 |
+
else:
|
| 92 |
+
min_words_hint = 12
|
| 93 |
+
|
| 94 |
+
# Build the context block
|
| 95 |
return (
|
| 96 |
f"\n[PSYCHOLOGICAL PROFILE]\n"
|
| 97 |
f"1. Interaction Mode: {conversation_mode}\n"
|
| 98 |
f"2. {emotional_context}\n"
|
| 99 |
+
f"3. Emoji Suggestions: Use {emoji_range[0]}β{emoji_range[1]} emoji(s). Examples: {emoji_examples}\n"
|
| 100 |
+
f"4. Minimum Word Guidance: Aim for ~{min_words_hint} words unless user explicitly requests 'short' or 'brief'.\n"
|
| 101 |
+
f"5. Directive: Mirror user's energy; prefer natural phrasing and avoid robotic one-line replies.\n"
|
| 102 |
)
|
| 103 |
except Exception as e:
|
| 104 |
print(f"Context Error: {e}")
|
| 105 |
+
# Safe default
|
| 106 |
+
return (
|
| 107 |
+
"\n[PSYCHOLOGICAL PROFILE]\n"
|
| 108 |
+
"1. Interaction Mode: Standard Chat Mode (Balanced)\n"
|
| 109 |
+
"2. User: Neutral. Vibe: Helpful and efficient.\n"
|
| 110 |
+
"3. Emoji Suggestions: Use 0β1 emoji. Examples: π\n"
|
| 111 |
+
"4. Minimum Word Guidance: Aim for ~30 words.\n"
|
| 112 |
+
)
|