Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,79 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
from smolagent import Agent, Tool
|
| 14 |
+
from duckduckgo_search import ddg
|
| 15 |
+
import sympy
|
| 16 |
+
import re
|
| 17 |
+
|
| 18 |
+
class WikipediaSearchTool(Tool):
|
| 19 |
+
name = "wikipedia_search"
|
| 20 |
+
description = "Searches for answers using Wikipedia"
|
| 21 |
+
|
| 22 |
+
def use(self, input: str) -> str:
|
| 23 |
+
results = ddg(input, max_results=3)
|
| 24 |
+
if results:
|
| 25 |
+
return results[0]["body"]
|
| 26 |
+
return "No information found."
|
| 27 |
+
|
| 28 |
+
class MathSolverTool(Tool):
|
| 29 |
+
name = "math_solver"
|
| 30 |
+
description = "Solves math expressions and equations"
|
| 31 |
+
|
| 32 |
+
def use(self, input: str) -> str:
|
| 33 |
+
try:
|
| 34 |
+
expression = sympy.sympify(input)
|
| 35 |
+
result = expression.evalf()
|
| 36 |
+
return str(result)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Math Error: {e}"
|
| 39 |
+
|
| 40 |
+
class CodeExecutionTool(Tool):
|
| 41 |
+
name = "python_executor"
|
| 42 |
+
description = "Executes Python code snippets and returns output"
|
| 43 |
+
|
| 44 |
+
def use(self, input: str) -> str:
|
| 45 |
+
try:
|
| 46 |
+
local_vars = {}
|
| 47 |
+
exec(f"result = {input}", {}, local_vars)
|
| 48 |
+
return str(local_vars["result"])
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Code Error: {e}"
|
| 51 |
+
|
| 52 |
+
def classify_question(q: str) -> str:
|
| 53 |
+
q_lower = q.lower()
|
| 54 |
+
if any(keyword in q_lower for keyword in ["solve", "evaluate", "calculate", "math"]):
|
| 55 |
+
return "math"
|
| 56 |
+
elif any(keyword in q_lower for keyword in ["code", "python", "return", "function"]):
|
| 57 |
+
return "code"
|
| 58 |
+
elif any(keyword in q_lower for keyword in ["who", "what", "where", "when", "why", "how", "capital", "invented"]):
|
| 59 |
+
return "search"
|
| 60 |
+
else:
|
| 61 |
+
return "default"
|
| 62 |
+
|
| 63 |
class BasicAgent:
|
| 64 |
def __init__(self):
|
| 65 |
+
self.agent = Agent(
|
| 66 |
+
tools=[
|
| 67 |
+
WikipediaSearchTool(),
|
| 68 |
+
MathSolverTool(),
|
| 69 |
+
CodeExecutionTool(),
|
| 70 |
+
]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
def __call__(self, question: str) -> str:
|
| 74 |
+
category = classify_question(question)
|
| 75 |
+
print(f"Routing question: {question[:50]}... -> [{category}]")
|
| 76 |
+
|
| 77 |
+
if category == "math":
|
| 78 |
+
return MathSolverTool().use(question)
|
| 79 |
+
elif category == "code":
|
| 80 |
+
return CodeExecutionTool().use(question)
|
| 81 |
+
elif category == "search":
|
| 82 |
+
return WikipediaSearchTool().use(question)
|
| 83 |
+
else:
|
| 84 |
+
return self.agent.run(question)
|
| 85 |
+
|
| 86 |
|
| 87 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 88 |
"""
|