Update agents/image_agent.py

#18
Files changed (1) hide show
  1. agents/image_agent.py +73 -39
agents/image_agent.py CHANGED
@@ -11,72 +11,106 @@ class ImageAgent:
11
  self.memory = memory
12
  self.planner = planner
13
 
 
 
 
14
  def upload(self, files):
15
- paths = save_uploads(files)
16
- self.memory.add_files(paths)
17
- return paths
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
 
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):
35
 
36
+ image = self.memory.last_used_image
37
+
38
+ if not image:
39
+ return None
40
+
41
+ analysis = analyze_image(image)
42
+
43
+ self.memory.analysis = analysis
44
+ self.memory.image_context = analysis
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
 
91
  User Request:
92
  {user_input}
93
  """
94
 
 
95
  for step in steps:
96
 
97
  if step == "generate":
98
+
99
+ result = generate_image(prompt)
100
 
101
  elif step == "edit":
102
+
103
+ result = edit_image(
104
+ image_path=image,
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