Ishikawa7 commited on
Commit
9ffebce
·
verified ·
1 Parent(s): f866b07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -30
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import inspect
3
  from smolagents import CodeAgent, load_tool, InferenceClientModel
4
- from smolagents.tool import tool
5
  import math, ast, os, requests, pandas as pd
6
  # (Keep Constants as is)
7
  # --- Constants ---
@@ -11,68 +11,59 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
  @tool
12
  def calc(expr: str) -> str:
13
  """
14
- Evaluate an arithmetic expression safely.
15
- Args:
16
- expr (str): math expression like '2+3*sqrt(16)'.
17
- Returns:
18
- str: the numeric result or an error message.
19
  """
20
  try:
21
  allowed = {"sqrt": math.sqrt, "log": math.log, "pow": pow, "abs": abs, "round": round}
22
  tree = ast.parse(expr, mode="eval")
23
- for node in ast.walk(tree):
24
- if isinstance(node, ast.Name) and node.id not in allowed:
25
- return f"Error: '{node.id}' not allowed"
26
  return str(eval(compile(tree, "<", "eval"), {"__builtins__": {}}, allowed))
27
  except Exception as e:
28
  return f"Error: {e}"
29
 
30
- # --- Tool: Python Code Runner ---
31
  @tool
32
  def run_py(code: str) -> str:
33
  """
34
- Execute snippet and return printed output or resulting variables.
35
- Args:
36
- code (str): Python code block.
37
- Returns:
38
- str: result or error text.
39
  """
40
  try:
41
- gl = {}
42
- loc = {}
43
  exec(code, gl, loc)
44
- if loc:
45
- return str(loc)
46
- return ""
47
  except Exception as e:
48
  return f"Error: {e}"
49
 
50
- # --- Tool: File Downloader for GAIA ---
51
  @tool
52
  def download(task_id: str) -> str:
53
  """
54
- Download GAIA task file.
55
- Args:
56
- task_id (str): ID from GET /random-question.
57
- Returns:
58
- str: local file path or error.
59
  """
60
  base = os.getenv("DEFAULT_API_URL", "https://agents-course-unit4-scoring.hf.space")
61
  url = f"{base}/files/{task_id}"
62
  try:
63
- r = requests.get(url); r.raise_for_status()
64
  path = f"/tmp/{task_id}"
65
- with open(path, "wb") as f: f.write(r.content)
66
  return path
67
  except Exception as e:
68
  return f"Error: {e}"
69
 
 
70
  model = InferenceClientModel()
71
- agent = CodeAgent(tools=[calc, run_py, download], model=model)
 
 
 
 
 
72
 
73
  class ToolAgent:
74
  def __init__(self):
75
- self.agent = agent # from above
76
 
77
  def __call__(self, question: str) -> str:
78
  result = self.agent.run(question)
 
1
  import gradio as gr
2
  import inspect
3
  from smolagents import CodeAgent, load_tool, InferenceClientModel
4
+ from smolagents.tools import tool
5
  import math, ast, os, requests, pandas as pd
6
  # (Keep Constants as is)
7
  # --- Constants ---
 
11
  @tool
12
  def calc(expr: str) -> str:
13
  """
14
+ Safely evaluate a math expression like '2 + sqrt(9)'.
 
 
 
 
15
  """
16
  try:
17
  allowed = {"sqrt": math.sqrt, "log": math.log, "pow": pow, "abs": abs, "round": round}
18
  tree = ast.parse(expr, mode="eval")
19
+ for n in ast.walk(tree):
20
+ if isinstance(n, ast.Name) and n.id not in allowed:
21
+ return f"Error: '{n.id}' not allowed"
22
  return str(eval(compile(tree, "<", "eval"), {"__builtins__": {}}, allowed))
23
  except Exception as e:
24
  return f"Error: {e}"
25
 
26
+ # --- Tool: Python Runner ---
27
  @tool
28
  def run_py(code: str) -> str:
29
  """
30
+ Execute Python code snippet; returns local variables dictionary or errors.
 
 
 
 
31
  """
32
  try:
33
+ gl, loc = {}, {}
 
34
  exec(code, gl, loc)
35
+ return str(loc)
 
 
36
  except Exception as e:
37
  return f"Error: {e}"
38
 
39
+ # --- Tool: File Downloader ---
40
  @tool
41
  def download(task_id: str) -> str:
42
  """
43
+ Download a file for GAIA task_id; returns local file path.
 
 
 
 
44
  """
45
  base = os.getenv("DEFAULT_API_URL", "https://agents-course-unit4-scoring.hf.space")
46
  url = f"{base}/files/{task_id}"
47
  try:
48
+ resp = requests.get(url); resp.raise_for_status()
49
  path = f"/tmp/{task_id}"
50
+ with open(path, "wb") as f: f.write(resp.content)
51
  return path
52
  except Exception as e:
53
  return f"Error: {e}"
54
 
55
+ # --- Initialize the agent ---
56
  model = InferenceClientModel()
57
+ agent = CodeAgent(
58
+ tools=[calc, run_py, download],
59
+ model=model,
60
+ add_base_tools=True, # include default Python REPL and web search
61
+ additional_authorized_imports=["math", "requests", "pandas"]
62
+ )
63
 
64
  class ToolAgent:
65
  def __init__(self):
66
+ self.agent = agent
67
 
68
  def __call__(self, question: str) -> str:
69
  result = self.agent.run(question)