Spaces:
Sleeping
Sleeping
Update tools/final_answer.py
Browse files- tools/final_answer.py +15 -4
tools/final_answer.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
-
from typing import Any
|
| 2 |
from smolagents.tools import Tool
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class FinalAnswerTool(Tool):
|
| 5 |
name = "final_answer"
|
|
@@ -8,7 +11,15 @@ 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 os
|
| 5 |
+
from uuid import uuid4
|
| 6 |
|
| 7 |
class FinalAnswerTool(Tool):
|
| 8 |
name = "final_answer"
|
|
|
|
| 11 |
output_type = "any"
|
| 12 |
|
| 13 |
def forward(self, answer: Any) -> Any:
|
| 14 |
+
# If it's an AgentImage, save it to file and return an updated object
|
| 15 |
+
if isinstance(answer, AgentImage):
|
| 16 |
+
image = answer
|
| 17 |
+
# Save to temp folder inside the Space
|
| 18 |
+
output_dir = "/tmp/final_outputs"
|
| 19 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 20 |
+
path = os.path.join(output_dir, f"{uuid4().hex}.png")
|
| 21 |
+
image.save(path)
|
| 22 |
+
# Patch the object so `to_string()` will work
|
| 23 |
+
image.path = path
|
| 24 |
+
return image
|
| 25 |
return answer
|
|
|
|
|
|
|
|
|