SamarthPujari commited on
Commit
145f293
·
verified ·
1 Parent(s): e2ed5fb

Update tools/final_answer.py

Browse files
Files changed (1) hide show
  1. tools/final_answer.py +26 -29
tools/final_answer.py CHANGED
@@ -1,27 +1,31 @@
1
- from typing import Optional
2
- from smolagents.tools import Tool
3
- from PIL import Image
4
  import os
5
  import datetime
 
 
 
6
 
7
  class FinalAnswerTool(Tool):
8
  name = "final_answer"
9
  description = "Provides a final answer. Can include text and/or a generated image."
10
-
11
  inputs = {
12
  'text': {'type': 'string', 'description': 'Final textual answer', 'nullable': True},
13
  'image_path': {'type': 'string', 'description': 'Path to a generated image file', 'nullable': True}
14
  }
15
-
16
- output_type = "string"
17
 
18
- def forward(self, text: Optional[str] = None, image_path: Optional[str] = None) -> str:
19
- # If an image exists, prefer displaying it
 
 
 
 
 
 
20
  if image_path and os.path.exists(image_path):
21
- image_display_path = image_path
22
  if image_path.lower().endswith(".webp"):
23
  try:
24
- img = Image.open(image_path)
25
  temp_dir = os.path.dirname(image_path) or "/tmp"
26
  os.makedirs(temp_dir, exist_ok=True)
27
 
@@ -33,25 +37,18 @@ class FinalAnswerTool(Tool):
33
  )
34
 
35
  img.save(temp_png_path, "PNG")
36
- image_display_path = temp_png_path
37
  except Exception as e:
38
- print(f"[ERROR] Failed to convert image {image_path} to PNG: {e}")
39
 
40
- # Embed image in Markdown
41
- output = f"![Generated Image]({image_display_path})"
42
- if text:
43
- output += f"\n\n{text}"
44
- return output
45
-
46
- # Fallback to text only
47
- if text:
48
- return text
49
-
50
- return "No final answer provided."
51
 
52
- def __call__(self, *args, **kwargs):
53
- return self.forward(*args, **kwargs)
54
-
55
- def __init__(self, *args, **kwargs):
56
- super().__init__(*args, **kwargs)
57
- self.is_initialized = False
 
 
 
 
 
 
 
1
  import os
2
  import datetime
3
+ from typing import Optional, Union, Tuple
4
+ from PIL import Image
5
+ from smolagents import Tool
6
 
7
  class FinalAnswerTool(Tool):
8
  name = "final_answer"
9
  description = "Provides a final answer. Can include text and/or a generated image."
10
+
11
  inputs = {
12
  'text': {'type': 'string', 'description': 'Final textual answer', 'nullable': True},
13
  'image_path': {'type': 'string', 'description': 'Path to a generated image file', 'nullable': True}
14
  }
 
 
15
 
16
+ output_type = "any" # <-- can now return text, image, or both
17
+
18
+ def forward(
19
+ self,
20
+ text: Optional[str] = None,
21
+ image_path: Optional[str] = None
22
+ ) -> Union[str, Image.Image, Tuple[Image.Image, str]]:
23
+
24
  if image_path and os.path.exists(image_path):
25
+ # Convert .webp to .png (since some frontends don't support webp well)
26
  if image_path.lower().endswith(".webp"):
27
  try:
28
+ img = Image.open(image_path).convert("RGB")
29
  temp_dir = os.path.dirname(image_path) or "/tmp"
30
  os.makedirs(temp_dir, exist_ok=True)
31
 
 
37
  )
38
 
39
  img.save(temp_png_path, "PNG")
40
+ image_path = temp_png_path
41
  except Exception as e:
42
+ print(f"[ERROR] Failed to convert image {image_path}: {e}")
43
 
44
+ img = Image.open(image_path)
 
 
 
 
 
 
 
 
 
 
45
 
46
+ if text:
47
+ # Return tuple if both text + image
48
+ return (img, text)
49
+ else:
50
+ # Return only image
51
+ return img
52
+
53
+ # Only text case
54
+ return text