Spaces:
Runtime error
Runtime error
Update tools/unit_conversion.py
Browse files- tools/unit_conversion.py +30 -23
tools/unit_conversion.py
CHANGED
|
@@ -1,26 +1,33 @@
|
|
| 1 |
-
|
| 2 |
-
def __init__(self):
|
| 3 |
-
self.conversion_factors = {
|
| 4 |
-
("miles", "kilometers"): 1.60934,
|
| 5 |
-
("kilometers", "miles"): 0.621371,
|
| 6 |
-
("pounds", "kilograms"): 0.453592,
|
| 7 |
-
("kilograms", "pounds"): 2.20462,
|
| 8 |
-
("celsius", "fahrenheit"): lambda c: (c * 9/5) + 32,
|
| 9 |
-
("fahrenheit", "celsius"): lambda f: (f - 32) * 5/9
|
| 10 |
-
}
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
if
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
@tool
|
| 4 |
+
def unit_conversion(value: float, from_unit: str, to_unit: str) -> str:
|
| 5 |
+
"""A tool for converting units like distance, weight, and temperature.
|
| 6 |
+
|
| 7 |
+
Args:
|
| 8 |
+
value: The numerical value to convert.
|
| 9 |
+
from_unit: The unit of the input value (e.g., 'miles', 'kilograms', 'celsius').
|
| 10 |
+
to_unit: The desired unit (e.g., 'kilometers', 'pounds', 'fahrenheit').
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
A string describing the converted value.
|
| 14 |
+
"""
|
| 15 |
|
| 16 |
+
conversion_factors = {
|
| 17 |
+
("miles", "kilometers"): 1.60934,
|
| 18 |
+
("kilometers", "miles"): 0.621371,
|
| 19 |
+
("kilograms", "pounds"): 2.20462,
|
| 20 |
+
("pounds", "kilograms"): 0.453592,
|
| 21 |
+
}
|
| 22 |
|
| 23 |
+
# Temperature conversions
|
| 24 |
+
if from_unit == "celsius" and to_unit == "fahrenheit":
|
| 25 |
+
result = (value * 9/5) + 32
|
| 26 |
+
elif from_unit == "fahrenheit" and to_unit == "celsius":
|
| 27 |
+
result = (value - 32) * 5/9
|
| 28 |
+
elif (from_unit, to_unit) in conversion_factors:
|
| 29 |
+
result = value * conversion_factors[(from_unit, to_unit)]
|
| 30 |
+
else:
|
| 31 |
+
return f"Sorry, conversion from {from_unit} to {to_unit} is not supported."
|
| 32 |
+
|
| 33 |
+
return f"{value} {from_unit} is {round(result, 2)} {to_unit}."
|