MoudClam commited on
Commit
00a84d6
·
verified ·
1 Parent(s): 9cd322b

Update tools/final_answer.py

Browse files
Files changed (1) hide show
  1. tools/final_answer.py +31 -5
tools/final_answer.py CHANGED
@@ -1,14 +1,40 @@
1
- from typing import Any, Optional
 
 
 
 
 
2
  from smolagents.tools import Tool
 
 
3
 
4
  class FinalAnswerTool(Tool):
5
  name = "final_answer"
6
  description = "Provides a final answer to the given problem."
7
- inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
8
  output_type = "any"
9
 
10
- def forward(self, answer: Any) -> Any:
11
- return answer
12
-
13
  def __init__(self, *args, **kwargs):
14
  self.is_initialized = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+ import os
5
+ import uuid
6
+
7
  from smolagents.tools import Tool
8
+ from smolagents.agent_types import AgentImage, AgentText, AgentAudio
9
+
10
 
11
  class FinalAnswerTool(Tool):
12
  name = "final_answer"
13
  description = "Provides a final answer to the given problem."
14
+ inputs = {"answer": {"type": "any", "description": "The final answer to the problem"}}
15
  output_type = "any"
16
 
 
 
 
17
  def __init__(self, *args, **kwargs):
18
  self.is_initialized = False
19
+ # Папка, где будем сохранять финальные изображения
20
+ self.output_dir = os.environ.get("FINAL_ANSWER_DIR", "/tmp/final_answers")
21
+ os.makedirs(self.output_dir, exist_ok=True)
22
+
23
+ def forward(self, answer: Any) -> Any:
24
+ # Если уже нормализовано — не трогаем
25
+ if isinstance(answer, (AgentImage, AgentText, AgentAudio)):
26
+ return answer
27
+
28
+ # Если это PIL.Image (или PngImageFile и т.п.) — сохраним и вернем AgentImage(path)
29
+ try:
30
+ from PIL import Image # pillow обычно уже есть из-за text-to-image
31
+ if isinstance(answer, Image.Image):
32
+ filename = f"{uuid.uuid4().hex}.png"
33
+ path = os.path.join(self.output_dir, filename)
34
+ answer.save(path, format="PNG")
35
+ return AgentImage(path)
36
+ except Exception:
37
+ # Если pillow не установлен или что-то пошло не так — просто вернем как есть
38
+ pass
39
+
40
+ return answer