File size: 1,239 Bytes
00a84d6
 
 
 
 
 
8fe992b
00a84d6
 
8fe992b
 
 
 
00a84d6
8fe992b
 
194b8ee
38e3dc1
5d62e8c
 
 
00a84d6
 
 
 
 
 
38e3dc1
00a84d6
 
 
 
 
 
 
 
 
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
37
38
from __future__ import annotations

from typing import Any
import os
import uuid

from smolagents.tools import Tool
from smolagents.agent_types import AgentImage, AgentText, AgentAudio


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 __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)  # IMPORTANT
        from pathlib import Path
        self.output_dir = Path.cwd() / "outputs" / "final_answers"
        self.output_dir.mkdir(parents=True, exist_ok=True)

    def forward(self, answer: Any) -> Any:
        if isinstance(answer, (AgentImage, AgentText, AgentAudio)):
            return answer

        try:
            from PIL import Image  # pillow is usually already there due to text-to-image
            if isinstance(answer, Image.Image):
                filename = f"{uuid.uuid4().hex}.png"
                path = os.path.join(self.output_dir, filename)
                answer.save(path, format="PNG")
                return AgentImage(path)
        except Exception:
            pass

        return answer