Spaces:
Sleeping
Sleeping
Zanqi commited on
Commit ·
8931cf9
1
Parent(s): d23c350
Add image handling capability in FinalAnswerTool to support saving images as PNG
Browse files- tools/final_answer.py +6 -0
tools/final_answer.py
CHANGED
|
@@ -8,6 +8,12 @@ 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):
|
|
|
|
| 8 |
output_type = "any"
|
| 9 |
|
| 10 |
def forward(self, answer: Any) -> Any:
|
| 11 |
+
# handle image, e.g. smolagents.agent_types.AgentImage
|
| 12 |
+
if hasattr(answer, "_raw") and hasattr(answer._raw, "save"):
|
| 13 |
+
import tempfile
|
| 14 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
| 15 |
+
answer._raw.save(f.name, format="PNG")
|
| 16 |
+
answer._path = f.name
|
| 17 |
return answer
|
| 18 |
|
| 19 |
def __init__(self, *args, **kwargs):
|