Spaces:
Runtime error
Runtime error
Added multi web search switching capability, automated
Browse files17 unmodified lines181920212223242526351 unmodified lines3783793803813823833843853863873883893903913923933943953963973983992 unmodified lines4024034044054064074084094104114124134144154 unmodified lines42042142242342442517 unmodified lines# Set up API key in environment variable as expected by HfApiModelos.environ["HUGGINGFACE_API_TOKEN"] = os.getenv("HUGGINGFACE_API_KEY", "")# Initialize the standard search toolsddg_search_tool = DuckDuckGoSearchTool(max_results=10) # Default is 10 results# google_search_tool = GoogleSearchTool()# Initialize the VisitWebpageToolvisit_webpage_tool = VisitWebpageTool()351 unmodified lines# Import tool from Hubimage_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)@tooldef web_search(query: str) -> str:"""A tool that searches the web for information.Args:query: The search query stringReturns:Search results as text"""try:# Use the already initialized DuckDuckGo search toolreturn ddg_search_tool(query)except Exception as e:return f"Error performing web search for '{query}': {str(e)}"with open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)2 unmodified linestools=[final_answer,Sonar_Web_Search_Tool,web_search, # Added explicit web_search toolddg_search_tool, # Added DuckDuckGo search tool# google_search_tool, # Added Google search toolget_current_time_in_timezone,image_generation_tool,Dataset_Creator_Tool,Check_Dataset_Validity,visit_webpage_tool, # Added webpage visiting capability],max_steps=6,verbosity_level=1,4 unmodified linesprompt_templates=prompt_templates)# To fix the TypeError in Gradio_UI.py, you would need to modify that file# For now, we'll just use the agent directlytry:17 unmodified lines18192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263351 unmodified lines4154164174184194202 unmodified lines4234244254264274284294304314324334344 unmodified lines43944044144244344444544617 unmodified lines# Set up API key in environment variable as expected by HfApiModelos.environ["HUGGINGFACE_API_TOKEN"] = os.getenv("HUGGINGFACE_API_KEY", "")# Initialize search tools with fallback capabilitytry:# Try DuckDuckGo first (default)print("Initializing DuckDuckGo search tool...")ddg_search_tool = DuckDuckGoSearchTool(max_results=10)# Test the tool with a simple querytest_result = ddg_search_tool("test query")print("DuckDuckGo search tool initialized successfully.")# Use DuckDuckGo as the primary search toolprimary_search_tool = ddg_search_toolsearch_tool_name = "DuckDuckGo"except Exception as e:print(f"Error initializing DuckDuckGo search tool: {str(e)}")print("Falling back to Google search tool...")try:# Import GoogleSearchTool only if neededfrom smolagents import GoogleSearchToolgoogle_search_tool = GoogleSearchTool()# Test the Google search tooltest_result = google_search_tool("test query")print("Google search tool initialized successfully.")# Use Google as the fallback search toolprimary_search_tool = google_search_toolsearch_tool_name = "Google"except Exception as google_error:print(f"Error initializing Google search tool: {str(google_error)}")print("WARNING: No working search tool available. Agent functionality will be limited.")# Create a minimal replacement that returns an explanatory messagedef search_fallback(query):return f"Search functionality unavailable. Both DuckDuckGo and Google search tools failed to initialize. Query was: {query}"primary_search_tool = search_fallbacksearch_tool_name = "Unavailable"# Initialize the VisitWebpageToolvisit_webpage_tool = VisitWebpageTool()351 unmodified lines# Import tool from Hubimage_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)with open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)2 unmodified linestools=[final_answer,Sonar_Web_Search_Tool,primary_search_tool, # Use whichever search tool was successfully initializedget_current_time_in_timezone,image_generation_tool,Dataset_Creator_Tool,Check_Dataset_Validity,visit_webpage_tool,],max_steps=6,verbosity_level=1,4 unmodified linesprompt_templates=prompt_templates)print(f"Agent initialized with {search_tool_name} as primary search tool")# To fix the TypeError in Gradio_UI.py, you would need to modify that file# For now, we'll just use the agent directlytry:
app.py+44-23