gokceKy commited on
Commit
985fec8
·
verified ·
1 Parent(s): 976c3b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -12
app.py CHANGED
@@ -10,24 +10,52 @@ from Gradio_UI import GradioUI
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:
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
 
12
  @tool
13
+ def temperature_converter(operation: str) -> str:
14
+ """
15
+ A secure tool that converts temperatures between Celsius and Fahrenheit.
16
+
17
  Args:
18
+ operation (str): A conversion request in the format "<value> <unit> to <target_unit>"
19
+ (e.g., "100 C to F", "32 F to C").
20
+
21
  Returns:
22
+ str: The converted temperature result or an error message.
23
  """
24
  try:
25
+ # Define allowed characters for input validation
26
+ allowed_chars = set("0123456789CFcf toTO-.")
27
+
28
  if not all(c in allowed_chars for c in operation):
29
+ return "Error: Invalid characters detected. Use the format '100 C to F'."
30
+
31
+ # Normalize input (remove extra spaces, make lowercase)
32
+ operation = operation.strip().upper().replace("TO", " to ")
33
+
34
+ # Parse the input format (e.g., "100 C to F")
35
+ parts = operation.split()
36
+ if len(parts) != 4 or parts[2] != "TO":
37
+ return "Error: Invalid format. Use the format '100 C to F'."
38
+
39
+ value, from_unit, _, to_unit = parts
40
+
41
+ # Ensure value is a valid float
42
+ try:
43
+ value = float(value)
44
+ except ValueError:
45
+ return "Error: Invalid temperature value."
46
+
47
+ # Perform the conversion
48
+ if from_unit == "C" and to_unit == "F":
49
+ result = (value * 9/5) + 32
50
+ elif from_unit == "F" and to_unit == "C":
51
+ result = (value - 32) * 5/9
52
+ else:
53
+ return "Error: Supported conversions are 'C to F' and 'F to C'."
54
+
55
+ return f"Result: {value}°{from_unit} = {result:.2f}°{to_unit}"
56
+
57
  except Exception as e:
58
+ return f"Error processing the conversion: {str(e)}"
59
 
60
  @tool
61
  def get_current_time_in_timezone(timezone: str) -> str: