Update agents/image_agent.py

#14
Files changed (1) hide show
  1. agents/image_agent.py +33 -13
agents/image_agent.py CHANGED
@@ -18,45 +18,65 @@ class ImageAgent:
18
 
19
  def auto_analyze(self, image_path):
20
  """
21
- STEP 1: Always understand image first
22
  """
23
  if image_path:
24
  analysis = analyze_image(image_path)
 
 
 
 
 
 
 
25
  self.memory.last_used_image = image_path
 
26
  return analysis
 
27
  return None
28
 
29
  def run(self, user_input: str):
30
 
 
31
  task = self.router.route(user_input)
32
  steps = self.planner.plan(task)
33
 
34
  result = None
35
 
36
- # 🔥 STEP 0: ALWAYS pick image first
37
  image = None
38
-
39
  if self.memory.uploaded_files:
40
  image = self.memory.uploaded_files[0]
41
 
42
- # 🔥 STEP 1: ANALYZE FIRST (IMPORTANT)
43
- if image:
44
- analysis = self.auto_analyze(image)
45
- self.memory.analysis = analysis
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- # 🔥 STEP 2: EXECUTE TASK
48
  for step in steps:
49
 
50
  if step == "generate":
51
- result = generate_image(user_input)
52
 
53
  elif step == "edit":
54
- image = self.memory.last_used_image or image
55
- result = edit_image(image, user_input)
56
- self.memory.last_used_image = image
57
 
58
  elif step == "analyze":
59
- image = self.memory.last_used_image or image
60
  result = analyze_image(image)
61
 
62
  return result
 
18
 
19
  def auto_analyze(self, image_path):
20
  """
21
+ STEP 1: Understand image
22
  """
23
  if image_path:
24
  analysis = analyze_image(image_path)
25
+
26
+ # store context safely
27
+ if hasattr(self.memory, "set_image_context"):
28
+ self.memory.set_image_context(analysis["description"])
29
+ else:
30
+ self.memory.image_context = analysis["description"]
31
+
32
  self.memory.last_used_image = image_path
33
+
34
  return analysis
35
+
36
  return None
37
 
38
  def run(self, user_input: str):
39
 
40
+ # ---------------- ROUTE ----------------
41
  task = self.router.route(user_input)
42
  steps = self.planner.plan(task)
43
 
44
  result = None
45
 
46
+ # ---------------- IMAGE PICK ----------------
47
  image = None
 
48
  if self.memory.uploaded_files:
49
  image = self.memory.uploaded_files[0]
50
 
51
+ # ---------------- AUTO ANALYZE ----------------
52
+ if image and not self.memory.image_context:
53
+ analysis = analyze_image(image)
54
+ self.memory.image_context = analysis["description"]
55
+
56
+ # ---------------- COMBINE CONTEXT ----------------
57
+ combined_prompt = user_input
58
+
59
+ if self.memory.image_context:
60
+ combined_prompt = f"""
61
+ Image Context:
62
+ {self.memory.image_context}
63
+
64
+ User Request:
65
+ {user_input}
66
+ """
67
 
68
+ # ---------------- EXECUTE STEPS ----------------
69
  for step in steps:
70
 
71
  if step == "generate":
72
+ result = generate_image(combined_prompt)
73
 
74
  elif step == "edit":
75
+ img = self.memory.last_used_image or image
76
+ result = edit_image(img, combined_prompt)
77
+ self.memory.last_used_image = img
78
 
79
  elif step == "analyze":
 
80
  result = analyze_image(image)
81
 
82
  return result