Spaces:
Runtime error
Runtime error
Added multi web search switching capability, automated
Browse files
app.py
CHANGED
|
@@ -18,9 +18,46 @@ Perplex_Assistant_Prompt = """You are a helpful AI assistant that searches the w
|
|
| 18 |
# Set up API key in environment variable as expected by HfApiModel
|
| 19 |
os.environ["HUGGINGFACE_API_TOKEN"] = os.getenv("HUGGINGFACE_API_KEY", "")
|
| 20 |
|
| 21 |
-
# Initialize
|
| 22 |
-
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Initialize the VisitWebpageTool
|
| 26 |
visit_webpage_tool = VisitWebpageTool()
|
|
@@ -378,22 +415,6 @@ model.__call__ = try_model_call_with_fallbacks
|
|
| 378 |
# Import tool from Hub
|
| 379 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 380 |
|
| 381 |
-
@tool
|
| 382 |
-
def web_search(query: str) -> str:
|
| 383 |
-
"""A tool that searches the web for information.
|
| 384 |
-
|
| 385 |
-
Args:
|
| 386 |
-
query: The search query string
|
| 387 |
-
|
| 388 |
-
Returns:
|
| 389 |
-
Search results as text
|
| 390 |
-
"""
|
| 391 |
-
try:
|
| 392 |
-
# Use the already initialized DuckDuckGo search tool
|
| 393 |
-
return ddg_search_tool(query)
|
| 394 |
-
except Exception as e:
|
| 395 |
-
return f"Error performing web search for '{query}': {str(e)}"
|
| 396 |
-
|
| 397 |
with open("prompts.yaml", 'r') as stream:
|
| 398 |
prompt_templates = yaml.safe_load(stream)
|
| 399 |
|
|
@@ -402,14 +423,12 @@ agent = CodeAgent(
|
|
| 402 |
tools=[
|
| 403 |
final_answer,
|
| 404 |
Sonar_Web_Search_Tool,
|
| 405 |
-
|
| 406 |
-
ddg_search_tool, # Added DuckDuckGo search tool
|
| 407 |
-
# google_search_tool, # Added Google search tool
|
| 408 |
get_current_time_in_timezone,
|
| 409 |
image_generation_tool,
|
| 410 |
Dataset_Creator_Tool,
|
| 411 |
Check_Dataset_Validity,
|
| 412 |
-
visit_webpage_tool,
|
| 413 |
],
|
| 414 |
max_steps=6,
|
| 415 |
verbosity_level=1,
|
|
@@ -420,6 +439,8 @@ agent = CodeAgent(
|
|
| 420 |
prompt_templates=prompt_templates
|
| 421 |
)
|
| 422 |
|
|
|
|
|
|
|
| 423 |
# To fix the TypeError in Gradio_UI.py, you would need to modify that file
|
| 424 |
# For now, we'll just use the agent directly
|
| 425 |
try:
|
|
|
|
| 18 |
# Set up API key in environment variable as expected by HfApiModel
|
| 19 |
os.environ["HUGGINGFACE_API_TOKEN"] = os.getenv("HUGGINGFACE_API_KEY", "")
|
| 20 |
|
| 21 |
+
# Initialize search tools with fallback capability
|
| 22 |
+
try:
|
| 23 |
+
# Try DuckDuckGo first (default)
|
| 24 |
+
print("Initializing DuckDuckGo search tool...")
|
| 25 |
+
ddg_search_tool = DuckDuckGoSearchTool(max_results=10)
|
| 26 |
+
|
| 27 |
+
# Test the tool with a simple query
|
| 28 |
+
test_result = ddg_search_tool("test query")
|
| 29 |
+
print("DuckDuckGo search tool initialized successfully.")
|
| 30 |
+
|
| 31 |
+
# Use DuckDuckGo as the primary search tool
|
| 32 |
+
primary_search_tool = ddg_search_tool
|
| 33 |
+
search_tool_name = "DuckDuckGo"
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error initializing DuckDuckGo search tool: {str(e)}")
|
| 37 |
+
print("Falling back to Google search tool...")
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
# Import GoogleSearchTool only if needed
|
| 41 |
+
from smolagents import GoogleSearchTool
|
| 42 |
+
google_search_tool = GoogleSearchTool()
|
| 43 |
+
|
| 44 |
+
# Test the Google search tool
|
| 45 |
+
test_result = google_search_tool("test query")
|
| 46 |
+
print("Google search tool initialized successfully.")
|
| 47 |
+
|
| 48 |
+
# Use Google as the fallback search tool
|
| 49 |
+
primary_search_tool = google_search_tool
|
| 50 |
+
search_tool_name = "Google"
|
| 51 |
+
|
| 52 |
+
except Exception as google_error:
|
| 53 |
+
print(f"Error initializing Google search tool: {str(google_error)}")
|
| 54 |
+
print("WARNING: No working search tool available. Agent functionality will be limited.")
|
| 55 |
+
# Create a minimal replacement that returns an explanatory message
|
| 56 |
+
def search_fallback(query):
|
| 57 |
+
return f"Search functionality unavailable. Both DuckDuckGo and Google search tools failed to initialize. Query was: {query}"
|
| 58 |
+
|
| 59 |
+
primary_search_tool = search_fallback
|
| 60 |
+
search_tool_name = "Unavailable"
|
| 61 |
|
| 62 |
# Initialize the VisitWebpageTool
|
| 63 |
visit_webpage_tool = VisitWebpageTool()
|
|
|
|
| 415 |
# Import tool from Hub
|
| 416 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
with open("prompts.yaml", 'r') as stream:
|
| 419 |
prompt_templates = yaml.safe_load(stream)
|
| 420 |
|
|
|
|
| 423 |
tools=[
|
| 424 |
final_answer,
|
| 425 |
Sonar_Web_Search_Tool,
|
| 426 |
+
primary_search_tool, # Use whichever search tool was successfully initialized
|
|
|
|
|
|
|
| 427 |
get_current_time_in_timezone,
|
| 428 |
image_generation_tool,
|
| 429 |
Dataset_Creator_Tool,
|
| 430 |
Check_Dataset_Validity,
|
| 431 |
+
visit_webpage_tool,
|
| 432 |
],
|
| 433 |
max_steps=6,
|
| 434 |
verbosity_level=1,
|
|
|
|
| 439 |
prompt_templates=prompt_templates
|
| 440 |
)
|
| 441 |
|
| 442 |
+
print(f"Agent initialized with {search_tool_name} as primary search tool")
|
| 443 |
+
|
| 444 |
# To fix the TypeError in Gradio_UI.py, you would need to modify that file
|
| 445 |
# For now, we'll just use the agent directly
|
| 446 |
try:
|