Spaces:
Sleeping
Sleeping
Update tools/final_answer.py
Browse files- tools/final_answer.py +58 -6
tools/final_answer.py
CHANGED
|
@@ -1,14 +1,66 @@
|
|
| 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 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def forward(self,
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def __init__(self, *args, **kwargs):
|
| 14 |
-
|
|
|
|
|
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
+
from PIL import Image # Add this import
|
| 4 |
+
import os # Add this import
|
| 5 |
+
import datetime # Add this import
|
| 6 |
|
| 7 |
class FinalAnswerTool(Tool):
|
| 8 |
name = "final_answer"
|
| 9 |
+
description = "Provides a final answer to the given problem. Can include a textual response and/or a path to a generated image."
|
| 10 |
+
|
| 11 |
+
# IMPORTANT: Update the inputs to explicitly allow 'text' and 'image_path'
|
| 12 |
+
inputs = {
|
| 13 |
+
'text': {'type': 'str', 'description': 'The final textual answer to the problem'},
|
| 14 |
+
'image_path': {'type': 'str', 'description': 'Optional path to a generated image file (e.g., .webp, .png, .jpg) to be displayed.'}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
output_type = "str" # The output will be a Markdown 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 |
+
# Gradio should typically handle .webp directly, but converting to PNG
|
| 27 |
+
# is a robust fallback for broader compatibility or specific display quirks.
|
| 28 |
+
image_display_path = image_path
|
| 29 |
+
if image_path.lower().endswith(".webp"):
|
| 30 |
+
try:
|
| 31 |
+
img = Image.open(image_path)
|
| 32 |
+
# Create a temporary filename for the PNG
|
| 33 |
+
# Use a more robust temporary path strategy
|
| 34 |
+
temp_dir = os.path.dirname(image_path) if os.path.dirname(image_path) else "/tmp"
|
| 35 |
+
os.makedirs(temp_dir, exist_ok=True) # Ensure temp_dir exists
|
| 36 |
+
|
| 37 |
+
base_name = os.path.basename(image_path)
|
| 38 |
+
name_without_ext = os.path.splitext(base_name)[0]
|
| 39 |
+
# Add microsecond precision to avoid collisions if multiple images are generated rapidly
|
| 40 |
+
temp_png_path = os.path.join(
|
| 41 |
+
temp_dir,
|
| 42 |
+
f"{name_without_ext}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}.png"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
img.save(temp_png_path, "PNG")
|
| 46 |
+
image_display_path = temp_png_path
|
| 47 |
+
print(f"[DEBUG] Converted {image_path} to {temp_png_path} for display.")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"[ERROR] Failed to convert image {image_path} to PNG: {e}. Attempting to use original path.")
|
| 50 |
+
# Fallback to original path if conversion fails
|
| 51 |
+
|
| 52 |
+
# Embed the image using Markdown syntax, which Gradio will render
|
| 53 |
+
output_content.append(f"")
|
| 54 |
+
|
| 55 |
+
if text:
|
| 56 |
+
output_content.append(text)
|
| 57 |
+
|
| 58 |
+
if not output_content:
|
| 59 |
+
return "No final answer provided."
|
| 60 |
+
|
| 61 |
+
return "\n\n".join(output_content)
|
| 62 |
|
| 63 |
def __init__(self, *args, **kwargs):
|
| 64 |
+
# Important: Call the superclass __init__ to properly initialize the Tool
|
| 65 |
+
super().__init__(*args, **kwargs)
|
| 66 |
+
self.is_initialized = False # You can keep this if it's used elsewhere
|