Spaces:
Sleeping
Sleeping
Update tools/final_answer.py
Browse files- tools/final_answer.py +21 -23
tools/final_answer.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from typing import
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
|
@@ -6,53 +6,51 @@ import datetime
|
|
| 6 |
|
| 7 |
class FinalAnswerTool(Tool):
|
| 8 |
name = "final_answer"
|
| 9 |
-
description = "Provides a final answer
|
| 10 |
|
| 11 |
-
# CORRECTED: Add "nullable": True for both 'text' and 'image_path'
|
| 12 |
inputs = {
|
| 13 |
-
'text': {'type': 'string', 'description': '
|
| 14 |
-
'image_path': {'type': 'string', 'description': '
|
| 15 |
}
|
| 16 |
|
| 17 |
-
output_type = "string"
|
| 18 |
|
| 19 |
def forward(self, text: Optional[str] = None, image_path: Optional[str] = None) -> str:
|
| 20 |
-
|
| 21 |
-
Processes the final answer, potentially embedding an image from a given path.
|
| 22 |
-
"""
|
| 23 |
-
output_content = []
|
| 24 |
-
|
| 25 |
if image_path and os.path.exists(image_path):
|
| 26 |
image_display_path = image_path
|
| 27 |
if image_path.lower().endswith(".webp"):
|
| 28 |
try:
|
| 29 |
img = Image.open(image_path)
|
| 30 |
-
temp_dir = os.path.dirname(image_path)
|
| 31 |
os.makedirs(temp_dir, exist_ok=True)
|
| 32 |
-
|
| 33 |
base_name = os.path.basename(image_path)
|
| 34 |
name_without_ext = os.path.splitext(base_name)[0]
|
| 35 |
temp_png_path = os.path.join(
|
| 36 |
temp_dir,
|
| 37 |
f"{name_without_ext}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}.png"
|
| 38 |
)
|
| 39 |
-
|
| 40 |
img.save(temp_png_path, "PNG")
|
| 41 |
image_display_path = temp_png_path
|
| 42 |
-
print(f"[DEBUG] Converted {image_path} to {temp_png_path} for display.")
|
| 43 |
except Exception as e:
|
| 44 |
-
print(f"[ERROR] Failed to convert image {image_path} to PNG: {e}
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
|
|
|
| 48 |
if text:
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
# If both are None, provide a default message
|
| 53 |
-
return "No final answer provided."
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
def __init__(self, *args, **kwargs):
|
| 58 |
super().__init__(*args, **kwargs)
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
|
|
|
| 6 |
|
| 7 |
class FinalAnswerTool(Tool):
|
| 8 |
name = "final_answer"
|
| 9 |
+
description = "Provides a final answer. Can include text and/or a generated image."
|
| 10 |
|
|
|
|
| 11 |
inputs = {
|
| 12 |
+
'text': {'type': 'string', 'description': 'Final textual answer', 'nullable': True},
|
| 13 |
+
'image_path': {'type': 'string', 'description': 'Path to a generated image file', 'nullable': True}
|
| 14 |
}
|
| 15 |
|
| 16 |
+
output_type = "string"
|
| 17 |
|
| 18 |
def forward(self, text: Optional[str] = None, image_path: Optional[str] = None) -> str:
|
| 19 |
+
# If an image exists, prefer displaying it
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
if image_path and os.path.exists(image_path):
|
| 21 |
image_display_path = image_path
|
| 22 |
if image_path.lower().endswith(".webp"):
|
| 23 |
try:
|
| 24 |
img = Image.open(image_path)
|
| 25 |
+
temp_dir = os.path.dirname(image_path) or "/tmp"
|
| 26 |
os.makedirs(temp_dir, exist_ok=True)
|
| 27 |
+
|
| 28 |
base_name = os.path.basename(image_path)
|
| 29 |
name_without_ext = os.path.splitext(base_name)[0]
|
| 30 |
temp_png_path = os.path.join(
|
| 31 |
temp_dir,
|
| 32 |
f"{name_without_ext}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}.png"
|
| 33 |
)
|
| 34 |
+
|
| 35 |
img.save(temp_png_path, "PNG")
|
| 36 |
image_display_path = temp_png_path
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"[ERROR] Failed to convert image {image_path} to PNG: {e}")
|
| 39 |
+
|
| 40 |
+
# Embed image in Markdown
|
| 41 |
+
output = f""
|
| 42 |
+
if text:
|
| 43 |
+
output += f"\n\n{text}"
|
| 44 |
+
return output
|
| 45 |
|
| 46 |
+
# Fallback to text only
|
| 47 |
if text:
|
| 48 |
+
return text
|
| 49 |
|
| 50 |
+
return "No final answer provided."
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
def __call__(self, *args, **kwargs):
|
| 53 |
+
return self.forward(*args, **kwargs)
|
| 54 |
|
| 55 |
def __init__(self, *args, **kwargs):
|
| 56 |
super().__init__(*args, **kwargs)
|