Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import sys
|
|
| 8 |
import uuid
|
| 9 |
import base64
|
| 10 |
from io import BytesIO
|
|
|
|
| 11 |
from tools.final_answer import FinalAnswerTool
|
| 12 |
from tools.web_search import DuckDuckGoSearchTool
|
| 13 |
from tools.visit_webpage import VisitWebpageTool
|
|
@@ -89,13 +90,13 @@ def search_web(query: str) -> str:
|
|
| 89 |
return f"Error searching the web: {str(e)}"
|
| 90 |
|
| 91 |
#############################################################
|
| 92 |
-
# FINAL ANSWER TOOL
|
| 93 |
#############################################################
|
| 94 |
-
# Initialize the final answer tool
|
| 95 |
final_answer_base = FinalAnswerTool()
|
| 96 |
|
| 97 |
@tool
|
| 98 |
-
def final_answer(answer) ->
|
| 99 |
"""Provides a final answer to the given problem.
|
| 100 |
Args:
|
| 101 |
answer: The final answer to the problem
|
|
@@ -149,7 +150,15 @@ except Exception as e:
|
|
| 149 |
print(f"✗ Failed to load image generation tool: {str(e)}")
|
| 150 |
# Provide a fallback if the image generation tool fails to load
|
| 151 |
def image_generation_tool(prompt):
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
print("✓ Time zone tool initialized")
|
| 155 |
print("✓ Web search tool initialized")
|
|
@@ -175,6 +184,7 @@ print("✓ Model configured successfully")
|
|
| 175 |
print("Loading prompt templates...")
|
| 176 |
# Create an upload directory if it doesn't exist
|
| 177 |
os.makedirs("uploads", exist_ok=True)
|
|
|
|
| 178 |
|
| 179 |
# Load prompt templates from YAML file for consistent agent responses
|
| 180 |
try:
|
|
@@ -197,7 +207,7 @@ agent = CodeAgent(
|
|
| 197 |
generate_image_from_text, # Tool 2: Image generation tool
|
| 198 |
search_web, # Tool 3: Web search tool
|
| 199 |
visit_webpage, # Tool 4: Visit webpage tool (added from tools directory)
|
| 200 |
-
final_answer #
|
| 201 |
],
|
| 202 |
max_steps=6, # Maximum number of reasoning steps
|
| 203 |
verbosity_level=1, # Level of detail in agent's output
|
|
|
|
| 8 |
import uuid
|
| 9 |
import base64
|
| 10 |
from io import BytesIO
|
| 11 |
+
from typing import Any, Union, List
|
| 12 |
from tools.final_answer import FinalAnswerTool
|
| 13 |
from tools.web_search import DuckDuckGoSearchTool
|
| 14 |
from tools.visit_webpage import VisitWebpageTool
|
|
|
|
| 90 |
return f"Error searching the web: {str(e)}"
|
| 91 |
|
| 92 |
#############################################################
|
| 93 |
+
# CUSTOM FINAL ANSWER TOOL
|
| 94 |
#############################################################
|
| 95 |
+
# Initialize the base final answer tool
|
| 96 |
final_answer_base = FinalAnswerTool()
|
| 97 |
|
| 98 |
@tool
|
| 99 |
+
def final_answer(answer: Any) -> Any:
|
| 100 |
"""Provides a final answer to the given problem.
|
| 101 |
Args:
|
| 102 |
answer: The final answer to the problem
|
|
|
|
| 150 |
print(f"✗ Failed to load image generation tool: {str(e)}")
|
| 151 |
# Provide a fallback if the image generation tool fails to load
|
| 152 |
def image_generation_tool(prompt):
|
| 153 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 154 |
+
import numpy as np
|
| 155 |
+
|
| 156 |
+
# Create a blank image with text saying this is a fallback
|
| 157 |
+
img = Image.new('RGB', (512, 512), color=(245, 245, 245))
|
| 158 |
+
d = ImageDraw.Draw(img)
|
| 159 |
+
d.text((10, 10), f"Fallback image for: {prompt}", fill=(0, 0, 0))
|
| 160 |
+
d.text((10, 30), "Image generation tool failed to load", fill=(255, 0, 0))
|
| 161 |
+
return img
|
| 162 |
|
| 163 |
print("✓ Time zone tool initialized")
|
| 164 |
print("✓ Web search tool initialized")
|
|
|
|
| 184 |
print("Loading prompt templates...")
|
| 185 |
# Create an upload directory if it doesn't exist
|
| 186 |
os.makedirs("uploads", exist_ok=True)
|
| 187 |
+
os.makedirs("uploads/images", exist_ok=True)
|
| 188 |
|
| 189 |
# Load prompt templates from YAML file for consistent agent responses
|
| 190 |
try:
|
|
|
|
| 207 |
generate_image_from_text, # Tool 2: Image generation tool
|
| 208 |
search_web, # Tool 3: Web search tool
|
| 209 |
visit_webpage, # Tool 4: Visit webpage tool (added from tools directory)
|
| 210 |
+
final_answer # Custom final answer tool
|
| 211 |
],
|
| 212 |
max_steps=6, # Maximum number of reasoning steps
|
| 213 |
verbosity_level=1, # Level of detail in agent's output
|