gokceKy commited on
Commit
8cd0907
·
verified ·
1 Parent(s): 5f3617e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -11,30 +11,27 @@ from Gradio_UI import GradioUI
11
 
12
  @tool
13
  def temperature_converter(operation: str) -> str:
14
- """
15
- Converts temperatures between Celsius and Fahrenheit.
16
 
17
  Args:
18
- operation (str): A string representing the conversion request.
19
- The format should be "<value> <unit> to <target_unit>"
20
- (e.g., "100 C to F", "32 F to C").
21
- Supported units: 'C' (Celsius) and 'F' (Fahrenheit).
22
 
23
  Returns:
24
- str: The converted temperature result as a string.
25
-
26
  """
27
  try:
28
  # Allowed characters for security
29
- allowed_chars = set("0123456789CFcf toTO-.")
30
 
31
  if not all(c in allowed_chars for c in operation):
32
- return "Error: Invalid characters detected. Use the format '100 C to F'."
33
 
34
  # Normalize input
35
  operation = operation.strip().upper().replace("TO", " to ")
36
 
37
- # Parse the input format
38
  parts = operation.split()
39
  if len(parts) != 4 or parts[2] != "TO":
40
  return "Error: Invalid format. Use the format '100 C to F'."
@@ -58,7 +55,7 @@ def temperature_converter(operation: str) -> str:
58
  return f"Result: {value}°{from_unit} = {result:.2f}°{to_unit}"
59
 
60
  except Exception as e:
61
- return f"Error processing the conversion: {str(e)}"
62
 
63
  @tool
64
  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 (str): 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'."
 
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: