Spaces:
Sleeping
Sleeping
Update tools/final_answer.py
Browse files- tools/final_answer.py +23 -8
tools/final_answer.py
CHANGED
|
@@ -1,14 +1,29 @@
|
|
| 1 |
-
from typing import Any
|
| 2 |
-
from smolagents.tools import Tool
|
| 3 |
|
| 4 |
class FinalAnswerTool(Tool):
|
| 5 |
name = "final_answer"
|
| 6 |
-
description = "Provides
|
| 7 |
-
inputs = {
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def forward(self, answer: Any) ->
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def __init__(self, *args, **kwargs):
|
| 14 |
-
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
+
from smolagents.tools import Tool, AgentImage
|
| 3 |
|
| 4 |
class FinalAnswerTool(Tool):
|
| 5 |
name = "final_answer"
|
| 6 |
+
description = "Provides the final formatted response to the user."
|
| 7 |
+
inputs = {
|
| 8 |
+
'answer': {'type': 'any', 'description': 'The final answer to the problem'},
|
| 9 |
+
'image': {'type': 'any', 'description': 'Optional image result to be displayed'}
|
| 10 |
+
}
|
| 11 |
+
output_type = "string"
|
| 12 |
|
| 13 |
+
def forward(self, answer: Any = "", image: Any = None) -> str:
|
| 14 |
+
# Handle AgentImage rendering
|
| 15 |
+
if isinstance(image, AgentImage):
|
| 16 |
+
try:
|
| 17 |
+
image_html = image.display() # or image.to_html()
|
| 18 |
+
except:
|
| 19 |
+
image_html = "[Image could not be displayed]"
|
| 20 |
+
# Combine text + image if both are provided
|
| 21 |
+
if answer:
|
| 22 |
+
return f"{answer}\n\n{image_html}"
|
| 23 |
+
else:
|
| 24 |
+
return image_html
|
| 25 |
+
else:
|
| 26 |
+
return str(answer)
|
| 27 |
|
| 28 |
def __init__(self, *args, **kwargs):
|
| 29 |
+
super().__init__(*args, **kwargs)
|