Aramide commited on
Commit
6490027
·
verified ·
1 Parent(s): 67738bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -1,43 +1,45 @@
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
 
12
- # @tool
13
- # def web_search_tool(query: str, num_results: int = 3) -> str:
14
- # """A tool that performs a web search and fetches up-to-date information using DuckDuckGo.
15
 
16
- # Args:
17
- # query: The search query or topic to look up on the web.
18
- # num_results: The number of top search results to return (default is 3).
19
 
20
- # Returns:
21
- # A string containing summarized up-to-date information from the web.
22
- # """
23
- # try:
24
- # ddg = DuckDuckGoSearchTool()
25
- # results = ddg.text(query, max_results=num_results)
26
 
27
- # if not results:
28
- # return f"No results found for '{query}'."
29
 
30
- # info_list = []
31
- # for i, r in enumerate(results):
32
- # title = r.get("title", "No title")
33
- # body = r.get("body", "No summary available.")
34
- # link = r.get("href", "")
35
- # info_list.append(f"{i+1}. {title}\n{body}\n({link})")
36
 
37
- # return f"Here are the top {num_results} results for '{query}':\n\n" + "\n\n".join(info_list)
38
 
39
- # except Exception as e:
40
- # return f"Error performing web search for '{query}': {str(e)}"
41
 
42
 
43
 
@@ -72,15 +74,13 @@ custom_role_conversions=None,
72
 
73
  # Import tool from Hub
74
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
75
- # Create an agent and register the tool
76
- web_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model="Qwen2.5-Coder-32B-Instruct")
77
 
78
  with open("prompts.yaml", 'r') as stream:
79
  prompt_templates = yaml.safe_load(stream)
80
 
81
  agent = CodeAgent(
82
  model=model,
83
- tools=[final_answer, get_current_time_in_timezone, web_agent], ## add your tools here (don't remove final answer)
84
  max_steps=6,
85
  verbosity_level=1,
86
  grammar=None,
 
1
+ pip install duckduckgo_search
2
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
3
  import datetime
4
  import requests
5
  import pytz
6
  import yaml
7
+ from duckduckgo_search import DDGS
8
  from tools.final_answer import FinalAnswerTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
 
14
+ @tool
15
+ def web_search_tool(query: str, num_results: int = 3) -> str:
16
+ """A tool that performs a web search and fetches up-to-date information using DuckDuckGo.
17
 
18
+ Args:
19
+ query: The search query or topic to look up on the web.
20
+ num_results: The number of top search results to return (default is 3).
21
 
22
+ Returns:
23
+ A string containing summarized up-to-date information from the web.
24
+ """
25
+ try:
26
+ ddg = DuckDuckGoSearchTool()
27
+ results = ddg.text(query, max_results=num_results)
28
 
29
+ if not results:
30
+ return f"No results found for '{query}'."
31
 
32
+ info_list = []
33
+ for i, r in enumerate(results):
34
+ title = r.get("title", "No title")
35
+ body = r.get("body", "No summary available.")
36
+ link = r.get("href", "")
37
+ info_list.append(f"{i+1}. {title}\n{body}\n({link})")
38
 
39
+ return f"Here are the top {num_results} results for '{query}':\n\n" + "\n\n".join(info_list)
40
 
41
+ except Exception as e:
42
+ return f"Error performing web search for '{query}': {str(e)}"
43
 
44
 
45
 
 
74
 
75
  # Import tool from Hub
76
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
77
 
78
  with open("prompts.yaml", 'r') as stream:
79
  prompt_templates = yaml.safe_load(stream)
80
 
81
  agent = CodeAgent(
82
  model=model,
83
+ tools=[final_answer, get_current_time_in_timezone, web_search_tool], ## add your tools here (don't remove final answer)
84
  max_steps=6,
85
  verbosity_level=1,
86
  grammar=None,