Update app.py
Browse files
app.py
CHANGED
|
@@ -11,27 +11,46 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 11 |
# ====================== CUSTOM TOOLS ======================
|
| 12 |
# Add any tools you want here. The more useful ones you add, the better your score.
|
| 13 |
|
|
|
|
|
|
|
| 14 |
@tool
|
| 15 |
def web_search(query: str) -> str:
|
| 16 |
-
"""Perform a web search and return
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
from duckduckgo_search import DDGS
|
| 21 |
with DDGS() as ddgs:
|
| 22 |
results = list(ddgs.text(query, max_results=5))
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
return f"
|
| 26 |
|
| 27 |
|
| 28 |
@tool
|
| 29 |
def calculate(expression: str) -> str:
|
| 30 |
-
"""Evaluate a mathematical expression
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
-
# You can use sympy for more complex math if you install it
|
| 33 |
import math
|
| 34 |
-
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
return f"Calculation error: {str(e)}"
|
| 37 |
|
|
|
|
| 11 |
# ====================== CUSTOM TOOLS ======================
|
| 12 |
# Add any tools you want here. The more useful ones you add, the better your score.
|
| 13 |
|
| 14 |
+
# ====================== CUSTOM TOOLS ======================
|
| 15 |
+
|
| 16 |
@tool
|
| 17 |
def web_search(query: str) -> str:
|
| 18 |
+
"""Perform a web search using DuckDuckGo and return the top results.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
query: The search query to look up on the web.
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
str: A string containing the title and snippet of the top search results.
|
| 25 |
+
"""
|
| 26 |
try:
|
| 27 |
from duckduckgo_search import DDGS
|
| 28 |
with DDGS() as ddgs:
|
| 29 |
results = list(ddgs.text(query, max_results=5))
|
| 30 |
+
if not results:
|
| 31 |
+
return "No search results found."
|
| 32 |
+
formatted = []
|
| 33 |
+
for r in results:
|
| 34 |
+
formatted.append(f"Title: {r.get('title', 'N/A')}\nSnippet: {r.get('body', r.get('snippet', 'N/A'))}")
|
| 35 |
+
return "\n\n".join(formatted)
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Web search failed: {str(e)}"
|
| 38 |
|
| 39 |
|
| 40 |
@tool
|
| 41 |
def calculate(expression: str) -> str:
|
| 42 |
+
"""Evaluate a simple mathematical expression.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
expression: The math expression to calculate (e.g. '2 + 2 * 3', 'sin(3.14)', etc.)
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
str: The result of the calculation as a string.
|
| 49 |
+
"""
|
| 50 |
try:
|
|
|
|
| 51 |
import math
|
| 52 |
+
allowed_names = {"math": math}
|
| 53 |
+
return str(eval(expression, {"__builtins__": {}}, allowed_names))
|
| 54 |
except Exception as e:
|
| 55 |
return f"Calculation error: {str(e)}"
|
| 56 |
|