Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,20 +8,86 @@ import pandas as pd
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"Agent received
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
@@ -40,7 +106,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
+
# ---- Tool 1: Calculator ----
|
| 12 |
+
def safe_eval_math(expr: str) -> str:
|
| 13 |
+
try:
|
| 14 |
+
# Only allow safe functions and math operators
|
| 15 |
+
allowed_names = {"sqrt": math.sqrt, "log": math.log, "pow": pow, "abs": abs, "round": round}
|
| 16 |
+
code = compile(expr, "<string>", "eval")
|
| 17 |
+
for node in ast.walk(code):
|
| 18 |
+
if isinstance(node, ast.Name) and node.id not in allowed_names:
|
| 19 |
+
raise ValueError(f"Use of '{node.id}' not allowed")
|
| 20 |
+
return str(eval(code, {"__builtins__": {}}, allowed_names))
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Error: {e}"
|
| 23 |
+
|
| 24 |
+
calculator_tool = Tool(
|
| 25 |
+
name="calculator",
|
| 26 |
+
description="Evaluates basic math expressions like '2 + 2 * sqrt(9)'",
|
| 27 |
+
func=safe_eval_math,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# ---- Tool 2: Python Code Evaluator ----
|
| 31 |
+
def run_python_code(code: str) -> str:
|
| 32 |
+
try:
|
| 33 |
+
# Eval in restricted namespace
|
| 34 |
+
local_vars = {}
|
| 35 |
+
exec(code, {"__builtins__": {}}, local_vars)
|
| 36 |
+
return str(local_vars)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Code Error: {e}"
|
| 39 |
+
|
| 40 |
+
python_tool = Tool(
|
| 41 |
+
name="python_eval",
|
| 42 |
+
description="Runs simple Python code blocks and returns the result. Use this for code-based reasoning.",
|
| 43 |
+
func=run_python_code,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# ---- Tool 3: File Downloader (Optional, if GAIA task uses /files/{task_id}) ----
|
| 47 |
+
def download_file(task_id: str) -> str:
|
| 48 |
+
base_url = os.getenv("DEFAULT_API_URL", "https://agents-course-unit4-scoring.hf.space")
|
| 49 |
+
url = f"{base_url}/files/{task_id}"
|
| 50 |
+
try:
|
| 51 |
+
r = requests.get(url)
|
| 52 |
+
r.raise_for_status()
|
| 53 |
+
file_path = f"/tmp/{task_id}"
|
| 54 |
+
with open(file_path, "wb") as f:
|
| 55 |
+
f.write(r.content)
|
| 56 |
+
return f"File downloaded to {file_path}"
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"Failed to download file: {e}"
|
| 59 |
+
|
| 60 |
+
file_tool = Tool(
|
| 61 |
+
name="download_file",
|
| 62 |
+
description="Downloads file for a given GAIA task_id.",
|
| 63 |
+
func=download_file,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# ---- The Smart Tool Agent ----
|
| 67 |
+
class ToolAgent:
|
| 68 |
def __init__(self):
|
| 69 |
+
print("Initializing ToolAgent with smolagents...")
|
| 70 |
+
self.agent = ToolUseAgent(
|
| 71 |
+
tools=[calculator_tool, python_tool, file_tool],
|
| 72 |
+
system_prompt=(
|
| 73 |
+
"You are a helpful agent for solving GAIA benchmark questions. "
|
| 74 |
+
"Use tools when appropriate. Return only the exact answer."
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
def __call__(self, question: str) -> str:
|
| 79 |
+
print(f"Agent received: {question[:80]}")
|
| 80 |
+
try:
|
| 81 |
+
result = self.agent.run(question)
|
| 82 |
+
print(f"Raw Result: {result}")
|
| 83 |
+
return result.strip()
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error while answering: {e}")
|
| 86 |
+
return "Agent error"
|
| 87 |
|
| 88 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 89 |
"""
|
| 90 |
+
Fetches all questions, runs the ToolAgent on them, submits all answers,
|
| 91 |
and displays the results.
|
| 92 |
"""
|
| 93 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
|
|
| 106 |
|
| 107 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 108 |
try:
|
| 109 |
+
agent = ToolAgent()
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Error instantiating agent: {e}")
|
| 112 |
return f"Error initializing agent: {e}", None
|