gokceKy commited on
Commit
b8b39db
·
verified ·
1 Parent(s): fa4af6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -12,39 +12,44 @@ from Gradio_UI import GradioUI
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":
 
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
+ Raises:
27
+ ValueError: If the input format is incorrect or an unsupported conversion is requested.
28
  """
29
  try:
30
+ # Allowed characters for security
31
  allowed_chars = set("0123456789CFcf toTO-.")
32
 
33
  if not all(c in allowed_chars for c in operation):
34
  return "Error: Invalid characters detected. Use the format '100 C to F'."
35
 
36
+ # Normalize input
37
  operation = operation.strip().upper().replace("TO", " to ")
38
 
39
+ # Parse the input format
40
  parts = operation.split()
41
  if len(parts) != 4 or parts[2] != "TO":
42
  return "Error: Invalid format. Use the format '100 C to F'."
43
 
44
  value, from_unit, _, to_unit = parts
45
 
46
+ # Ensure value is a valid number
47
  try:
48
  value = float(value)
49
  except ValueError:
50
  return "Error: Invalid temperature value."
51
 
52
+ # Perform conversion
53
  if from_unit == "C" and to_unit == "F":
54
  result = (value * 9/5) + 32
55
  elif from_unit == "F" and to_unit == "C":