Journi-09032025 / tools /final_answer.py
PLBot's picture
Update tools/final_answer.py
8d6731a verified
raw
history blame contribute delete
922 Bytes
from typing import Any, Optional
from smolagents.tools import Tool
from smolagents.agent_types import AgentImage
class FinalAnswerTool(Tool):
name = "final_answer"
description = "Provides a final answer to the given problem."
inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
output_type = "any"
def forward(self, answer: Any) -> Any:
# Check if the answer is an image path
if isinstance(answer, str) and ('/tmp/gradio/' in answer or answer.endswith(('.png', '.jpg', '.jpeg', '.webp'))):
# Return as AgentImage for proper handling by stream_to_gradio
return AgentImage(answer)
# Return the original answer for non-image responses
return answer
def __init__(self, *args, **kwargs):
super().__init__() # Make sure to call the parent class initializer
self.is_initialized = False