Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 14 |
-
"""
|
| 15 |
-
|
|
|
|
| 16 |
Args:
|
| 17 |
-
operation:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
Returns:
|
| 21 |
-
str: The
|
| 22 |
"""
|
| 23 |
try:
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
if not all(c in allowed_chars for c in operation):
|
| 26 |
-
return "Error:
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
return f"Error
|
| 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:
|