Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
import base64
|
| 2 |
import requests
|
| 3 |
try:
|
| 4 |
-
from smolagents import DuckDuckGoSearchTool
|
| 5 |
from smolagents import Tool
|
| 6 |
except ImportError:
|
| 7 |
-
DuckDuckGoSearchTool = object
|
| 8 |
Tool = object
|
| 9 |
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -59,23 +57,27 @@ class ImageReaderTool:
|
|
| 59 |
return error_msg
|
| 60 |
|
| 61 |
|
| 62 |
-
#search web tool
|
| 63 |
-
class WebSearchTool
|
| 64 |
name = "web_search"
|
| 65 |
-
|
| 66 |
-
inputs = {
|
| 67 |
-
"query": {
|
| 68 |
-
"type": "string",
|
| 69 |
-
"description": "The query string to search for."
|
| 70 |
-
}
|
| 71 |
-
}
|
| 72 |
-
output_type = "string"
|
| 73 |
def __init__(self):
|
| 74 |
-
super().__init__()
|
| 75 |
print("WebSearchTool initialized.")
|
| 76 |
-
|
|
|
|
| 77 |
print(f"WebSearchTool received query (first 50 chars): {query[:50]}...")
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
|
|
|
| 1 |
import base64
|
| 2 |
import requests
|
| 3 |
try:
|
|
|
|
| 4 |
from smolagents import Tool
|
| 5 |
except ImportError:
|
|
|
|
| 6 |
Tool = object
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 57 |
return error_msg
|
| 58 |
|
| 59 |
|
| 60 |
+
#search web tool — uses ddgs directly to avoid smolagents DuckDuckGoSearchTool package check
|
| 61 |
+
class WebSearchTool:
|
| 62 |
name = "web_search"
|
| 63 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def __init__(self):
|
|
|
|
| 65 |
print("WebSearchTool initialized.")
|
| 66 |
+
|
| 67 |
+
def __call__(self, query: str) -> str:
|
| 68 |
print(f"WebSearchTool received query (first 50 chars): {query[:50]}...")
|
| 69 |
+
try:
|
| 70 |
+
from ddgs import DDGS
|
| 71 |
+
with DDGS() as ddgs:
|
| 72 |
+
results = list(ddgs.text(query, max_results=5))
|
| 73 |
+
if not results:
|
| 74 |
+
return "No results found."
|
| 75 |
+
output = "\n\n".join(
|
| 76 |
+
f"{r.get('title', '')}\n{r.get('href', '')}\n{r.get('body', '')}"
|
| 77 |
+
for r in results
|
| 78 |
+
)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
output = f"Search error: {e}"
|
| 81 |
+
print(f"WebSearchTool returning result (first 100 chars): {output[:100]}...")
|
| 82 |
+
return output
|
| 83 |
|