-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_visualizer.py
More file actions
340 lines (300 loc) · 16.1 KB
/
memory_visualizer.py
File metadata and controls
340 lines (300 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import json
from flask import Flask, render_template
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def memory_visualizer():
# Load memory data from JSON file
try:
with open('data/memory_bank.json', 'r') as f:
memory_data = json.load(f)
except Exception as e:
return f"Error loading memory bank: {str(e)}"
# Extract all types of attributes
original_attributes = memory_data.get('original_attributes', [])
combined_attributes = memory_data.get('combined_attributes', [])
inferred_attributes = memory_data.get('inferred_attributes', [])
assert original_attributes, "No original attributes found"
assert combined_attributes, "No combined attributes found"
assert inferred_attributes, "No inferred attributes found"
# Process all attributes
all_attributes = []
# Process original attributes
for attr in original_attributes:
attr_copy = attr.copy()
attr_copy['type'] = 'original' # Add type label
# Preserve conversation content for display
if 'conversation_content' in attr_copy:
attr_copy['has_conversation'] = True
else:
attr_copy['has_conversation'] = False
if 'timestamp' in attr_copy:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M:%S')
all_attributes.append(attr_copy)
except ValueError:
try:
# Try alternative format
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M')
all_attributes.append(attr_copy)
except ValueError:
# If timestamp format is different, use a default date
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
# Process combined attributes
for attr in combined_attributes:
attr_copy = attr.copy()
attr_copy['type'] = 'combined' # Add type label
# Check if source attributes have conversation content
attr_copy['has_conversation'] = False
if 'source_attributes' in attr_copy:
for source_attr in attr_copy['source_attributes']:
if 'conversation_content' in source_attr:
attr_copy['has_conversation'] = True
break
# For combined attributes, try to get timestamp from source_attributes
if 'timestamp' not in attr_copy and 'source_attributes' in attr_copy:
# Find the earliest timestamp from source attributes
earliest_timestamp = None
for source_attr in attr_copy['source_attributes']:
if 'timestamp' in source_attr:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M:%S')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
continue
if 'timestamp' in attr_copy:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M:%S')
all_attributes.append(attr_copy)
except ValueError:
try:
# Try alternative format
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M')
all_attributes.append(attr_copy)
except ValueError:
# If timestamp format is different, use a default date
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
else:
# If no timestamp found, use default date
print(f"No timestamp found for attribute: {attr_copy['attribute']}")
# Process inferred attributes
for attr in inferred_attributes:
attr_copy = attr.copy()
attr_copy['type'] = 'inferred' # Add type label
# Check if source attributes have conversation content
attr_copy['has_conversation'] = False
if 'source_attributes' in attr_copy:
for source_attr in attr_copy['source_attributes']:
if 'conversation_content' in source_attr:
attr_copy['has_conversation'] = True
break
# For inferred attributes, try to get timestamp from source_attributes (same as combined)
if 'timestamp' not in attr_copy and 'source_attributes' in attr_copy:
# Find the earliest timestamp from source attributes
earliest_timestamp = None
for source_attr in attr_copy['source_attributes']:
if 'timestamp' in source_attr:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M:%S')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
continue
if 'timestamp' in attr_copy:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M:%S')
all_attributes.append(attr_copy)
except ValueError:
try:
# Try alternative format
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M')
all_attributes.append(attr_copy)
except ValueError:
# If timestamp format is different, use a default date
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
else:
# If no timestamp found, use default date
print(f"No timestamp found for attribute: {attr_copy['attribute']}")
# Sort all attributes by timestamp (newest first)
sorted_attributes = sorted(all_attributes, key=lambda x: x.get('datetime_obj', datetime.min), reverse=True)
# Add global index to each attribute for conversation lookup
for i, attr in enumerate(sorted_attributes):
attr['global_index'] = i
# Group attributes by date for better visualization
grouped_memories = {}
for attr in sorted_attributes:
date_str = attr['datetime_obj'].strftime('%Y-%m-%d')
if date_str not in grouped_memories:
grouped_memories[date_str] = []
grouped_memories[date_str].append(attr)
# Store the sorted attributes in the session for conversation view
app.config['sorted_attributes'] = sorted_attributes
return render_template('memory_visualizer.html', grouped_memories=grouped_memories)
@app.route('/conversation/<int:attr_index>')
def view_conversation(attr_index):
# Get the sorted attributes from the app config
sorted_attributes = app.config.get('sorted_attributes', [])
if not sorted_attributes:
# If sorted_attributes is not available, recreate it
try:
with open('data/combined_attributes.json', 'r') as f:
memory_data = json.load(f)
except Exception as e:
return f"Error loading memory bank: {str(e)}"
# Extract all types of attributes
original_attributes = memory_data.get('original_attributes', [])
combined_attributes = memory_data.get('combined_attributes', [])
inferred_attributes = memory_data.get('inferred_attributes', [])
# Process all attributes - using the EXACT same logic as in memory_visualizer
all_attributes = []
# Process original attributes
for attr in original_attributes:
attr_copy = attr.copy()
attr_copy['type'] = 'original'
if 'conversation_content' in attr_copy:
attr_copy['has_conversation'] = True
else:
attr_copy['has_conversation'] = False
if 'timestamp' in attr_copy:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M:%S')
all_attributes.append(attr_copy)
except ValueError:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M')
all_attributes.append(attr_copy)
except ValueError:
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
# Process combined attributes
for attr in combined_attributes:
attr_copy = attr.copy()
attr_copy['type'] = 'combined'
attr_copy['has_conversation'] = False
if 'source_attributes' in attr_copy:
for source_attr in attr_copy['source_attributes']:
if 'conversation_content' in source_attr:
attr_copy['has_conversation'] = True
break
if 'timestamp' not in attr_copy and 'source_attributes' in attr_copy:
earliest_timestamp = None
for source_attr in attr_copy['source_attributes']:
if 'timestamp' in source_attr:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M:%S')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
continue
if 'timestamp' in attr_copy:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M:%S')
all_attributes.append(attr_copy)
except ValueError:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M')
all_attributes.append(attr_copy)
except ValueError:
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
else:
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
# Process inferred attributes
for attr in inferred_attributes:
attr_copy = attr.copy()
attr_copy['type'] = 'inferred'
attr_copy['has_conversation'] = False
if 'source_attributes' in attr_copy:
for source_attr in attr_copy['source_attributes']:
if 'conversation_content' in source_attr:
attr_copy['has_conversation'] = True
break
if 'timestamp' not in attr_copy and 'source_attributes' in attr_copy:
earliest_timestamp = None
for source_attr in attr_copy['source_attributes']:
if 'timestamp' in source_attr:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M:%S')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
try:
timestamp_obj = datetime.strptime(source_attr['timestamp'], '%Y-%m-%d %H:%M')
if earliest_timestamp is None or timestamp_obj < earliest_timestamp:
earliest_timestamp = timestamp_obj
attr_copy['timestamp'] = source_attr['timestamp']
except ValueError:
continue
if 'timestamp' in attr_copy:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M:%S')
all_attributes.append(attr_copy)
except ValueError:
try:
attr_copy['datetime_obj'] = datetime.strptime(attr_copy['timestamp'], '%Y-%m-%d %H:%M')
all_attributes.append(attr_copy)
except ValueError:
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
else:
attr_copy['datetime_obj'] = datetime.min
all_attributes.append(attr_copy)
# Sort all attributes by timestamp (newest first) - EXACT same sorting as in memory_visualizer
sorted_attributes = sorted(all_attributes, key=lambda x: x.get('datetime_obj', datetime.min), reverse=True)
if attr_index < 0 or attr_index >= len(sorted_attributes):
return "Attribute index out of range"
attribute = sorted_attributes[attr_index]
# Get conversation content
conversation = None
if 'conversation_content' in attribute:
conversation = attribute['conversation_content']
elif 'source_attributes' in attribute:
# For combined/inferred attributes, get conversations from source attributes
conversations = []
for source_attr in attribute['source_attributes']:
if 'conversation_content' in source_attr:
conversations.append({
'attribute': source_attr.get('attribute', 'Unknown'),
'content': source_attr['conversation_content']
})
if conversations:
return render_template('conversation_view.html',
attribute=attribute,
multiple_conversations=True,
conversations=conversations)
if conversation:
return render_template('conversation_view.html',
attribute=attribute,
multiple_conversations=False,
conversation=conversation)
else:
return "No conversation content found for this attribute"
if __name__ == '__main__':
app.run(debug=True)