SamarthPujari commited on
Commit
96a3e1f
·
verified ·
1 Parent(s): d24c217

Update tools/final_answer.py

Browse files
Files changed (1) hide show
  1. tools/final_answer.py +9 -17
tools/final_answer.py CHANGED
@@ -1,20 +1,20 @@
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
  """
@@ -23,20 +23,15 @@ class FinalAnswerTool(Tool):
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"
@@ -47,9 +42,7 @@ class FinalAnswerTool(Tool):
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"![Generated Image]({image_display_path})")
54
 
55
  if text:
@@ -61,6 +54,5 @@ class FinalAnswerTool(Tool):
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
 
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
+ from PIL import Image
4
+ import os
5
+ import datetime
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
+ # CORRECTED: Use 'string' instead of 'str' for type
12
  inputs = {
13
+ 'text': {'type': 'string', 'description': 'The final textual answer to the problem'},
14
+ 'image_path': {'type': 'string', 'description': 'Optional path to a generated image file (e.g., .webp, .png, .jpg) to be displayed.'}
15
  }
16
 
17
+ output_type = "string" # Output will be a Markdown string, so 'string' is appropriate
18
 
19
  def forward(self, text: Optional[str] = None, image_path: Optional[str] = None) -> str:
20
  """
 
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) if os.path.dirname(image_path) else "/tmp"
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"
 
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}. Attempting to use original path.")
 
45
 
 
46
  output_content.append(f"![Generated Image]({image_display_path})")
47
 
48
  if text:
 
54
  return "\n\n".join(output_content)
55
 
56
  def __init__(self, *args, **kwargs):
 
57
  super().__init__(*args, **kwargs)
58
+ self.is_initialized = False