Spaces:
Sleeping
Sleeping
tools approach
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import pandas as pd
|
|
| 6 |
|
| 7 |
import re
|
| 8 |
import requests
|
| 9 |
-
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
@@ -14,41 +14,51 @@ 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 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 54 |
"""
|
|
|
|
| 6 |
|
| 7 |
import re
|
| 8 |
import requests
|
| 9 |
+
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
|
|
| 14 |
|
| 15 |
# --- Basic Agent Definition ---
|
| 16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 17 |
+
class MathTool:
|
| 18 |
+
def run(self, query):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
+
return str(eval(query))
|
| 21 |
+
except Exception:
|
| 22 |
+
return "Math error."
|
| 23 |
+
|
| 24 |
+
class GoogleSearchTool:
|
| 25 |
+
def __init__(self, api_key, engine_id):
|
| 26 |
+
self.api_key = api_key
|
| 27 |
+
self.engine_id = engine_id
|
| 28 |
+
|
| 29 |
+
def run(self, query):
|
| 30 |
+
url = "https://www.googleapis.com/customsearch/v1"
|
| 31 |
+
params = {"key": self.api_key, "cx": self.engine_id, "q": query}
|
| 32 |
+
r = requests.get(url, params=params).json()
|
| 33 |
+
items = r.get("items", [])
|
| 34 |
+
return " ".join([item["snippet"] for item in items[:3]])
|
| 35 |
+
|
| 36 |
+
class WikipediaTool:
|
| 37 |
+
def run(self, query):
|
| 38 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
|
| 39 |
+
r = requests.get(url)
|
| 40 |
+
if r.status_code == 200:
|
| 41 |
+
return r.json().get("extract", "")
|
| 42 |
+
return "No wiki entry found."
|
| 43 |
|
| 44 |
+
class BasicAgent:
|
| 45 |
+
def __init__(self, tools):
|
| 46 |
+
self.tools = tools
|
| 47 |
+
|
| 48 |
+
def __call__(self, question: str):
|
| 49 |
+
print(f"Reasoning on: {question[:50]}...")
|
| 50 |
+
|
| 51 |
+
# Decide which tool to use
|
| 52 |
+
if re.search(r"\d+\s*[\+\-\*\/]\s*\d+", question):
|
| 53 |
+
return self.tools["math"].run(question)
|
| 54 |
+
|
| 55 |
+
if "who" in question.lower() or "what" in question.lower():
|
| 56 |
+
wiki_ans = self.tools["wiki"].run(question)
|
| 57 |
+
if wiki_ans:
|
| 58 |
+
return wiki_ans
|
| 59 |
+
|
| 60 |
+
# Default: search
|
| 61 |
+
return self.tools["google"].run(question)
|
| 62 |
|
| 63 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 64 |
"""
|