Zanqi commited on
Commit
c495e4a
·
1 Parent(s): cec8501

Add image handling in FinalAnswerTool to support saving and validating AgentImage instances

Browse files
Files changed (1) hide show
  1. tools/final_answer.py +22 -1
tools/final_answer.py CHANGED
@@ -1,5 +1,7 @@
1
- from typing import Any, Optional
2
  from smolagents.tools import Tool
 
 
3
 
4
  class FinalAnswerTool(Tool):
5
  name = "final_answer"
@@ -8,7 +10,26 @@ class FinalAnswerTool(Tool):
8
  output_type = "any"
9
 
10
  def forward(self, answer: Any) -> Any:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  return answer
12
 
13
  def __init__(self, *args, **kwargs):
 
14
  self.is_initialized = False
 
1
+ from typing import Any
2
  from smolagents.tools import Tool
3
+ from smolagents.agent_types import AgentImage
4
+ import tempfile
5
 
6
  class FinalAnswerTool(Tool):
7
  name = "final_answer"
 
10
  output_type = "any"
11
 
12
  def forward(self, answer: Any) -> Any:
13
+ # Handle PIL Images by converting them to AgentImage with saved file
14
+ if hasattr(answer, 'save') and hasattr(answer, 'mode'): # PIL Image
15
+ # Create a temporary file to save the image
16
+ with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
17
+ answer.save(tmp_file.name, format='PNG')
18
+ # Create AgentImage from the saved file
19
+ return AgentImage(tmp_file.name)
20
+
21
+ # If it's already an AgentImage, ensure it has valid data
22
+ if isinstance(answer, AgentImage):
23
+ # If the AgentImage doesn't have a valid path, try to save it
24
+ if not hasattr(answer, '_path') or not answer._path:
25
+ if hasattr(answer, '_value') and answer._value is not None:
26
+ with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
27
+ answer._value.save(tmp_file.name, format='PNG')
28
+ answer._path = tmp_file.name
29
+ return answer
30
+
31
  return answer
32
 
33
  def __init__(self, *args, **kwargs):
34
+ super().__init__(*args, **kwargs)
35
  self.is_initialized = False