zerito commited on
Commit
ea64f5a
·
verified ·
1 Parent(s): b1531aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -5,11 +5,10 @@ from tools.final_answer import FinalAnswerTool
5
  from Gradio_UI import GradioUI
6
  import markdownify
7
  import re
8
- from smolagents.tools.web import web_search as web_search_tool # Import web search from SmolAgents
9
 
10
  @tool
11
  def web_search(query: str) -> str:
12
- """Performs a web search and returns the top search results.
13
 
14
  Args:
15
  query: The search query string.
@@ -18,10 +17,25 @@ def web_search(query: str) -> str:
18
  A formatted string with the top search results, or an error message if the request fails.
19
  """
20
  try:
21
- results = web_search_tool(query)
22
- return results if results else "No relevant results found."
23
- except Exception as e:
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  return f"Error performing web search: {str(e)}"
 
 
25
 
26
  @tool
27
  def visit_webpage(url: str) -> str:
@@ -75,7 +89,7 @@ final_answer = FinalAnswerTool()
75
 
76
  # Initialize the model
77
  model = HfApiModel(
78
- max_tokens=1000, # Adjusted to avoid token limit errors
79
  temperature=0.5,
80
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
81
  custom_role_conversions=None,
 
5
  from Gradio_UI import GradioUI
6
  import markdownify
7
  import re
 
8
 
9
  @tool
10
  def web_search(query: str) -> str:
11
+ """Performs a web search using DuckDuckGo and returns the top search results.
12
 
13
  Args:
14
  query: The search query string.
 
17
  A formatted string with the top search results, or an error message if the request fails.
18
  """
19
  try:
20
+ url = f"https://duckduckgo.com/html/?q={query.replace(' ', '+')}"
21
+ headers = {"User-Agent": "Mozilla/5.0"}
22
+ response = requests.get(url, headers=headers)
23
+ response.raise_for_status()
24
+
25
+ # Extract search results
26
+ search_results = re.findall(r'<a rel="nofollow" class="result__url" href="(.*?)">', response.text)
27
+
28
+ if not search_results:
29
+ return "No relevant results found."
30
+
31
+ # Format the top results
32
+ results_str = "\n".join([f"{i+1}. {result}" for i, result in enumerate(search_results[:5])])
33
+ return f"Top search results:\n{results_str}"
34
+
35
+ except RequestException as e:
36
  return f"Error performing web search: {str(e)}"
37
+ except Exception as e:
38
+ return f"An unexpected error occurred: {str(e)}"
39
 
40
  @tool
41
  def visit_webpage(url: str) -> str:
 
89
 
90
  # Initialize the model
91
  model = HfApiModel(
92
+ max_tokens=1100, # Adjusted to avoid token limit errors
93
  temperature=0.5,
94
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
95
  custom_role_conversions=None,