gokceKy commited on
Commit
976c3b8
·
verified ·
1 Parent(s): 8f3f9bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -8,25 +8,26 @@ from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
11
  @tool
12
  def calculator(operation: str) -> str:
13
- """
14
- A simple calculator that evaluates a basic arithmetic operation given as a string.
15
-
16
  Args:
17
- operation (str): A mathematical expression (e.g., '5 + 3', '10 - 2', '4 * 7', '20 / 5').
18
-
 
19
  Returns:
20
- str: The result of the calculation as a string.
21
-
22
- Raises:
23
- ValueError: If the operation is invalid or cannot be computed.
24
  """
25
  try:
26
- result = eval(operation)
27
- return str(result)
 
 
 
28
  except Exception as e:
29
- raise ValueError(f"Invalid operation: {operation}. Error: {str(e)}")
30
 
31
  @tool
32
  def get_current_time_in_timezone(timezone: str) -> str:
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
+
12
  @tool
13
  def calculator(operation: str) -> str:
14
+ """A simple calculator tool that safely evaluates basic math expressions.
15
+
 
16
  Args:
17
+ operation: The mathematical expression to evaluate (e.g., "2 + 2", "5 * 3").
18
+ Supports basic operations (+, -, *, /) and parentheses.
19
+
20
  Returns:
21
+ str: The result of the calculation or an error message
 
 
 
22
  """
23
  try:
24
+ allowed_chars = set("0123456789+-*/ .()")
25
+ if not all(c in allowed_chars for c in operation):
26
+ return "Error: Only basic math operations allowed"
27
+ result = eval(operation, {"__builtins__": {}})
28
+ return f"Result: {result}"
29
  except Exception as e:
30
+ return f"Error calculating {operation}: {str(e)}"
31
 
32
  @tool
33
  def get_current_time_in_timezone(timezone: str) -> str: