karenwky commited on
Commit
789270d
·
verified ·
1 Parent(s): 2428de5

Upload tool

Browse files
Files changed (3) hide show
  1. app.py +5 -0
  2. requirements.txt +1 -0
  3. tool.py +48 -0
app.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from smolagents import launch_gradio_demo
2
+ from tool import SimpleTool
3
+
4
+ tool = SimpleTool()
5
+ launch_gradio_demo(tool)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ smolagents
tool.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import Tool
2
+ from typing import Any, Optional
3
+
4
+ class SimpleTool(Tool):
5
+ name = "convert_unit"
6
+ description = "Convert a value from one unit to another."
7
+ inputs = {'value': {'type': 'number', 'description': 'The numerical value to be converted.'}, 'from_unit': {'type': 'string', 'description': 'The unit of the input value (e.g., "cm", "kg").'}, 'to_unit': {'type': 'string', 'description': 'The target unit for conversion (e.g., "km", "lb").'}}
8
+ output_type = "number"
9
+
10
+ def forward(self, value: float, from_unit: str, to_unit: str) -> float:
11
+ """
12
+ Convert a value from one unit to another.
13
+
14
+ Args:
15
+ value: The numerical value to be converted.
16
+ from_unit: The unit of the input value (e.g., "cm", "kg").
17
+ to_unit: The target unit for conversion (e.g., "km", "lb").
18
+
19
+ Returns:
20
+ The converted value in the target unit.
21
+
22
+ Raises:
23
+ ValueError: If the conversion between the specified units is not supported.
24
+ """
25
+
26
+ # Define conversion rates
27
+ conversion_rates = {
28
+ # Length
29
+ 'mm': {'cm': 0.1, 'm': 0.001, 'km': 0.000001, 'in': 0.0393701, 'ft': 0.00328084},
30
+ 'cm': {'mm': 10, 'm': 0.01, 'km': 0.00001, 'in': 0.393701, 'ft': 0.0328084},
31
+ 'm': {'mm': 1000, 'cm': 100, 'km': 0.001, 'in': 39.3701, 'ft': 3.28084,},
32
+ 'km': {'mm': 1000000, 'cm': 100000, 'm': 1000, 'in': 39370.1, 'ft': 3280.84},
33
+ 'in': {'mm': 25.4, 'cm': 2.54, 'm': 0.0254, 'km': 0.0000254, 'ft': 0.0833333},
34
+
35
+ # Weight
36
+ 'mg': {'g': 0.001, 'kg': 0.000001, 'lb': 0.00000220462, 'oz': 0.000035274},
37
+ 'g': {'mg': 1000, 'kg': 0.001, 'lb': 0.00220462, 'oz': 0.035274},
38
+ 'kg': {'mg': 1000000, 'g': 1000, 'lb': 2.20462, 'oz': 35.274},
39
+ 'lb': {'mg': 453592, 'g': 453.592, 'kg': 0.453592, 'oz': 16},
40
+ 'oz': {'mg': 28349.5, 'g': 28.3495, 'kg': 0.0283495, 'lb': 0.0625},
41
+ }
42
+
43
+ # Check if the units are valid
44
+ if from_unit not in conversion_rates or to_unit not in conversion_rates[from_unit]:
45
+ raise ValueError(f"Conversion from {from_unit} to {to_unit} is not supported")
46
+
47
+ # Perform the conversion
48
+ return value * conversion_rates[from_unit][to_unit]