Maximofn commited on
Commit
c2a2c27
·
verified ·
1 Parent(s): a07f0a2

Update tools

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -1,8 +1,10 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, 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
@@ -20,16 +22,32 @@ def web_search(query:str)-> str:
20
  return f"Error searching on internet the query '{query}': {str(e)}"
21
 
22
  @tool
23
- def visit_web(url:str)-> str:
24
- """A tool that obtains the content of a website in markdown form.
 
25
  Args:
26
- url: A string with the URL of the website
 
 
 
27
  """
28
  try:
29
- content = VisitWebpageTool(url)
30
- return content
 
 
 
 
 
 
 
 
 
 
 
 
31
  except Exception as e:
32
- return f"Error fetching page '{url}' content: {str(e)}"
33
 
34
  @tool
35
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -48,6 +66,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
48
 
49
 
50
  final_answer = FinalAnswerTool()
 
51
 
52
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
53
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
@@ -68,7 +87,7 @@ with open("prompts.yaml", 'r') as stream:
68
 
69
  agent = CodeAgent(
70
  model=model,
71
- tools=[final_answer, web_search, visit_web],
72
  max_steps=6,
73
  verbosity_level=1,
74
  grammar=None,
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
+ from markdownify import markdownify
7
+ import re
8
  from tools.final_answer import FinalAnswerTool
9
 
10
  from Gradio_UI import GradioUI
 
22
  return f"Error searching on internet the query '{query}': {str(e)}"
23
 
24
  @tool
25
+ def visit_webpage(url: str) -> str:
26
+ """Visits a webpage at the given URL and returns its content as a markdown string.
27
+
28
  Args:
29
+ url: The URL of the webpage to visit.
30
+
31
+ Returns:
32
+ The content of the webpage converted to Markdown, or an error message if the request fails.
33
  """
34
  try:
35
+ # Send a GET request to the URL
36
+ response = requests.get(url)
37
+ response.raise_for_status() # Raise an exception for bad status codes
38
+
39
+ # Convert the HTML content to Markdown
40
+ markdown_content = markdownify(response.text).strip()
41
+
42
+ # Remove multiple line breaks
43
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
44
+
45
+ return markdown_content
46
+
47
+ except RequestException as e:
48
+ return f"Error fetching the webpage: {str(e)}"
49
  except Exception as e:
50
+ return f"An unexpected error occurred: {str(e)}"
51
 
52
  @tool
53
  def get_current_time_in_timezone(timezone: str) -> str:
 
66
 
67
 
68
  final_answer = FinalAnswerTool()
69
+ web_searcher = DuckDuckGoSearchTool(max_results=5)
70
 
71
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
72
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
87
 
88
  agent = CodeAgent(
89
  model=model,
90
+ tools=[final_answer, web_searcher, visit_webpage],
91
  max_steps=6,
92
  verbosity_level=1,
93
  grammar=None,