Spaces:
Runtime error
Runtime error
feat: added own tool
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -8,30 +9,68 @@ from tools.final_answer import FinalAnswerTool
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
-
@tool
|
| 12 |
-
def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
|
|
|
|
|
|
| 24 |
Args:
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
+
from typing import Union
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
|
|
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
+
# @tool
|
| 13 |
+
# def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 14 |
+
# #Keep this format for the description / args / args description but feel free to modify the tool
|
| 15 |
+
# """A tool that does nothing yet
|
| 16 |
+
# Args:
|
| 17 |
+
# arg1: the first argument
|
| 18 |
+
# arg2: the second argument
|
| 19 |
+
# """
|
| 20 |
+
# return "What magic will you build ?"
|
| 21 |
+
|
| 22 |
+
# @tool
|
| 23 |
+
# def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
+
# """A tool that fetches the current local time in a specified timezone.
|
| 25 |
+
# Args:
|
| 26 |
+
# timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 27 |
+
# """
|
| 28 |
+
# try:
|
| 29 |
+
# # Create timezone object
|
| 30 |
+
# tz = pytz.timezone(timezone)
|
| 31 |
+
# # Get current time in that timezone
|
| 32 |
+
# local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 33 |
+
# return f"The current local time in {timezone} is: {local_time}"
|
| 34 |
+
# except Exception as e:
|
| 35 |
+
# return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
@tool
|
| 38 |
+
def unit_to_unit(unit_value: Union[int, float], input_unit_type: str, output_unit_type: str) -> str:
|
| 39 |
+
"""
|
| 40 |
+
A tool that converts the incoming unit to a given target unit\n
|
| 41 |
+
Supports length, weight and temperature conversions
|
| 42 |
Args:
|
| 43 |
+
unit_value: The numeric value to be adjusted.
|
| 44 |
+
input_unit_type: The input unit to convert from.
|
| 45 |
+
output_unit_type: The target unit to convert to.
|
| 46 |
"""
|
| 47 |
+
conversion_dict = {
|
| 48 |
+
# Length conversions
|
| 49 |
+
("km", "mi"): lambda x: x * 0.621371, # Kilometers to miles
|
| 50 |
+
("mi", "km"): lambda x: x * 1.60934, # Miles to kilometers
|
| 51 |
+
("m", "ft"): lambda x: x * 3.28084, # Meters to feet
|
| 52 |
+
("ft", "m"): lambda x: x * 0.3048, # Feet to meters
|
| 53 |
+
("cm", "in"): lambda x: x * 0.393701, # Centimeters to inches
|
| 54 |
+
("in", "cm"): lambda x: x * 2.54, # Inches to centimeters
|
| 55 |
+
|
| 56 |
+
# Weight conversions
|
| 57 |
+
("kg", "lbs"): lambda x: x * 2.20462, # Kilograms to pounds
|
| 58 |
+
("lbs", "kg"): lambda x: x * 0.453592, # Pounds to kilograms
|
| 59 |
+
("g", "oz"): lambda x: x * 0.035274, # Grams to ounces
|
| 60 |
+
("oz", "g"): lambda x: x * 28.3495, # Ounces to grams
|
| 61 |
+
|
| 62 |
+
# Temperature conversions
|
| 63 |
+
("c", "f"): lambda x: (x * 9/5) + 32, # Celsius to Fahrenheit
|
| 64 |
+
("f", "c"): lambda x: (x - 32) * 5/9, # Fahrenheit to Celsius
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
pairing = (input_unit_type, output_unit_type)
|
| 68 |
+
|
| 69 |
+
if pairing in conversion_dict:
|
| 70 |
+
scale = conversion_dict[pairing]
|
| 71 |
+
return f"{unit_value}{input_unit_type} is equal to {scale(unit_value): .2f}{output_unit_type}"
|
| 72 |
+
else:
|
| 73 |
+
return f"Conversion between {input_unit_type} and {output_unit_type} is not supported"
|
| 74 |
|
| 75 |
|
| 76 |
final_answer = FinalAnswerTool()
|