File size: 922 Bytes
fd18d93
8fe992b
b6ddb1d
8fe992b
 
 
 
 
 
 
 
d561cab
 
 
 
 
 
8fe992b
 
 
8d6731a
b6ddb1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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