Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,12 +8,14 @@ from tools.final_answer import FinalAnswerTool
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
from typing import List
|
11 |
-
|
|
|
|
|
12 |
|
13 |
@tool
|
14 |
def get_web_search(query: str, max_results: int = 3) -> str:
|
15 |
-
|
16 |
-
A tool that searches for relevant information on the internet.
|
17 |
|
18 |
Args:
|
19 |
query: The search term or topic to look up.
|
@@ -22,15 +24,20 @@ def get_web_search(query: str, max_results: int = 3) -> str:
|
|
22 |
Returns:
|
23 |
A string containing summarized relevant information about the topic.
|
24 |
"""
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
result_summaries.append(f"{i+1}. {result['title']}: {result['snippet']}")
|
33 |
|
|
|
34 |
return "\n".join(result_summaries)
|
35 |
|
36 |
@tool
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
from typing import List
|
11 |
+
import requests
|
12 |
+
from bs4 import BeautifulSoup
|
13 |
+
|
14 |
|
15 |
@tool
|
16 |
def get_web_search(query: str, max_results: int = 3) -> str:
|
17 |
+
"""
|
18 |
+
A tool that searches for relevant information on the internet without using an API.
|
19 |
|
20 |
Args:
|
21 |
query: The search term or topic to look up.
|
|
|
24 |
Returns:
|
25 |
A string containing summarized relevant information about the topic.
|
26 |
"""
|
27 |
+
search_url = f"https://www.google.com/search?q={query}"
|
28 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
29 |
+
response = requests.get(search_url, headers=headers)
|
30 |
+
|
31 |
+
if response.status_code != 200:
|
32 |
+
return "Failed to retrieve search results."
|
33 |
|
34 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
35 |
+
result_elements = soup.find_all("h3")[:max_results]
|
36 |
|
37 |
+
if not result_elements:
|
38 |
+
return "No relevant information found."
|
|
|
39 |
|
40 |
+
result_summaries = [f"{i+1}. {elem.get_text()}" for i, elem in enumerate(result_elements)]
|
41 |
return "\n".join(result_summaries)
|
42 |
|
43 |
@tool
|