File size: 943 Bytes
dba960a
8fe992b
dba960a
 
 
8fe992b
 
 
 
 
 
 
 
dba960a
 
 
 
 
 
 
 
 
 
 
8fe992b
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
from typing import Any
from smolagents.tools import Tool
from smolagents.agent_types import AgentImage
import os
from uuid import uuid4

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:
        # If it's an AgentImage, save it to file and return an updated object
        if isinstance(answer, AgentImage):
            image = answer
            # Save to temp folder inside the Space
            output_dir = "/tmp/final_outputs"
            os.makedirs(output_dir, exist_ok=True)
            path = os.path.join(output_dir, f"{uuid4().hex}.png")
            image.save(path)
            # Patch the object so `to_string()` will work
            image.path = path
            return image
        return answer