gokceKy commited on
Commit
f9e801a
·
verified ·
1 Parent(s): bb64a8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -11,27 +11,40 @@ from Gradio_UI import GradioUI
11
 
12
  @tool
13
  def temperature_converter(operation: str) -> str:
14
- """Converts temperatures between Celsius and Fahrenheit.
15
 
16
  Args:
17
- operation: Temperature conversion request in the format '100 C to F' or '32 F to C'.
 
 
18
 
19
  Returns:
20
- str: Converted temperature result or an error message.
21
  """
22
  try:
23
- parts = operation.strip().upper().split()
 
 
 
 
24
 
 
 
 
 
 
25
  if len(parts) != 4 or parts[2] != "TO":
26
- return "Error: Use the format '100 C to F' or '32 F to C'."
27
 
28
  value, from_unit, _, to_unit = parts
29
 
 
30
  try:
31
  value = float(value)
32
  except ValueError:
33
- return "Error: Invalid number format."
34
 
 
35
  if from_unit == "C" and to_unit == "F":
36
  result = (value * 9/5) + 32
37
  elif from_unit == "F" and to_unit == "C":
@@ -39,11 +52,10 @@ def temperature_converter(operation: str) -> str:
39
  else:
40
  return "Error: Supported conversions are 'C to F' and 'F to C'."
41
 
42
- return f"{value}°{from_unit} = {result:.2f}°{to_unit}"
43
-
44
- except Exception:
45
- return "Error: Could not process the conversion."
46
 
 
 
47
 
48
  @tool
49
  def get_current_time_in_timezone(timezone: str) -> str:
 
11
 
12
  @tool
13
  def temperature_converter(operation: str) -> str:
14
+ """A secure temperature converter tool that evaluates basic temperature conversions.
15
 
16
  Args:
17
+ operation: The conversion request in the format "<value> <unit> to <target_unit>"
18
+ (e.g., "100 C to F", "32 F to C").
19
+ Supports conversions between Celsius ('C') and Fahrenheit ('F').
20
 
21
  Returns:
22
+ str: The converted temperature result or an error message.
23
  """
24
  try:
25
+ # Allowed characters for security
26
+ allowed_chars = set("0123456789CFcf toTO-. ")
27
+
28
+ if not all(c in allowed_chars for c in operation):
29
+ return "Error: Only 'C' (Celsius) and 'F' (Fahrenheit) conversions are allowed."
30
 
31
+ # Normalize input
32
+ operation = operation.strip().upper().replace("TO", " to ")
33
+
34
+ # Ensure format is correct
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 number
42
  try:
43
  value = float(value)
44
  except ValueError:
45
+ return "Error: Invalid temperature value."
46
 
47
+ # Perform 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":
 
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 converting {operation}: {str(e)}"
59
 
60
  @tool
61
  def get_current_time_in_timezone(timezone: str) -> str: