Spaces:
Running
Running
Add a currency converter tool
Browse files
app.py
CHANGED
@@ -18,6 +18,27 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
18 |
"""
|
19 |
return "What magic will you build ?"
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
18 |
"""
|
19 |
return "What magic will you build ?"
|
20 |
|
21 |
+
@tool
|
22 |
+
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
|
23 |
+
"""A tool that converts a given amount from one currency to another using real-time exchange rates.
|
24 |
+
Args:
|
25 |
+
amount: The amount of money to convert.
|
26 |
+
from_currency: The source currency code (e.g., 'USD', 'EUR').
|
27 |
+
to_currency: The target currency code (e.g., 'JPY', 'GBP').
|
28 |
+
"""
|
29 |
+
try:
|
30 |
+
response = requests.get(f"https://api.exchangerate-api.com/v4/latest/{from_currency}")
|
31 |
+
data = response.json()
|
32 |
+
rate = data['rates'].get(to_currency)
|
33 |
+
|
34 |
+
if rate:
|
35 |
+
converted_amount = amount * rate
|
36 |
+
return f"{amount} {from_currency} is approximately {converted_amount:.2f} {to_currency}."
|
37 |
+
else:
|
38 |
+
return f"Conversion rate from {from_currency} to {to_currency} not found."
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error fetching exchange rates: {str(e)}"
|
41 |
+
|
42 |
@tool
|
43 |
def get_current_time_in_timezone(timezone: str) -> str:
|
44 |
"""A tool that fetches the current local time in a specified timezone.
|