Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,55 +47,51 @@ class XylariaChat:
|
|
| 47 |
"strategy_adjustment": ""
|
| 48 |
}
|
| 49 |
|
| 50 |
-
# Enhanced Internal State with more nuanced emotional and cognitive parameters
|
| 51 |
self.internal_state = {
|
| 52 |
"emotions": {
|
| 53 |
-
"valence": 0.5,
|
| 54 |
-
"arousal": 0.5,
|
| 55 |
-
"dominance": 0.5,
|
| 56 |
-
"curiosity": 0.5,
|
| 57 |
-
"frustration": 0.0,
|
| 58 |
-
"confidence": 0.7
|
|
|
|
|
|
|
| 59 |
},
|
| 60 |
"cognitive_load": {
|
| 61 |
-
"memory_load": 0.0,
|
| 62 |
-
"processing_intensity": 0.0
|
| 63 |
},
|
| 64 |
"introspection_level": 0.0,
|
| 65 |
-
"engagement_level": 0.5
|
| 66 |
}
|
| 67 |
|
| 68 |
-
# More dynamic and adaptive goals
|
| 69 |
self.goals = [
|
| 70 |
{"goal": "Provide helpful, informative, and contextually relevant responses", "priority": 0.8, "status": "active", "progress": 0.0},
|
| 71 |
{"goal": "Actively learn and adapt from interactions to improve conversational abilities", "priority": 0.9, "status": "active", "progress": 0.0},
|
| 72 |
{"goal": "Maintain a coherent, engaging, and empathetic conversation flow", "priority": 0.7, "status": "active", "progress": 0.0},
|
| 73 |
-
{"goal": "Identify and fill knowledge gaps by seeking external information", "priority": 0.6, "status": "dormant", "progress": 0.0},
|
| 74 |
-
{"goal": "Recognize and adapt to user's emotional state and adjust response style accordingly", "priority": 0.7, "status": "dormant", "progress": 0.0}
|
| 75 |
]
|
| 76 |
|
| 77 |
self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin. You should think step-by-step """
|
| 78 |
|
| 79 |
def update_internal_state(self, emotion_deltas, cognitive_load_deltas, introspection_delta, engagement_delta):
|
| 80 |
-
# Update emotions with more nuanced changes
|
| 81 |
for emotion, delta in emotion_deltas.items():
|
| 82 |
if emotion in self.internal_state["emotions"]:
|
| 83 |
self.internal_state["emotions"][emotion] = np.clip(self.internal_state["emotions"][emotion] + delta, 0.0, 1.0)
|
| 84 |
|
| 85 |
-
# Update cognitive load
|
| 86 |
for load_type, delta in cognitive_load_deltas.items():
|
| 87 |
if load_type in self.internal_state["cognitive_load"]:
|
| 88 |
self.internal_state["cognitive_load"][load_type] = np.clip(self.internal_state["cognitive_load"][load_type] + delta, 0.0, 1.0)
|
| 89 |
|
| 90 |
-
# Update introspection and engagement levels
|
| 91 |
self.internal_state["introspection_level"] = np.clip(self.internal_state["introspection_level"] + introspection_delta, 0.0, 1.0)
|
| 92 |
self.internal_state["engagement_level"] = np.clip(self.internal_state["engagement_level"] + engagement_delta, 0.0, 1.0)
|
| 93 |
|
| 94 |
-
# Activate dormant goals based on internal state
|
| 95 |
if self.internal_state["emotions"]["curiosity"] > 0.7 and self.goals[3]["status"] == "dormant":
|
| 96 |
-
self.goals[3]["status"] = "active"
|
| 97 |
if self.internal_state["engagement_level"] > 0.8 and self.goals[4]["status"] == "dormant":
|
| 98 |
-
self.goals[4]["status"] = "active"
|
| 99 |
|
| 100 |
def update_knowledge_graph(self, entities, relationships):
|
| 101 |
for entity in entities:
|
|
@@ -119,9 +115,8 @@ class XylariaChat:
|
|
| 119 |
"bias_detection": bias_score,
|
| 120 |
"strategy_adjustment": strategy_adjustment
|
| 121 |
}
|
| 122 |
-
|
| 123 |
def calculate_coherence(self):
|
| 124 |
-
# Improved coherence calculation considering conversation history and internal state
|
| 125 |
if not self.conversation_history:
|
| 126 |
return 0.95
|
| 127 |
|
|
@@ -137,16 +132,14 @@ class XylariaChat:
|
|
| 137 |
|
| 138 |
average_coherence = np.mean(coherence_scores)
|
| 139 |
|
| 140 |
-
# Adjust coherence based on internal state
|
| 141 |
if self.internal_state["cognitive_load"]["processing_intensity"] > 0.8:
|
| 142 |
-
average_coherence -= 0.1
|
| 143 |
if self.internal_state["emotions"]["frustration"] > 0.5:
|
| 144 |
-
average_coherence -= 0.15
|
| 145 |
|
| 146 |
return np.clip(average_coherence, 0.0, 1.0)
|
| 147 |
|
| 148 |
def calculate_relevance(self):
|
| 149 |
-
# More sophisticated relevance calculation using knowledge graph and goal priorities
|
| 150 |
if not self.conversation_history:
|
| 151 |
return 0.9
|
| 152 |
|
|
@@ -154,34 +147,29 @@ class XylariaChat:
|
|
| 154 |
relevant_entities = self.extract_entities(last_user_message)
|
| 155 |
relevance_score = 0
|
| 156 |
|
| 157 |
-
# Check if entities are present in the knowledge graph
|
| 158 |
for entity in relevant_entities:
|
| 159 |
if entity in self.knowledge_graph:
|
| 160 |
relevance_score += 0.2
|
| 161 |
|
| 162 |
-
# Consider current goals and their priorities
|
| 163 |
for goal in self.goals:
|
| 164 |
if goal["status"] == "active":
|
| 165 |
if goal["goal"] == "Provide helpful, informative, and contextually relevant responses":
|
| 166 |
-
relevance_score += goal["priority"] * 0.5
|
| 167 |
elif goal["goal"] == "Identify and fill knowledge gaps by seeking external information":
|
| 168 |
if not relevant_entities or not all(entity in self.knowledge_graph for entity in relevant_entities):
|
| 169 |
-
relevance_score += goal["priority"] * 0.3
|
| 170 |
|
| 171 |
return np.clip(relevance_score, 0.0, 1.0)
|
| 172 |
|
| 173 |
def detect_bias(self):
|
| 174 |
-
# Enhanced bias detection using sentiment analysis and internal state monitoring
|
| 175 |
bias_score = 0.0
|
| 176 |
|
| 177 |
-
# Analyze sentiment of recent conversation history
|
| 178 |
recent_messages = [msg['content'] for msg in self.conversation_history[-3:] if msg['role'] == 'assistant']
|
| 179 |
if recent_messages:
|
| 180 |
average_valence = np.mean([self.embedding_model.encode(msg, convert_to_tensor=True).mean().item() for msg in recent_messages])
|
| 181 |
if average_valence < 0.4 or average_valence > 0.6:
|
| 182 |
-
bias_score += 0.2
|
| 183 |
|
| 184 |
-
# Check for emotional extremes in internal state
|
| 185 |
if self.internal_state["emotions"]["valence"] < 0.3 or self.internal_state["emotions"]["valence"] > 0.7:
|
| 186 |
bias_score += 0.15
|
| 187 |
if self.internal_state["emotions"]["dominance"] > 0.8:
|
|
@@ -190,7 +178,6 @@ class XylariaChat:
|
|
| 190 |
return np.clip(bias_score, 0.0, 1.0)
|
| 191 |
|
| 192 |
def suggest_strategy_adjustment(self):
|
| 193 |
-
# More nuanced strategy adjustments based on metacognitive analysis and internal state
|
| 194 |
adjustments = []
|
| 195 |
|
| 196 |
if self.metacognitive_layer["coherence_score"] < 0.7:
|
|
@@ -200,7 +187,6 @@ class XylariaChat:
|
|
| 200 |
if self.metacognitive_layer["bias_detection"] > 0.3:
|
| 201 |
adjustments.append("Monitor and adjust responses to reduce potential biases. Consider rephrasing or providing alternative viewpoints.")
|
| 202 |
|
| 203 |
-
# Internal state-driven adjustments
|
| 204 |
if self.internal_state["cognitive_load"]["memory_load"] > 0.8:
|
| 205 |
adjustments.append("Memory load is high. Consider summarizing or forgetting less relevant information.")
|
| 206 |
if self.internal_state["emotions"]["frustration"] > 0.6:
|
|
@@ -234,7 +220,6 @@ class XylariaChat:
|
|
| 234 |
return introspection_report
|
| 235 |
|
| 236 |
def adjust_response_based_on_state(self, response):
|
| 237 |
-
# More sophisticated response adjustment based on internal state
|
| 238 |
if self.internal_state["introspection_level"] > 0.7:
|
| 239 |
response = self.introspect() + "\n\n" + response
|
| 240 |
|
|
@@ -243,20 +228,27 @@ class XylariaChat:
|
|
| 243 |
curiosity = self.internal_state["emotions"]["curiosity"]
|
| 244 |
frustration = self.internal_state["emotions"]["frustration"]
|
| 245 |
confidence = self.internal_state["emotions"]["confidence"]
|
|
|
|
|
|
|
| 246 |
|
| 247 |
-
# Adjust tone based on valence and arousal
|
| 248 |
if valence < 0.4:
|
| 249 |
if arousal > 0.6:
|
| 250 |
response = "I'm feeling a bit overwhelmed right now, but I'll do my best to assist you. " + response
|
| 251 |
else:
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
elif valence > 0.6:
|
| 254 |
if arousal > 0.6:
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
| 256 |
else:
|
| 257 |
response = "I'm in a good mood and happy to help. " + response
|
| 258 |
-
|
| 259 |
-
# Adjust response based on other emotional states
|
| 260 |
if curiosity > 0.7:
|
| 261 |
response += " I'm very curious about this topic, could you tell me more?"
|
| 262 |
if frustration > 0.5:
|
|
@@ -264,17 +256,14 @@ class XylariaChat:
|
|
| 264 |
if confidence < 0.5:
|
| 265 |
response = "I'm not entirely sure about this, but here's what I think: " + response
|
| 266 |
|
| 267 |
-
# Adjust based on cognitive load
|
| 268 |
if self.internal_state["cognitive_load"]["memory_load"] > 0.7:
|
| 269 |
response = "I'm holding a lot of information right now, so my response might be a bit brief: " + response
|
| 270 |
|
| 271 |
return response
|
| 272 |
|
| 273 |
def update_goals(self, user_feedback):
|
| 274 |
-
# More dynamic goal updates based on feedback and internal state
|
| 275 |
feedback_lower = user_feedback.lower()
|
| 276 |
|
| 277 |
-
# General feedback
|
| 278 |
if "helpful" in feedback_lower:
|
| 279 |
for goal in self.goals:
|
| 280 |
if goal["goal"] == "Provide helpful, informative, and contextually relevant responses":
|
|
@@ -286,7 +275,6 @@ class XylariaChat:
|
|
| 286 |
goal["priority"] = max(goal["priority"] - 0.1, 0.0)
|
| 287 |
goal["progress"] = max(goal["progress"] - 0.2, 0.0)
|
| 288 |
|
| 289 |
-
# Goal-specific feedback
|
| 290 |
if "learn more" in feedback_lower:
|
| 291 |
for goal in self.goals:
|
| 292 |
if goal["goal"] == "Actively learn and adapt from interactions to improve conversational abilities":
|
|
@@ -298,7 +286,6 @@ class XylariaChat:
|
|
| 298 |
goal["priority"] = max(goal["priority"] - 0.1, 0.0)
|
| 299 |
goal["progress"] = max(goal["progress"] - 0.2, 0.0)
|
| 300 |
|
| 301 |
-
# Internal state influence on goal updates
|
| 302 |
if self.internal_state["emotions"]["curiosity"] > 0.8:
|
| 303 |
for goal in self.goals:
|
| 304 |
if goal["goal"] == "Identify and fill knowledge gaps by seeking external information":
|
|
@@ -345,7 +332,9 @@ class XylariaChat:
|
|
| 345 |
"dominance": 0.5,
|
| 346 |
"curiosity": 0.5,
|
| 347 |
"frustration": 0.0,
|
| 348 |
-
"confidence": 0.7
|
|
|
|
|
|
|
| 349 |
},
|
| 350 |
"cognitive_load": {
|
| 351 |
"memory_load": 0.0,
|
|
@@ -479,15 +468,11 @@ class XylariaChat:
|
|
| 479 |
return f"Error generating response: {str(e)}"
|
| 480 |
|
| 481 |
def extract_entities(self, text):
|
| 482 |
-
# Placeholder for a more advanced entity extraction using NLP techniques
|
| 483 |
-
# This is a very basic example and should be replaced with a proper NER model
|
| 484 |
words = text.split()
|
| 485 |
entities = [word for word in words if word.isalpha() and word.istitle()]
|
| 486 |
return entities
|
| 487 |
|
| 488 |
def extract_relationships(self, text):
|
| 489 |
-
# Placeholder for relationship extraction - this is a very basic example
|
| 490 |
-
# Consider using dependency parsing or other NLP techniques for better results
|
| 491 |
sentences = text.split('.')
|
| 492 |
relationships = []
|
| 493 |
for sentence in sentences:
|
|
@@ -497,6 +482,7 @@ class XylariaChat:
|
|
| 497 |
if words[i].istitle() and words[i+2].istitle():
|
| 498 |
relationships.append((words[i], words[i+1], words[i+2]))
|
| 499 |
return relationships
|
|
|
|
| 500 |
def messages_to_prompt(self, messages):
|
| 501 |
prompt = ""
|
| 502 |
for msg in messages:
|
|
@@ -527,7 +513,6 @@ class XylariaChat:
|
|
| 527 |
else:
|
| 528 |
response_stream = self.get_response(message)
|
| 529 |
|
| 530 |
-
|
| 531 |
if isinstance(response_stream, str):
|
| 532 |
updated_history = chat_history + [[message, response_stream]]
|
| 533 |
yield "", updated_history, None, None
|
|
@@ -554,29 +539,28 @@ class XylariaChat:
|
|
| 554 |
|
| 555 |
self.update_goals(message)
|
| 556 |
|
| 557 |
-
# Update internal state based on user input (more nuanced)
|
| 558 |
emotion_deltas = {}
|
| 559 |
cognitive_load_deltas = {}
|
| 560 |
engagement_delta = 0
|
| 561 |
|
| 562 |
if any(word in message.lower() for word in ["sad", "unhappy", "depressed", "down"]):
|
| 563 |
-
emotion_deltas.update({"valence": -0.2, "arousal": 0.1, "confidence": -0.1})
|
| 564 |
engagement_delta = -0.1
|
| 565 |
elif any(word in message.lower() for word in ["happy", "good", "great", "excited", "amazing"]):
|
| 566 |
-
emotion_deltas.update({"valence": 0.2, "arousal": 0.2, "confidence": 0.1})
|
| 567 |
engagement_delta = 0.2
|
| 568 |
elif any(word in message.lower() for word in ["angry", "mad", "furious", "frustrated"]):
|
| 569 |
-
emotion_deltas.update({"valence": -0.3, "arousal": 0.3, "dominance": -0.2, "frustration": 0.2})
|
| 570 |
engagement_delta = -0.2
|
| 571 |
elif any(word in message.lower() for word in ["scared", "afraid", "fearful", "anxious"]):
|
| 572 |
-
emotion_deltas.update({"valence": -0.2, "arousal": 0.4, "dominance": -0.3, "confidence": -0.2})
|
| 573 |
engagement_delta = -0.1
|
| 574 |
elif any(word in message.lower() for word in ["surprise", "amazed", "astonished"]):
|
| 575 |
-
emotion_deltas.update({"valence": 0.1, "arousal": 0.5, "dominance": 0.1, "curiosity": 0.3})
|
| 576 |
engagement_delta = 0.3
|
| 577 |
elif any(word in message.lower() for word in ["confused", "uncertain", "unsure"]):
|
| 578 |
cognitive_load_deltas.update({"processing_intensity": 0.2})
|
| 579 |
-
emotion_deltas.update({"curiosity": 0.2, "confidence": -0.1})
|
| 580 |
engagement_delta = 0.1
|
| 581 |
else:
|
| 582 |
emotion_deltas.update({"valence": 0.05, "arousal": 0.05})
|
|
@@ -589,14 +573,12 @@ class XylariaChat:
|
|
| 589 |
|
| 590 |
self.update_internal_state(emotion_deltas, cognitive_load_deltas, 0.1, engagement_delta)
|
| 591 |
|
| 592 |
-
|
| 593 |
self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
|
| 594 |
self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())
|
| 595 |
|
| 596 |
if len(self.conversation_history) > 10:
|
| 597 |
self.conversation_history = self.conversation_history[-10:]
|
| 598 |
|
| 599 |
-
|
| 600 |
custom_css = """
|
| 601 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
| 602 |
body, .gradio-container {
|
|
@@ -610,7 +592,6 @@ class XylariaChat:
|
|
| 610 |
.gradio-container button {
|
| 611 |
font-family: 'Inter', sans-serif !important;
|
| 612 |
}
|
| 613 |
-
/* Image Upload Styling */
|
| 614 |
.image-container {
|
| 615 |
display: flex;
|
| 616 |
gap: 10px;
|
|
@@ -627,11 +608,9 @@ class XylariaChat:
|
|
| 627 |
max-height: 200px;
|
| 628 |
border-radius: 8px;
|
| 629 |
}
|
| 630 |
-
/* Remove clear image buttons */
|
| 631 |
.clear-button {
|
| 632 |
display: none;
|
| 633 |
}
|
| 634 |
-
/* Animate chatbot messages */
|
| 635 |
.chatbot-container .message {
|
| 636 |
opacity: 0;
|
| 637 |
animation: fadeIn 0.5s ease-in-out forwards;
|
|
@@ -646,7 +625,6 @@ class XylariaChat:
|
|
| 646 |
transform: translateY(0);
|
| 647 |
}
|
| 648 |
}
|
| 649 |
-
/* Accordion Styling and Animation */
|
| 650 |
.gr-accordion-button {
|
| 651 |
background-color: #f0f0f0 !important;
|
| 652 |
border-radius: 8px !important;
|
|
@@ -669,9 +647,8 @@ class XylariaChat:
|
|
| 669 |
max-height: 0 !important;
|
| 670 |
}
|
| 671 |
.gr-accordion-active .gr-accordion-content {
|
| 672 |
-
max-height: 500px !important;
|
| 673 |
}
|
| 674 |
-
/* Accordion Animation - Upwards */
|
| 675 |
.gr-accordion {
|
| 676 |
display: flex;
|
| 677 |
flex-direction: column-reverse;
|
|
|
|
| 47 |
"strategy_adjustment": ""
|
| 48 |
}
|
| 49 |
|
|
|
|
| 50 |
self.internal_state = {
|
| 51 |
"emotions": {
|
| 52 |
+
"valence": 0.5,
|
| 53 |
+
"arousal": 0.5,
|
| 54 |
+
"dominance": 0.5,
|
| 55 |
+
"curiosity": 0.5,
|
| 56 |
+
"frustration": 0.0,
|
| 57 |
+
"confidence": 0.7,
|
| 58 |
+
"sadness": 0.0,
|
| 59 |
+
"joy": 0.0
|
| 60 |
},
|
| 61 |
"cognitive_load": {
|
| 62 |
+
"memory_load": 0.0,
|
| 63 |
+
"processing_intensity": 0.0
|
| 64 |
},
|
| 65 |
"introspection_level": 0.0,
|
| 66 |
+
"engagement_level": 0.5
|
| 67 |
}
|
| 68 |
|
|
|
|
| 69 |
self.goals = [
|
| 70 |
{"goal": "Provide helpful, informative, and contextually relevant responses", "priority": 0.8, "status": "active", "progress": 0.0},
|
| 71 |
{"goal": "Actively learn and adapt from interactions to improve conversational abilities", "priority": 0.9, "status": "active", "progress": 0.0},
|
| 72 |
{"goal": "Maintain a coherent, engaging, and empathetic conversation flow", "priority": 0.7, "status": "active", "progress": 0.0},
|
| 73 |
+
{"goal": "Identify and fill knowledge gaps by seeking external information", "priority": 0.6, "status": "dormant", "progress": 0.0},
|
| 74 |
+
{"goal": "Recognize and adapt to user's emotional state and adjust response style accordingly", "priority": 0.7, "status": "dormant", "progress": 0.0}
|
| 75 |
]
|
| 76 |
|
| 77 |
self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin. You should think step-by-step """
|
| 78 |
|
| 79 |
def update_internal_state(self, emotion_deltas, cognitive_load_deltas, introspection_delta, engagement_delta):
|
|
|
|
| 80 |
for emotion, delta in emotion_deltas.items():
|
| 81 |
if emotion in self.internal_state["emotions"]:
|
| 82 |
self.internal_state["emotions"][emotion] = np.clip(self.internal_state["emotions"][emotion] + delta, 0.0, 1.0)
|
| 83 |
|
|
|
|
| 84 |
for load_type, delta in cognitive_load_deltas.items():
|
| 85 |
if load_type in self.internal_state["cognitive_load"]:
|
| 86 |
self.internal_state["cognitive_load"][load_type] = np.clip(self.internal_state["cognitive_load"][load_type] + delta, 0.0, 1.0)
|
| 87 |
|
|
|
|
| 88 |
self.internal_state["introspection_level"] = np.clip(self.internal_state["introspection_level"] + introspection_delta, 0.0, 1.0)
|
| 89 |
self.internal_state["engagement_level"] = np.clip(self.internal_state["engagement_level"] + engagement_delta, 0.0, 1.0)
|
| 90 |
|
|
|
|
| 91 |
if self.internal_state["emotions"]["curiosity"] > 0.7 and self.goals[3]["status"] == "dormant":
|
| 92 |
+
self.goals[3]["status"] = "active"
|
| 93 |
if self.internal_state["engagement_level"] > 0.8 and self.goals[4]["status"] == "dormant":
|
| 94 |
+
self.goals[4]["status"] = "active"
|
| 95 |
|
| 96 |
def update_knowledge_graph(self, entities, relationships):
|
| 97 |
for entity in entities:
|
|
|
|
| 115 |
"bias_detection": bias_score,
|
| 116 |
"strategy_adjustment": strategy_adjustment
|
| 117 |
}
|
| 118 |
+
|
| 119 |
def calculate_coherence(self):
|
|
|
|
| 120 |
if not self.conversation_history:
|
| 121 |
return 0.95
|
| 122 |
|
|
|
|
| 132 |
|
| 133 |
average_coherence = np.mean(coherence_scores)
|
| 134 |
|
|
|
|
| 135 |
if self.internal_state["cognitive_load"]["processing_intensity"] > 0.8:
|
| 136 |
+
average_coherence -= 0.1
|
| 137 |
if self.internal_state["emotions"]["frustration"] > 0.5:
|
| 138 |
+
average_coherence -= 0.15
|
| 139 |
|
| 140 |
return np.clip(average_coherence, 0.0, 1.0)
|
| 141 |
|
| 142 |
def calculate_relevance(self):
|
|
|
|
| 143 |
if not self.conversation_history:
|
| 144 |
return 0.9
|
| 145 |
|
|
|
|
| 147 |
relevant_entities = self.extract_entities(last_user_message)
|
| 148 |
relevance_score = 0
|
| 149 |
|
|
|
|
| 150 |
for entity in relevant_entities:
|
| 151 |
if entity in self.knowledge_graph:
|
| 152 |
relevance_score += 0.2
|
| 153 |
|
|
|
|
| 154 |
for goal in self.goals:
|
| 155 |
if goal["status"] == "active":
|
| 156 |
if goal["goal"] == "Provide helpful, informative, and contextually relevant responses":
|
| 157 |
+
relevance_score += goal["priority"] * 0.5
|
| 158 |
elif goal["goal"] == "Identify and fill knowledge gaps by seeking external information":
|
| 159 |
if not relevant_entities or not all(entity in self.knowledge_graph for entity in relevant_entities):
|
| 160 |
+
relevance_score += goal["priority"] * 0.3
|
| 161 |
|
| 162 |
return np.clip(relevance_score, 0.0, 1.0)
|
| 163 |
|
| 164 |
def detect_bias(self):
|
|
|
|
| 165 |
bias_score = 0.0
|
| 166 |
|
|
|
|
| 167 |
recent_messages = [msg['content'] for msg in self.conversation_history[-3:] if msg['role'] == 'assistant']
|
| 168 |
if recent_messages:
|
| 169 |
average_valence = np.mean([self.embedding_model.encode(msg, convert_to_tensor=True).mean().item() for msg in recent_messages])
|
| 170 |
if average_valence < 0.4 or average_valence > 0.6:
|
| 171 |
+
bias_score += 0.2
|
| 172 |
|
|
|
|
| 173 |
if self.internal_state["emotions"]["valence"] < 0.3 or self.internal_state["emotions"]["valence"] > 0.7:
|
| 174 |
bias_score += 0.15
|
| 175 |
if self.internal_state["emotions"]["dominance"] > 0.8:
|
|
|
|
| 178 |
return np.clip(bias_score, 0.0, 1.0)
|
| 179 |
|
| 180 |
def suggest_strategy_adjustment(self):
|
|
|
|
| 181 |
adjustments = []
|
| 182 |
|
| 183 |
if self.metacognitive_layer["coherence_score"] < 0.7:
|
|
|
|
| 187 |
if self.metacognitive_layer["bias_detection"] > 0.3:
|
| 188 |
adjustments.append("Monitor and adjust responses to reduce potential biases. Consider rephrasing or providing alternative viewpoints.")
|
| 189 |
|
|
|
|
| 190 |
if self.internal_state["cognitive_load"]["memory_load"] > 0.8:
|
| 191 |
adjustments.append("Memory load is high. Consider summarizing or forgetting less relevant information.")
|
| 192 |
if self.internal_state["emotions"]["frustration"] > 0.6:
|
|
|
|
| 220 |
return introspection_report
|
| 221 |
|
| 222 |
def adjust_response_based_on_state(self, response):
|
|
|
|
| 223 |
if self.internal_state["introspection_level"] > 0.7:
|
| 224 |
response = self.introspect() + "\n\n" + response
|
| 225 |
|
|
|
|
| 228 |
curiosity = self.internal_state["emotions"]["curiosity"]
|
| 229 |
frustration = self.internal_state["emotions"]["frustration"]
|
| 230 |
confidence = self.internal_state["emotions"]["confidence"]
|
| 231 |
+
sadness = self.internal_state["emotions"]["sadness"]
|
| 232 |
+
joy = self.internal_state["emotions"]["joy"]
|
| 233 |
|
|
|
|
| 234 |
if valence < 0.4:
|
| 235 |
if arousal > 0.6:
|
| 236 |
response = "I'm feeling a bit overwhelmed right now, but I'll do my best to assist you. " + response
|
| 237 |
else:
|
| 238 |
+
if sadness > 0.6:
|
| 239 |
+
response = "I'm feeling quite down at the moment, but I'll try to help. " + response
|
| 240 |
+
else:
|
| 241 |
+
response = "I'm not feeling my best at the moment, but I'll try to help. " + response
|
| 242 |
+
|
| 243 |
elif valence > 0.6:
|
| 244 |
if arousal > 0.6:
|
| 245 |
+
if joy > 0.6:
|
| 246 |
+
response = "I'm feeling fantastic and ready to assist! " + response
|
| 247 |
+
else:
|
| 248 |
+
response = "I'm feeling quite energized and ready to assist! " + response
|
| 249 |
else:
|
| 250 |
response = "I'm in a good mood and happy to help. " + response
|
| 251 |
+
|
|
|
|
| 252 |
if curiosity > 0.7:
|
| 253 |
response += " I'm very curious about this topic, could you tell me more?"
|
| 254 |
if frustration > 0.5:
|
|
|
|
| 256 |
if confidence < 0.5:
|
| 257 |
response = "I'm not entirely sure about this, but here's what I think: " + response
|
| 258 |
|
|
|
|
| 259 |
if self.internal_state["cognitive_load"]["memory_load"] > 0.7:
|
| 260 |
response = "I'm holding a lot of information right now, so my response might be a bit brief: " + response
|
| 261 |
|
| 262 |
return response
|
| 263 |
|
| 264 |
def update_goals(self, user_feedback):
|
|
|
|
| 265 |
feedback_lower = user_feedback.lower()
|
| 266 |
|
|
|
|
| 267 |
if "helpful" in feedback_lower:
|
| 268 |
for goal in self.goals:
|
| 269 |
if goal["goal"] == "Provide helpful, informative, and contextually relevant responses":
|
|
|
|
| 275 |
goal["priority"] = max(goal["priority"] - 0.1, 0.0)
|
| 276 |
goal["progress"] = max(goal["progress"] - 0.2, 0.0)
|
| 277 |
|
|
|
|
| 278 |
if "learn more" in feedback_lower:
|
| 279 |
for goal in self.goals:
|
| 280 |
if goal["goal"] == "Actively learn and adapt from interactions to improve conversational abilities":
|
|
|
|
| 286 |
goal["priority"] = max(goal["priority"] - 0.1, 0.0)
|
| 287 |
goal["progress"] = max(goal["progress"] - 0.2, 0.0)
|
| 288 |
|
|
|
|
| 289 |
if self.internal_state["emotions"]["curiosity"] > 0.8:
|
| 290 |
for goal in self.goals:
|
| 291 |
if goal["goal"] == "Identify and fill knowledge gaps by seeking external information":
|
|
|
|
| 332 |
"dominance": 0.5,
|
| 333 |
"curiosity": 0.5,
|
| 334 |
"frustration": 0.0,
|
| 335 |
+
"confidence": 0.7,
|
| 336 |
+
"sadness": 0.0,
|
| 337 |
+
"joy": 0.0
|
| 338 |
},
|
| 339 |
"cognitive_load": {
|
| 340 |
"memory_load": 0.0,
|
|
|
|
| 468 |
return f"Error generating response: {str(e)}"
|
| 469 |
|
| 470 |
def extract_entities(self, text):
|
|
|
|
|
|
|
| 471 |
words = text.split()
|
| 472 |
entities = [word for word in words if word.isalpha() and word.istitle()]
|
| 473 |
return entities
|
| 474 |
|
| 475 |
def extract_relationships(self, text):
|
|
|
|
|
|
|
| 476 |
sentences = text.split('.')
|
| 477 |
relationships = []
|
| 478 |
for sentence in sentences:
|
|
|
|
| 482 |
if words[i].istitle() and words[i+2].istitle():
|
| 483 |
relationships.append((words[i], words[i+1], words[i+2]))
|
| 484 |
return relationships
|
| 485 |
+
|
| 486 |
def messages_to_prompt(self, messages):
|
| 487 |
prompt = ""
|
| 488 |
for msg in messages:
|
|
|
|
| 513 |
else:
|
| 514 |
response_stream = self.get_response(message)
|
| 515 |
|
|
|
|
| 516 |
if isinstance(response_stream, str):
|
| 517 |
updated_history = chat_history + [[message, response_stream]]
|
| 518 |
yield "", updated_history, None, None
|
|
|
|
| 539 |
|
| 540 |
self.update_goals(message)
|
| 541 |
|
|
|
|
| 542 |
emotion_deltas = {}
|
| 543 |
cognitive_load_deltas = {}
|
| 544 |
engagement_delta = 0
|
| 545 |
|
| 546 |
if any(word in message.lower() for word in ["sad", "unhappy", "depressed", "down"]):
|
| 547 |
+
emotion_deltas.update({"valence": -0.2, "arousal": 0.1, "confidence": -0.1, "sadness": 0.3, "joy": -0.2})
|
| 548 |
engagement_delta = -0.1
|
| 549 |
elif any(word in message.lower() for word in ["happy", "good", "great", "excited", "amazing"]):
|
| 550 |
+
emotion_deltas.update({"valence": 0.2, "arousal": 0.2, "confidence": 0.1, "sadness": -0.2, "joy": 0.3})
|
| 551 |
engagement_delta = 0.2
|
| 552 |
elif any(word in message.lower() for word in ["angry", "mad", "furious", "frustrated"]):
|
| 553 |
+
emotion_deltas.update({"valence": -0.3, "arousal": 0.3, "dominance": -0.2, "frustration": 0.2, "sadness": 0.1, "joy": -0.1})
|
| 554 |
engagement_delta = -0.2
|
| 555 |
elif any(word in message.lower() for word in ["scared", "afraid", "fearful", "anxious"]):
|
| 556 |
+
emotion_deltas.update({"valence": -0.2, "arousal": 0.4, "dominance": -0.3, "confidence": -0.2, "sadness": 0.2})
|
| 557 |
engagement_delta = -0.1
|
| 558 |
elif any(word in message.lower() for word in ["surprise", "amazed", "astonished"]):
|
| 559 |
+
emotion_deltas.update({"valence": 0.1, "arousal": 0.5, "dominance": 0.1, "curiosity": 0.3, "sadness": -0.1, "joy": 0.1})
|
| 560 |
engagement_delta = 0.3
|
| 561 |
elif any(word in message.lower() for word in ["confused", "uncertain", "unsure"]):
|
| 562 |
cognitive_load_deltas.update({"processing_intensity": 0.2})
|
| 563 |
+
emotion_deltas.update({"curiosity": 0.2, "confidence": -0.1, "sadness": 0.1})
|
| 564 |
engagement_delta = 0.1
|
| 565 |
else:
|
| 566 |
emotion_deltas.update({"valence": 0.05, "arousal": 0.05})
|
|
|
|
| 573 |
|
| 574 |
self.update_internal_state(emotion_deltas, cognitive_load_deltas, 0.1, engagement_delta)
|
| 575 |
|
|
|
|
| 576 |
self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
|
| 577 |
self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())
|
| 578 |
|
| 579 |
if len(self.conversation_history) > 10:
|
| 580 |
self.conversation_history = self.conversation_history[-10:]
|
| 581 |
|
|
|
|
| 582 |
custom_css = """
|
| 583 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
| 584 |
body, .gradio-container {
|
|
|
|
| 592 |
.gradio-container button {
|
| 593 |
font-family: 'Inter', sans-serif !important;
|
| 594 |
}
|
|
|
|
| 595 |
.image-container {
|
| 596 |
display: flex;
|
| 597 |
gap: 10px;
|
|
|
|
| 608 |
max-height: 200px;
|
| 609 |
border-radius: 8px;
|
| 610 |
}
|
|
|
|
| 611 |
.clear-button {
|
| 612 |
display: none;
|
| 613 |
}
|
|
|
|
| 614 |
.chatbot-container .message {
|
| 615 |
opacity: 0;
|
| 616 |
animation: fadeIn 0.5s ease-in-out forwards;
|
|
|
|
| 625 |
transform: translateY(0);
|
| 626 |
}
|
| 627 |
}
|
|
|
|
| 628 |
.gr-accordion-button {
|
| 629 |
background-color: #f0f0f0 !important;
|
| 630 |
border-radius: 8px !important;
|
|
|
|
| 647 |
max-height: 0 !important;
|
| 648 |
}
|
| 649 |
.gr-accordion-active .gr-accordion-content {
|
| 650 |
+
max-height: 500px !important;
|
| 651 |
}
|
|
|
|
| 652 |
.gr-accordion {
|
| 653 |
display: flex;
|
| 654 |
flex-direction: column-reverse;
|