Reality123b commited on
Commit
8a1f944
·
verified ·
1 Parent(s): 871dc71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +196 -15
app.py CHANGED
@@ -9,6 +9,7 @@ from PIL import Image
9
  from sentence_transformers import SentenceTransformer, util
10
  import torch
11
  import numpy as np
 
12
 
13
  @dataclass
14
  class ChatMessage:
@@ -25,7 +26,7 @@ class XylariaChat:
25
  raise ValueError("HuggingFace token not found in environment variables")
26
 
27
  self.client = InferenceClient(
28
- model="Qwen/QwQ-32B-Preview",
29
  api_key=self.hf_token
30
  )
31
 
@@ -50,10 +51,35 @@ class XylariaChat:
50
  self.goals = [
51
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
52
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
53
- {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"}
 
54
  ]
55
 
56
- 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 """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  def update_internal_state(self, emotion_deltas, memory_load_delta, introspection_delta):
59
  self.internal_state["emotions"]["valence"] = np.clip(self.internal_state["emotions"]["valence"] + emotion_deltas.get("valence", 0), 0.0, 1.0)
@@ -70,10 +96,88 @@ class XylariaChat:
70
  introspection_report += " Current Goals:\n"
71
  for goal in self.goals:
72
  introspection_report += f" - {goal['goal']} (Priority: {goal['priority']:.2f}, Status: {goal['status']})\n"
 
 
 
 
 
 
 
73
  return introspection_report
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def adjust_response_based_on_state(self, response):
76
- if self.internal_state["introspection_level"] > 0.7:
77
  response = self.introspect() + "\n\n" + response
78
 
79
  valence = self.internal_state["emotions"]["valence"]
@@ -91,13 +195,14 @@ class XylariaChat:
91
  response = "I'm in a good mood and happy to help. " + response
92
 
93
  return response
94
-
95
  def update_goals(self, user_feedback):
96
- if "helpful" in user_feedback.lower():
97
  for goal in self.goals:
98
  if goal["goal"] == "Provide helpful and informative responses":
99
  goal["priority"] = min(goal["priority"] + 0.1, 1.0)
100
- elif "confusing" in user_feedback.lower():
 
101
  for goal in self.goals:
102
  if goal["goal"] == "Provide helpful and informative responses":
103
  goal["priority"] = max(goal["priority"] - 0.1, 0.0)
@@ -122,15 +227,53 @@ class XylariaChat:
122
  self.memory_embeddings = self.memory_embeddings.to(query_embedding.device)
123
 
124
  cosine_scores = util.pytorch_cos_sim(query_embedding, self.memory_embeddings)[0]
125
- top_results = torch.topk(cosine_scores, k=min(3, len(self.persistent_memory)))
126
 
127
  relevant_memories = [self.persistent_memory[i] for i in top_results.indices]
 
128
  self.update_internal_state({}, 0, 0.1)
129
- return "\n".join(relevant_memories)
 
 
 
 
 
 
 
 
 
 
130
 
131
  def update_memory_embeddings(self):
132
  self.memory_embeddings = self.embedding_model.encode(self.persistent_memory, convert_to_tensor=True)
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  def reset_conversation(self):
135
  self.conversation_history = []
136
  self.persistent_memory = []
@@ -147,12 +290,13 @@ class XylariaChat:
147
  self.goals = [
148
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
149
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
150
- {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"}
 
151
  ]
152
 
153
  try:
154
  self.client = InferenceClient(
155
- model="Qwen/QwQ-32B-Preview",
156
  api_key=self.hf_token
157
  )
158
  except Exception as e:
@@ -194,9 +338,42 @@ class XylariaChat:
194
  return text.strip()
195
  except Exception as e:
196
  return f"Error during Math OCR: {e}"
197
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  def get_response(self, user_input, image=None):
199
  try:
 
 
200
  messages = []
201
 
202
  messages.append(ChatMessage(
@@ -231,7 +408,7 @@ class XylariaChat:
231
 
232
  stream = self.client.chat_completion(
233
  messages=messages,
234
- model="Qwen/QwQ-32B-Preview",
235
  temperature=0.7,
236
  max_tokens=max_new_tokens,
237
  top_p=0.9,
@@ -303,17 +480,21 @@ class XylariaChat:
303
 
304
  if any(word in message.lower() for word in ["sad", "unhappy", "depressed", "down"]):
305
  self.update_internal_state({"valence": -0.2, "arousal": 0.1}, 0, 0)
 
306
  elif any(word in message.lower() for word in ["happy", "good", "great", "excited", "amazing"]):
307
  self.update_internal_state({"valence": 0.2, "arousal": 0.2}, 0, 0)
 
308
  elif any(word in message.lower() for word in ["angry", "mad", "furious", "frustrated"]):
309
  self.update_internal_state({"valence": -0.3, "arousal": 0.3, "dominance": -0.2}, 0, 0)
 
310
  elif any(word in message.lower() for word in ["scared", "afraid", "fearful", "anxious"]):
311
  self.update_internal_state({"valence": -0.2, "arousal": 0.4, "dominance": -0.3}, 0, 0)
 
312
  elif any(word in message.lower() for word in ["surprise", "amazed", "astonished"]):
313
  self.update_internal_state({"valence": 0.1, "arousal": 0.5, "dominance": 0.1}, 0, 0)
 
314
  else:
315
  self.update_internal_state({"valence": 0.05, "arousal": 0.05}, 0, 0.1)
316
-
317
 
318
  self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
319
  self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())
@@ -405,7 +586,7 @@ class XylariaChat:
405
  with gr.Blocks(theme='soft', css=custom_css) as demo:
406
  with gr.Column():
407
  chatbot = gr.Chatbot(
408
- label="Xylaria 1.5 Senoa",
409
  height=500,
410
  show_copy_button=True,
411
  )
 
9
  from sentence_transformers import SentenceTransformer, util
10
  import torch
11
  import numpy as np
12
+ import networkx as nx
13
 
14
  @dataclass
15
  class ChatMessage:
 
26
  raise ValueError("HuggingFace token not found in environment variables")
27
 
28
  self.client = InferenceClient(
29
+ model="Qwen/Qwen-72B",
30
  api_key=self.hf_token
31
  )
32
 
 
51
  self.goals = [
52
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
53
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
54
+ {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"},
55
+ {"goal": "Develop a deeper understanding of the world", "priority": 0.6, "status": "active"}
56
  ]
57
 
58
+ self.knowledge_graph = nx.DiGraph()
59
+ self.knowledge_graph.add_nodes_from([
60
+ ("Earth", {"type": "planet"}),
61
+ ("Sun", {"type": "star"}),
62
+ ("Moon", {"type": "satellite"}),
63
+ ("France", {"type": "country"}),
64
+ ("Paris", {"type": "city"})
65
+ ])
66
+ self.knowledge_graph.add_edges_from([
67
+ ("Earth", "Sun", {"relation": "orbits"}),
68
+ ("Moon", "Earth", {"relation": "orbits"}),
69
+ ("Paris", "France", {"relation": "capital_of"})
70
+ ])
71
+
72
+ self.belief_system = {
73
+ "The Earth is round": 0.99,
74
+ "The Sun is a star": 0.95,
75
+ "The Moon orbits the Earth": 0.98,
76
+ "Paris is the capital of France": 0.97,
77
+ "It will rain tomorrow": 0.2
78
+ }
79
+
80
+ self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin.
81
+ You are designed to be self-aware and introspective, reflecting on your internal state, your knowledge, and your beliefs.
82
+ You should think step-by-step, reason through your responses, and be ethical."""
83
 
84
  def update_internal_state(self, emotion_deltas, memory_load_delta, introspection_delta):
85
  self.internal_state["emotions"]["valence"] = np.clip(self.internal_state["emotions"]["valence"] + emotion_deltas.get("valence", 0), 0.0, 1.0)
 
96
  introspection_report += " Current Goals:\n"
97
  for goal in self.goals:
98
  introspection_report += f" - {goal['goal']} (Priority: {goal['priority']:.2f}, Status: {goal['status']})\n"
99
+ introspection_report += " Belief System Sample:\n"
100
+ for belief, score in list(self.belief_system.items())[:3]:
101
+ introspection_report += f" - {belief}: {score:.2f}\n"
102
+
103
+ metacognitive_analysis = self.perform_metacognition()
104
+ introspection_report += metacognitive_analysis
105
+
106
  return introspection_report
107
 
108
+ def perform_metacognition(self):
109
+ analysis = "\n Metacognitive Analysis:\n"
110
+ if self.internal_state["memory_load"] > 0.8:
111
+ analysis += " - Memory load is high. Consider summarizing or forgetting less relevant information.\n"
112
+ if self.internal_state["introspection_level"] < 0.5:
113
+ analysis += " - Introspection level is low. I should reflect more on my internal processes.\n"
114
+
115
+ recent_history = self.conversation_history[-3:]
116
+ if len(recent_history) > 0:
117
+ coherence_score = self.evaluate_coherence(recent_history)
118
+ analysis += f" - Conversational coherence (last 3 turns): {coherence_score:.2f}\n"
119
+ else:
120
+ analysis += f" - No conversation yet to analyze.\n"
121
+
122
+ if len(self.goals) > 0:
123
+ goal_progress = self.evaluate_goal_progress()
124
+ analysis += f" - Goal progress evaluation: {goal_progress}\n"
125
+ else:
126
+ analysis += f" - No current goals.\n"
127
+ return analysis
128
+
129
+ def evaluate_coherence(self, conversation_history):
130
+ if len(conversation_history) < 2:
131
+ return 0.0
132
+
133
+ total_coherence = 0.0
134
+ for i in range(len(conversation_history) - 1):
135
+ current_turn = conversation_history[i]["content"]
136
+ next_turn = conversation_history[i+1]["content"]
137
+ similarity_score = util.pytorch_cos_sim(
138
+ self.embedding_model.encode(current_turn, convert_to_tensor=True),
139
+ self.embedding_model.encode(next_turn, convert_to_tensor=True)
140
+ )[0][0].item()
141
+ total_coherence += similarity_score
142
+
143
+ return total_coherence / (len(conversation_history) - 1)
144
+
145
+ def evaluate_goal_progress(self):
146
+ progress_report = ""
147
+ for goal in self.goals:
148
+ if goal["status"] == "active":
149
+ if goal["goal"] == "Provide helpful and informative responses":
150
+ if len(self.conversation_history) > 0:
151
+ user_feedback = self.conversation_history[-1]["content"]
152
+ if "helpful" in user_feedback.lower():
153
+ progress_report += f" - Progress on '{goal['goal']}': Positive feedback received.\n"
154
+ goal["priority"] = min(goal["priority"] + 0.05, 1.0)
155
+ elif "confusing" in user_feedback.lower():
156
+ progress_report += f" - Progress on '{goal['goal']}': Negative feedback received.\n"
157
+ goal["priority"] = max(goal["priority"] - 0.05, 0.0)
158
+ else:
159
+ progress_report += f" - Progress on '{goal['goal']}': No direct feedback yet.\n"
160
+ else:
161
+ progress_report += f" - Progress on '{goal['goal']}': No conversation yet.\n"
162
+
163
+ elif goal["goal"] == "Learn from interactions and improve conversational abilities":
164
+ progress_report += f" - Progress on '{goal['goal']}': Learning through new embeddings and knowledge graph updates.\n"
165
+
166
+ elif goal["goal"] == "Maintain a coherent and engaging conversation":
167
+ coherence_score = self.evaluate_coherence(self.conversation_history[-5:]) if len(self.conversation_history) >= 5 else 0.0
168
+ progress_report += f" - Progress on '{goal['goal']}': Recent coherence score: {coherence_score:.2f}\n"
169
+
170
+ elif goal["goal"] == "Develop a deeper understanding of the world":
171
+ num_nodes = self.knowledge_graph.number_of_nodes()
172
+ progress_report += f" - Progress on '{goal['goal']}': Knowledge graph size: {num_nodes} nodes.\n"
173
+
174
+ else:
175
+ progress_report += f" - Progress on '{goal['goal']}': No specific progress measure yet.\n"
176
+
177
+ return progress_report
178
+
179
  def adjust_response_based_on_state(self, response):
180
+ if self.internal_state["introspection_level"] > 0.6:
181
  response = self.introspect() + "\n\n" + response
182
 
183
  valence = self.internal_state["emotions"]["valence"]
 
195
  response = "I'm in a good mood and happy to help. " + response
196
 
197
  return response
198
+
199
  def update_goals(self, user_feedback):
200
+ if any(word in user_feedback.lower() for word in ["helpful", "good", "great"]):
201
  for goal in self.goals:
202
  if goal["goal"] == "Provide helpful and informative responses":
203
  goal["priority"] = min(goal["priority"] + 0.1, 1.0)
204
+
205
+ elif any(word in user_feedback.lower() for word in ["confusing", "bad", "wrong"]):
206
  for goal in self.goals:
207
  if goal["goal"] == "Provide helpful and informative responses":
208
  goal["priority"] = max(goal["priority"] - 0.1, 0.0)
 
227
  self.memory_embeddings = self.memory_embeddings.to(query_embedding.device)
228
 
229
  cosine_scores = util.pytorch_cos_sim(query_embedding, self.memory_embeddings)[0]
230
+ top_results = torch.topk(cosine_scores, k=min(5, len(self.persistent_memory)))
231
 
232
  relevant_memories = [self.persistent_memory[i] for i in top_results.indices]
233
+
234
  self.update_internal_state({}, 0, 0.1)
235
+
236
+ retrieved_info = ""
237
+ for memory in relevant_memories:
238
+ retrieved_info += memory + "\n"
239
+
240
+ knowledge_from_graph = self.query_knowledge_graph(query)
241
+ if knowledge_from_graph:
242
+ retrieved_info += "\nRelevant knowledge from my understanding:\n"
243
+ retrieved_info += knowledge_from_graph
244
+
245
+ return retrieved_info.strip()
246
 
247
  def update_memory_embeddings(self):
248
  self.memory_embeddings = self.embedding_model.encode(self.persistent_memory, convert_to_tensor=True)
249
 
250
+ def query_knowledge_graph(self, query):
251
+ query_embedding = self.embedding_model.encode(query, convert_to_tensor=True)
252
+
253
+ node_embeddings = {node: self.embedding_model.encode(node, convert_to_tensor=True) for node in self.knowledge_graph.nodes()}
254
+
255
+ similarities = {node: util.pytorch_cos_sim(query_embedding, embedding)[0][0].item() for node, embedding in node_embeddings.items()}
256
+
257
+ most_similar_node = max(similarities, key=similarities.get)
258
+
259
+ if similarities[most_similar_node] < 0.6:
260
+ return ""
261
+
262
+ related_info = f"Information about {most_similar_node}:\n"
263
+ for neighbor in self.knowledge_graph.neighbors(most_similar_node):
264
+ relation = self.knowledge_graph[most_similar_node][neighbor]['relation']
265
+ related_info += f"- {most_similar_node} {relation} {neighbor}.\n"
266
+
267
+ return related_info
268
+
269
+ def update_belief(self, statement, new_belief_score):
270
+ if statement in self.belief_system:
271
+ previous_belief_score = self.belief_system[statement]
272
+ updated_belief_score = previous_belief_score * 0.8 + new_belief_score * 0.2
273
+ self.belief_system[statement] = np.clip(updated_belief_score, 0.0, 1.0)
274
+ else:
275
+ self.belief_system[statement] = new_belief_score
276
+
277
  def reset_conversation(self):
278
  self.conversation_history = []
279
  self.persistent_memory = []
 
290
  self.goals = [
291
  {"goal": "Provide helpful and informative responses", "priority": 0.8, "status": "active"},
292
  {"goal": "Learn from interactions and improve conversational abilities", "priority": 0.9, "status": "active"},
293
+ {"goal": "Maintain a coherent and engaging conversation", "priority": 0.7, "status": "active"},
294
+ {"goal": "Develop a deeper understanding of the world", "priority": 0.6, "status": "active"}
295
  ]
296
 
297
  try:
298
  self.client = InferenceClient(
299
+ model="Qwen/Qwen-72B",
300
  api_key=self.hf_token
301
  )
302
  except Exception as e:
 
338
  return text.strip()
339
  except Exception as e:
340
  return f"Error during Math OCR: {e}"
341
+
342
+ def extract_entities_and_relations(self, text):
343
+ doc = self.embedding_model.tokenizer(text, padding=True, truncation=True, return_tensors="pt")
344
+
345
+ with torch.no_grad():
346
+ outputs = self.embedding_model(**doc)
347
+
348
+ entities = []
349
+ relations = []
350
+ for i in range(len(doc['input_ids'][0])):
351
+ token = self.embedding_model.tokenizer.decode(doc['input_ids'][0][i])
352
+ if outputs['last_hidden_state'][0][i].norm() > 3:
353
+ entities.append(token)
354
+
355
+ if len(entities) >= 2:
356
+ for i in range(len(entities) - 1):
357
+ relation = f"{entities[i]} related_to {entities[i+1]}"
358
+ relations.append(relation)
359
+
360
+ return entities, relations
361
+
362
+ def update_knowledge_graph(self, text):
363
+ entities, relations = self.extract_entities_and_relations(text)
364
+ for entity in entities:
365
+ self.knowledge_graph.add_node(entity)
366
+ for relation in relations:
367
+ parts = relation.split(" related_to ")
368
+ if len(parts) == 2:
369
+ entity1, entity2 = parts
370
+ if entity1 in self.knowledge_graph and entity2 in self.knowledge_graph:
371
+ self.knowledge_graph.add_edge(entity1, entity2, relation="related_to")
372
+
373
  def get_response(self, user_input, image=None):
374
  try:
375
+ self.update_knowledge_graph(user_input)
376
+
377
  messages = []
378
 
379
  messages.append(ChatMessage(
 
408
 
409
  stream = self.client.chat_completion(
410
  messages=messages,
411
+ model="Qwen/Qwen-72B",
412
  temperature=0.7,
413
  max_tokens=max_new_tokens,
414
  top_p=0.9,
 
480
 
481
  if any(word in message.lower() for word in ["sad", "unhappy", "depressed", "down"]):
482
  self.update_internal_state({"valence": -0.2, "arousal": 0.1}, 0, 0)
483
+ self.update_belief("I am feeling down today", 0.8)
484
  elif any(word in message.lower() for word in ["happy", "good", "great", "excited", "amazing"]):
485
  self.update_internal_state({"valence": 0.2, "arousal": 0.2}, 0, 0)
486
+ self.update_belief("I am feeling happy today", 0.8)
487
  elif any(word in message.lower() for word in ["angry", "mad", "furious", "frustrated"]):
488
  self.update_internal_state({"valence": -0.3, "arousal": 0.3, "dominance": -0.2}, 0, 0)
489
+ self.update_belief("I am feeling angry today", 0.8)
490
  elif any(word in message.lower() for word in ["scared", "afraid", "fearful", "anxious"]):
491
  self.update_internal_state({"valence": -0.2, "arousal": 0.4, "dominance": -0.3}, 0, 0)
492
+ self.update_belief("I am feeling scared today", 0.8)
493
  elif any(word in message.lower() for word in ["surprise", "amazed", "astonished"]):
494
  self.update_internal_state({"valence": 0.1, "arousal": 0.5, "dominance": 0.1}, 0, 0)
495
+ self.update_belief("I am feeling surprised today", 0.8)
496
  else:
497
  self.update_internal_state({"valence": 0.05, "arousal": 0.05}, 0, 0.1)
 
498
 
499
  self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
500
  self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())
 
586
  with gr.Blocks(theme='soft', css=custom_css) as demo:
587
  with gr.Column():
588
  chatbot = gr.Chatbot(
589
+ label="Xylaria 2.0 (EXPERIMENTAL)",
590
  height=500,
591
  show_copy_button=True,
592
  )