Spaces:
Sleeping
Sleeping
Update agents/memory.py
#23
by Muthuraja18 - opened
- agents/memory.py +79 -35
agents/memory.py
CHANGED
|
@@ -1,27 +1,34 @@
|
|
| 1 |
class Memory:
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def __init__(self):
|
| 4 |
|
| 5 |
-
# Conversation
|
| 6 |
self.chat_history = []
|
| 7 |
|
| 8 |
-
# Uploaded
|
| 9 |
self.uploaded_files = []
|
| 10 |
|
| 11 |
-
# Current
|
| 12 |
-
self.
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
self.
|
| 16 |
|
| 17 |
-
#
|
| 18 |
self.analysis = None
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def add_message(self, role, content):
|
|
|
|
| 25 |
self.chat_history.append({
|
| 26 |
"role": role,
|
| 27 |
"content": content
|
|
@@ -30,44 +37,81 @@ class Memory:
|
|
| 30 |
def get_history(self):
|
| 31 |
return self.chat_history
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
-
#
|
| 36 |
|
| 37 |
def add_files(self, files):
|
| 38 |
-
self.uploaded_files.extend(files)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
# Image Context
|
| 42 |
-
# --------------------------
|
| 43 |
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
Store the complete image analysis.
|
| 47 |
-
"""
|
| 48 |
-
self.image_context = analysis
|
| 49 |
-
self.analysis = analysis
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
#
|
| 56 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def set_last_image(self, image_path):
|
|
|
|
| 59 |
self.last_used_image = image_path
|
|
|
|
| 60 |
|
| 61 |
def get_last_image(self):
|
|
|
|
| 62 |
return self.last_used_image
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
#
|
| 66 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def clear(self):
|
| 69 |
-
|
| 70 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
self.last_used_image = None
|
| 72 |
-
|
| 73 |
-
self.analysis = None
|
|
|
|
|
|
|
|
|
| 1 |
class Memory:
|
| 2 |
+
"""
|
| 3 |
+
Stores conversation history and image context for one user session.
|
| 4 |
+
"""
|
| 5 |
|
| 6 |
def __init__(self):
|
| 7 |
|
| 8 |
+
# Conversation
|
| 9 |
self.chat_history = []
|
| 10 |
|
| 11 |
+
# Uploaded image paths
|
| 12 |
self.uploaded_files = []
|
| 13 |
|
| 14 |
+
# Current working image
|
| 15 |
+
self.current_image = None
|
| 16 |
|
| 17 |
+
# Latest edited image
|
| 18 |
+
self.last_used_image = None
|
| 19 |
|
| 20 |
+
# Full image analysis
|
| 21 |
self.analysis = None
|
| 22 |
|
| 23 |
+
# Natural language description
|
| 24 |
+
self.image_context = ""
|
| 25 |
+
|
| 26 |
+
# =====================================================
|
| 27 |
+
# CHAT
|
| 28 |
+
# =====================================================
|
| 29 |
|
| 30 |
def add_message(self, role, content):
|
| 31 |
+
|
| 32 |
self.chat_history.append({
|
| 33 |
"role": role,
|
| 34 |
"content": content
|
|
|
|
| 37 |
def get_history(self):
|
| 38 |
return self.chat_history
|
| 39 |
|
| 40 |
+
# =====================================================
|
| 41 |
+
# FILES
|
| 42 |
+
# =====================================================
|
| 43 |
|
| 44 |
def add_files(self, files):
|
|
|
|
| 45 |
|
| 46 |
+
self.uploaded_files = files
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
if files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
self.current_image = files[0]
|
| 51 |
+
self.last_used_image = files[0]
|
| 52 |
+
|
| 53 |
+
def get_files(self):
|
| 54 |
+
|
| 55 |
+
return self.uploaded_files
|
| 56 |
|
| 57 |
+
# =====================================================
|
| 58 |
+
# IMAGE
|
| 59 |
+
# =====================================================
|
| 60 |
+
|
| 61 |
+
def set_current_image(self, image_path):
|
| 62 |
+
|
| 63 |
+
self.current_image = image_path
|
| 64 |
+
|
| 65 |
+
def get_current_image(self):
|
| 66 |
+
|
| 67 |
+
return self.current_image
|
| 68 |
|
| 69 |
def set_last_image(self, image_path):
|
| 70 |
+
|
| 71 |
self.last_used_image = image_path
|
| 72 |
+
self.current_image = image_path
|
| 73 |
|
| 74 |
def get_last_image(self):
|
| 75 |
+
|
| 76 |
return self.last_used_image
|
| 77 |
|
| 78 |
+
# =====================================================
|
| 79 |
+
# ANALYSIS
|
| 80 |
+
# =====================================================
|
| 81 |
+
|
| 82 |
+
def set_analysis(self, analysis):
|
| 83 |
+
|
| 84 |
+
self.analysis = analysis
|
| 85 |
+
|
| 86 |
+
if isinstance(analysis, dict):
|
| 87 |
+
|
| 88 |
+
self.image_context = analysis.get(
|
| 89 |
+
"description",
|
| 90 |
+
""
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
def get_analysis(self):
|
| 94 |
+
|
| 95 |
+
return self.analysis
|
| 96 |
+
|
| 97 |
+
def get_image_context(self):
|
| 98 |
+
|
| 99 |
+
return self.image_context
|
| 100 |
+
|
| 101 |
+
# =====================================================
|
| 102 |
+
# CLEAR
|
| 103 |
+
# =====================================================
|
| 104 |
|
| 105 |
def clear(self):
|
| 106 |
+
|
| 107 |
+
self.chat_history = []
|
| 108 |
+
|
| 109 |
+
self.uploaded_files = []
|
| 110 |
+
|
| 111 |
+
self.current_image = None
|
| 112 |
+
|
| 113 |
self.last_used_image = None
|
| 114 |
+
|
| 115 |
+
self.analysis = None
|
| 116 |
+
|
| 117 |
+
self.image_context = ""
|