Spaces:
Sleeping
Sleeping
add custom tools
Browse files
app.py
CHANGED
@@ -7,16 +7,6 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
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_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""A tool that does nothing yet
|
15 |
-
Args:
|
16 |
-
arg1: the first argument
|
17 |
-
arg2: the second argument
|
18 |
-
"""
|
19 |
-
return "What magic will you build ?"
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -33,6 +23,123 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
@@ -55,7 +162,10 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[
|
|
|
|
|
|
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
@tool
|
12 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
23 |
except Exception as e:
|
24 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
25 |
|
26 |
+
# my custom tools
|
27 |
+
@tool
|
28 |
+
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
|
29 |
+
"""A tool that converts an amount from one currency to another using current exchange rates.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
amount: The amount to convert.
|
33 |
+
from_currency: The source currency code (e.g., 'USD', 'EUR', 'JPY').
|
34 |
+
to_currency: The target currency code (e.g., 'USD', 'EUR', 'JPY').
|
35 |
+
"""
|
36 |
+
try:
|
37 |
+
from_currency = from_currency.upper()
|
38 |
+
to_currency = to_currency.upper()
|
39 |
+
|
40 |
+
# Using a free currency API
|
41 |
+
response = requests.get(f"https://open.er-api.com/v6/latest/{from_currency}")
|
42 |
+
if response.status_code == 200:
|
43 |
+
data = response.json()
|
44 |
+
if data["result"] == "success":
|
45 |
+
rates = data["rates"]
|
46 |
+
if to_currency in rates:
|
47 |
+
exchange_rate = rates[to_currency]
|
48 |
+
converted_amount = amount * exchange_rate
|
49 |
+
return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency} (Rate: 1 {from_currency} = {exchange_rate} {to_currency})"
|
50 |
+
else:
|
51 |
+
return f"Currency {to_currency} not found in exchange rates."
|
52 |
+
else:
|
53 |
+
return f"API error: {data.get('error-type', 'Unknown error')}"
|
54 |
+
else:
|
55 |
+
return f"Error fetching exchange rates: HTTP Status {response.status_code}"
|
56 |
+
except Exception as e:
|
57 |
+
return f"Error converting currency: {str(e)}"
|
58 |
+
|
59 |
+
@tool
|
60 |
+
def get_ip_info(ip: str = "") -> str:
|
61 |
+
"""A tool that provides information about an IP address using a free IP geolocation API.
|
62 |
+
If no IP is provided, it returns information about the user's public IP.
|
63 |
+
|
64 |
+
Args:
|
65 |
+
ip: Optional IP address (e.g., '8.8.8.8'). If empty, uses the current public IP.
|
66 |
+
"""
|
67 |
+
try:
|
68 |
+
if ip:
|
69 |
+
response = requests.get(f"https://ipapi.co/{ip}/json/")
|
70 |
+
else:
|
71 |
+
response = requests.get("https://ipapi.co/json/")
|
72 |
+
|
73 |
+
if response.status_code == 200:
|
74 |
+
data = response.json()
|
75 |
+
if "error" in data:
|
76 |
+
return f"API Error: {data['reason']}"
|
77 |
+
|
78 |
+
result = f"IP: {data.get('ip', 'N/A')}\n"
|
79 |
+
result += f"Location: {data.get('city', 'N/A')}, {data.get('region', 'N/A')}, {data.get('country_name', 'N/A')}\n"
|
80 |
+
result += f"ISP: {data.get('org', 'N/A')}\n"
|
81 |
+
result += f"Timezone: {data.get('timezone', 'N/A')}\n"
|
82 |
+
result += f"Coordinates: {data.get('latitude', 'N/A')}, {data.get('longitude', 'N/A')}"
|
83 |
+
|
84 |
+
return result
|
85 |
+
else:
|
86 |
+
return f"Error fetching IP information: HTTP Status {response.status_code}"
|
87 |
+
except Exception as e:
|
88 |
+
return f"Error getting IP information: {str(e)}"
|
89 |
+
|
90 |
+
|
91 |
+
@tool
|
92 |
+
def string_utilities(action: str, text: str, additional_param: str = "") -> str:
|
93 |
+
"""A tool that performs various operations on strings without requiring internet access.
|
94 |
+
|
95 |
+
Args:
|
96 |
+
action: The operation to perform ('count', 'reverse', 'uppercase', 'lowercase', 'find', 'replace').
|
97 |
+
text: The input text to process.
|
98 |
+
additional_param: Additional parameter needed for some operations (e.g., text to find or replace).
|
99 |
+
"""
|
100 |
+
try:
|
101 |
+
if action.lower() == "count":
|
102 |
+
char_count = len(text)
|
103 |
+
word_count = len(text.split())
|
104 |
+
line_count = len(text.splitlines()) or 1
|
105 |
+
return f"Character count: {char_count}\nWord count: {word_count}\nLine count: {line_count}"
|
106 |
+
|
107 |
+
elif action.lower() == "reverse":
|
108 |
+
return f"Reversed text: {text[::-1]}"
|
109 |
+
|
110 |
+
elif action.lower() == "uppercase":
|
111 |
+
return f"Uppercase text: {text.upper()}"
|
112 |
+
|
113 |
+
elif action.lower() == "lowercase":
|
114 |
+
return f"Lowercase text: {text.lower()}"
|
115 |
+
|
116 |
+
elif action.lower() == "find":
|
117 |
+
if not additional_param:
|
118 |
+
return "Error: 'find' action requires text to search for in the additional_param."
|
119 |
+
|
120 |
+
occurrences = text.count(additional_param)
|
121 |
+
if occurrences > 0:
|
122 |
+
positions = [str(i) for i in range(len(text)) if text.startswith(additional_param, i)]
|
123 |
+
return f"Found '{additional_param}' {occurrences} times at positions: {', '.join(positions)}"
|
124 |
+
else:
|
125 |
+
return f"'{additional_param}' not found in the text."
|
126 |
+
|
127 |
+
elif action.lower() == "replace":
|
128 |
+
if not additional_param:
|
129 |
+
return "Error: 'replace' action requires 'old_text:new_text' format in the additional_param."
|
130 |
+
|
131 |
+
try:
|
132 |
+
old_text, new_text = additional_param.split(":", 1)
|
133 |
+
result = text.replace(old_text, new_text)
|
134 |
+
return f"Text after replacing '{old_text}' with '{new_text}':\n{result}"
|
135 |
+
except ValueError:
|
136 |
+
return "Error: For 'replace' action, additional_param must be in 'old_text:new_text' format."
|
137 |
+
|
138 |
+
else:
|
139 |
+
return f"Error: Unknown action '{action}'. Valid actions are 'count', 'reverse', 'uppercase', 'lowercase', 'find', and 'replace'."
|
140 |
+
except Exception as e:
|
141 |
+
return f"Error processing string: {str(e)}"
|
142 |
+
|
143 |
|
144 |
final_answer = FinalAnswerTool()
|
145 |
|
|
|
162 |
|
163 |
agent = CodeAgent(
|
164 |
model=model,
|
165 |
+
tools=[convert_currency,
|
166 |
+
get_ip_info,
|
167 |
+
string_utilities,
|
168 |
+
final_answer],
|
169 |
max_steps=6,
|
170 |
verbosity_level=1,
|
171 |
grammar=None,
|