Update agents/image_agent.py

#31
Files changed (1) hide show
  1. agents/image_agent.py +129 -32
agents/image_agent.py CHANGED
@@ -11,32 +11,42 @@ class ImageAgent:
11
  self.memory = memory
12
  self.planner = planner
13
 
14
- # --------------------------
15
  # Upload Files
16
- # --------------------------
 
17
  def upload(self, files):
18
 
19
  uploaded = save_uploads(files)
20
 
21
  self.memory.uploaded_files = uploaded
22
 
23
- # Automatically choose first uploaded image
 
 
24
  for file in uploaded:
25
- if file["type"] and file["type"].startswith("image/"):
 
 
26
  self.memory.last_used_image = file["path"]
 
27
  break
28
 
29
  return uploaded
30
 
31
- # --------------------------
32
  # Analyze Image
33
- # --------------------------
 
34
  def auto_analyze(self, image_path=None):
35
 
36
- image = self.memory.last_used_image
37
 
38
  if not image:
39
- return None
 
 
 
40
 
41
  analysis = analyze_image(image)
42
 
@@ -45,46 +55,123 @@ class ImageAgent:
45
 
46
  return analysis
47
 
48
- # --------------------------
49
- # Main Agent
50
- # --------------------------
51
- def run(self, user_input):
52
 
53
- task = self.router.route(user_input)
54
 
55
- steps = self.planner.plan(task)
 
56
 
57
- result = None
58
 
59
- image = self.memory.last_used_image
60
 
61
- # Automatically analyze uploaded image once
62
- if image and self.memory.image_context is None:
63
- self.auto_analyze()
64
 
65
- analysis = self.memory.image_context
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- context = ""
 
68
 
69
- if analysis:
70
 
71
- context = f"""
 
 
 
72
  Image Description:
73
- {analysis["description"]}
74
 
75
  Objects:
76
- {", ".join(analysis.get("objects", []))}
 
 
 
77
 
78
  Style:
79
- {analysis.get("style", "")}
80
 
81
  Lighting:
82
- {analysis.get("lighting", "")}
 
 
 
 
 
 
83
 
84
- OCR:
85
- {analysis.get("text", "")}
 
 
 
 
 
 
 
 
 
86
  """
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  prompt = f"""
89
  {context}
90
 
@@ -92,6 +179,8 @@ User Request:
92
  {user_input}
93
  """
94
 
 
 
95
  for step in steps:
96
 
97
  if step == "generate":
@@ -105,12 +194,20 @@ User Request:
105
  prompt=prompt
106
  )
107
 
108
- # Remember edited image
109
- if result:
 
110
  self.memory.last_used_image = result
111
 
 
 
 
112
  elif step == "analyze":
113
 
114
- result = self.auto_analyze()
 
 
 
 
115
 
116
  return result
 
11
  self.memory = memory
12
  self.planner = planner
13
 
14
+ # --------------------------------------------------
15
  # Upload Files
16
+ # --------------------------------------------------
17
+
18
  def upload(self, files):
19
 
20
  uploaded = save_uploads(files)
21
 
22
  self.memory.uploaded_files = uploaded
23
 
24
+ self.memory.image_context = None
25
+ self.memory.analysis = None
26
+
27
  for file in uploaded:
28
+
29
+ if file.get("type", "").startswith("image/"):
30
+
31
  self.memory.last_used_image = file["path"]
32
+
33
  break
34
 
35
  return uploaded
36
 
37
+ # --------------------------------------------------
38
  # Analyze Image
39
+ # --------------------------------------------------
40
+
41
  def auto_analyze(self, image_path=None):
42
 
43
+ image = image_path or self.memory.last_used_image
44
 
45
  if not image:
46
+
47
+ return {
48
+ "description": "No uploaded image found."
49
+ }
50
 
51
  analysis = analyze_image(image)
52
 
 
55
 
56
  return analysis
57
 
58
+ # --------------------------------------------------
59
+ # Convert object list safely
60
+ # --------------------------------------------------
 
61
 
62
+ def format_objects(self, objects):
63
 
64
+ if not objects:
65
+ return ""
66
 
67
+ names = []
68
 
69
+ for obj in objects:
70
 
71
+ if isinstance(obj, str):
 
 
72
 
73
+ names.append(obj)
74
+
75
+ elif isinstance(obj, dict):
76
+
77
+ if "name" in obj:
78
+
79
+ names.append(str(obj["name"]))
80
+
81
+ elif "label" in obj:
82
+
83
+ names.append(str(obj["label"]))
84
+
85
+ else:
86
+
87
+ names.append(str(obj))
88
+
89
+ else:
90
+
91
+ names.append(str(obj))
92
+
93
+ return ", ".join(names)
94
+
95
+ # --------------------------------------------------
96
+ # Build Prompt
97
+ # --------------------------------------------------
98
+
99
+ def build_context(self, analysis):
100
+
101
+ if not analysis:
102
+ return ""
103
+
104
+ objects = self.format_objects(
105
+ analysis.get("objects", [])
106
+ )
107
+
108
+ colors = analysis.get("colors", [])
109
 
110
+ if isinstance(colors, list):
111
+ colors = ", ".join(colors)
112
 
113
+ suggestions = analysis.get("suggestions", [])
114
 
115
+ if isinstance(suggestions, list):
116
+ suggestions = ", ".join(suggestions)
117
+
118
+ context = f"""
119
  Image Description:
120
+ {analysis.get("description","")}
121
 
122
  Objects:
123
+ {objects}
124
+
125
+ People:
126
+ {analysis.get("people","")}
127
 
128
  Style:
129
+ {analysis.get("style","")}
130
 
131
  Lighting:
132
+ {analysis.get("lighting","")}
133
+
134
+ Colors:
135
+ {colors}
136
+
137
+ Background:
138
+ {analysis.get("background","")}
139
 
140
+ Camera Angle:
141
+ {analysis.get("camera_angle","")}
142
+
143
+ Quality:
144
+ {analysis.get("quality","")}
145
+
146
+ Editing Prompt:
147
+ {analysis.get("editing_prompt","")}
148
+
149
+ Suggestions:
150
+ {suggestions}
151
  """
152
 
153
+ return context
154
+
155
+ # --------------------------------------------------
156
+ # Main Agent
157
+ # --------------------------------------------------
158
+
159
+ def run(self, user_input):
160
+
161
+ task = self.router.route(user_input)
162
+
163
+ steps = self.planner.plan(task)
164
+
165
+ image = self.memory.last_used_image
166
+
167
+ if image and self.memory.image_context is None:
168
+
169
+ self.auto_analyze(image)
170
+
171
+ analysis = self.memory.image_context
172
+
173
+ context = self.build_context(analysis)
174
+
175
  prompt = f"""
176
  {context}
177
 
 
179
  {user_input}
180
  """
181
 
182
+ result = None
183
+
184
  for step in steps:
185
 
186
  if step == "generate":
 
194
  prompt=prompt
195
  )
196
 
197
+ # If edit_image returns a file path
198
+ if isinstance(result, str):
199
+
200
  self.memory.last_used_image = result
201
 
202
+ # If edit_image returns a PIL.Image,
203
+ # app.py will display it directly.
204
+
205
  elif step == "analyze":
206
 
207
+ result = self.auto_analyze(image)
208
+
209
+ elif step == "chat":
210
+
211
+ result = analysis
212
 
213
  return result