Spaces:
Sleeping
Sleeping
Redirect to DuckDuckGo as a tool
Browse files
app.py
CHANGED
|
@@ -4,41 +4,51 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
-
def extract_answer(self, thoughts: str) -> str:
|
| 35 |
-
# Normally, use selected answer from reasoning.
|
| 36 |
-
# For now, this placeholder returns demo result.
|
| 37 |
-
# Replace demo logic with answer extraction, format as per rules:
|
| 38 |
-
# - if a number is needed, just plain digits (no commas or units unless specified)
|
| 39 |
-
# - if string, no articles or abbreviations, digits written out unless specified
|
| 40 |
-
# - if list, elements comma separated, each following corresponding rules
|
| 41 |
-
return "42"
|
| 42 |
|
| 43 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 44 |
"""
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
import re
|
| 8 |
+
import requests
|
| 9 |
+
from duckduckgo_search import DDGS
|
| 10 |
+
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
# --- Basic Agent Definition ---
|
| 16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 17 |
+
|
| 18 |
+
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
print("BasicAgent initialized.")
|
| 22 |
+
self.searcher = DDGS()
|
| 23 |
+
|
| 24 |
+
def web_search(self, query: str, max_results: int = 3) -> str:
|
| 25 |
+
"""Perform a DuckDuckGo search and return snippets."""
|
| 26 |
+
try:
|
| 27 |
+
results = list(self.searcher.text(query, max_results=max_results))
|
| 28 |
+
if not results:
|
| 29 |
+
return "No search results found."
|
| 30 |
+
# Concatenate top snippets
|
| 31 |
+
snippets = [r.get("body", "") for r in results if "body" in r]
|
| 32 |
+
return " ".join(snippets[:3])
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Search error: {e}"
|
| 35 |
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 38 |
+
|
| 39 |
+
q = question.strip()
|
| 40 |
+
|
| 41 |
+
# If it looks like a math problem, handle locally
|
| 42 |
+
if re.search(r"\d+\s*[\+\-\*\/]\s*\d+", q):
|
| 43 |
+
try:
|
| 44 |
+
return str(eval(q))
|
| 45 |
+
except Exception:
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
# Otherwise: do a web search
|
| 49 |
+
print("Delegating to web search...")
|
| 50 |
+
snippets = self.web_search(q, max_results=3)
|
| 51 |
+
return snippets if snippets else "I could not find an answer."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 54 |
"""
|