File size: 1,337 Bytes
7b69201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd18d93
8fe992b
a31a574
8fe992b
 
 
 
 
 
 
 
a31a574
 
 
 
7b69201
 
8fe992b
 
 
7b69201
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# from typing import Any, Optional
# from smolagents.tools import Tool

# 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:
#         return answer

#     def __init__(self, *args, **kwargs):
#         self.is_initialized = False

from typing import Any, Optional
from smolagents.tools import Tool
from smolagents.agent_types import AgentImage  # Import this class

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):
        self.is_initialized = False