MartinHummel commited on
Commit
07dcb79
Β·
1 Parent(s): c574549
Files changed (3) hide show
  1. .gitignore +5 -0
  2. app.py +9 -0
  3. tools/tool_math.py +10 -10
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ .git
2
+ tools/__pycache__
3
+ *.py~
4
+ __pycache__/
5
+
app.py CHANGED
@@ -29,12 +29,17 @@ class MathSolverAgent:
29
  try:
30
  if random.random() < 0.5:
31
  raise ValueError("Simulating incorrect or skipped answer.")
 
32
  solution = self.tools[0](question)
 
 
 
33
  explanation = self.tools[1](question)
34
  return f"{solution}\nExplanation:\n{explanation}"
35
  except Exception as e:
36
  return "Sorry, I couldn't solve that one."
37
 
 
38
  def run_and_submit_all(profile: gr.OAuthProfile | None):
39
  space_id = os.getenv("SPACE_ID")
40
 
@@ -139,7 +144,11 @@ with gr.Blocks() as demo:
139
 
140
  def run_manual_input(user_input):
141
  agent = MathSolverAgent()
 
 
142
  return agent(user_input)
 
 
143
 
144
  manual_test_button.click(fn=run_manual_input, inputs=manual_input, outputs=manual_output)
145
 
 
29
  try:
30
  if random.random() < 0.5:
31
  raise ValueError("Simulating incorrect or skipped answer.")
32
+
33
  solution = self.tools[0](question)
34
+ if solution.startswith("Error"):
35
+ return solution
36
+
37
  explanation = self.tools[1](question)
38
  return f"{solution}\nExplanation:\n{explanation}"
39
  except Exception as e:
40
  return "Sorry, I couldn't solve that one."
41
 
42
+
43
  def run_and_submit_all(profile: gr.OAuthProfile | None):
44
  space_id = os.getenv("SPACE_ID")
45
 
 
144
 
145
  def run_manual_input(user_input):
146
  agent = MathSolverAgent()
147
+ user_input = user_input.strip()
148
+ print(f"Manual input received: {user_input}")
149
  return agent(user_input)
150
+ #agent = MathSolverAgent()
151
+ #return agent(user_input)
152
 
153
  manual_test_button.click(fn=run_manual_input, inputs=manual_input, outputs=manual_output)
154
 
tools/tool_math.py CHANGED
@@ -1,26 +1,26 @@
1
- from transformers import Tool
2
- from sympy import symbols, Eq, solve, simplify, diff, integrate, sympify
3
 
4
- class SolveEquationTool(Tool):
5
  name = "solve_equation"
6
  description = "Solves a basic equation for x. Format: '2*x + 3 = 7'"
7
 
8
  def __call__(self, equation: str) -> str:
9
  x = symbols('x')
10
  try:
 
 
11
  lhs, rhs = equation.split("=")
12
  eq = Eq(sympify(lhs), sympify(rhs))
13
  sol = solve(eq, x)
14
  return f"x = {sol}"
15
  except Exception as e:
16
- return f"Error solving equation: {str(e)}"
17
 
18
- class ExplainSolutionTool(Tool):
19
  name = "explain_solution"
20
- description = "Explains the steps to solve a math problem."
21
 
22
  def __call__(self, problem: str) -> str:
23
- # Minimalist inline explanation; in a real agent, call an LLM
24
- if "2*x + 3 = 7" in problem:
25
- return "Step 1: Subtract 3 from both sides β†’ 2x = 4\nStep 2: Divide both sides by 2 β†’ x = 2"
26
- return "This agent only explains '2*x + 3 = 7' as a hardcoded example. Extend me with an LLM!"
 
1
+ from sympy import symbols, Eq, solve, sympify
 
2
 
3
+ class SolveEquationTool:
4
  name = "solve_equation"
5
  description = "Solves a basic equation for x. Format: '2*x + 3 = 7'"
6
 
7
  def __call__(self, equation: str) -> str:
8
  x = symbols('x')
9
  try:
10
+ if "=" not in equation:
11
+ return "Error: Equation must contain '=' sign. E.g., '2*x + 3 = 7'"
12
  lhs, rhs = equation.split("=")
13
  eq = Eq(sympify(lhs), sympify(rhs))
14
  sol = solve(eq, x)
15
  return f"x = {sol}"
16
  except Exception as e:
17
+ return f"Error: {str(e)}"
18
 
19
+ class ExplainSolutionTool:
20
  name = "explain_solution"
21
+ description = "Provides a hardcoded explanation for a known equation."
22
 
23
  def __call__(self, problem: str) -> str:
24
+ if "2*x + 3 = 7" in problem.replace(" ", ""):
25
+ return "Step 1: Subtract 3 β†’ 2x = 4\nStep 2: Divide by 2 β†’ x = 2"
26
+ return "This agent currently only explains '2*x + 3 = 7'. Extend this for more coverage."