metehan yalçın commited on
Commit
4795af1
·
verified ·
1 Parent(s): a023776

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -1
app.py CHANGED
@@ -9,7 +9,7 @@ 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:
@@ -18,6 +18,53 @@ 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.
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ def my_custom_tool(currency_name:str, currency_value:int, target_currency:str)-> int: #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:
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def my_custom_tool(currency_name: str, currency_value: Union[int, float], target_currency: str) -> float:
23
+ """
24
+ Convert a given amount from one currency to another using current exchange rates.
25
+
26
+ Args:
27
+ currency_name: Source currency code (e.g., "TRY", "USD", "EUR")
28
+ currency_value: Amount to convert
29
+ target_currency: Target currency code (e.g., "USD", "EUR", "GBP")
30
+
31
+ Returns:
32
+ float: The converted amount in the target currency
33
+
34
+ Example:
35
+ my_custom_tool("TRY", 36, "USD") -> 1.11 (or current equivalent)
36
+ """
37
+ try:
38
+ # Normalize currency codes
39
+ from_currency = currency_name.upper().strip()
40
+ to_currency = target_currency.upper().strip()
41
+
42
+ # Use a public API to get the current exchange rate
43
+ url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}"
44
+ response = requests.get(url)
45
+ data = response.json()
46
+
47
+ # Check if the API request was successful
48
+ if response.status_code != 200:
49
+ return f"Error: API request failed with status code {response.status_code}"
50
+
51
+ # Check if the target currency is available
52
+ if to_currency not in data['rates']:
53
+ return f"Error: Target currency {to_currency} not found"
54
+
55
+ # Get the exchange rate and calculate the converted amount
56
+ exchange_rate = data['rates'][to_currency]
57
+ converted_amount = currency_value * exchange_rate
58
+
59
+ # Return the converted amount rounded to 2 decimal places
60
+ return round(converted_amount, 2)
61
+
62
+ except requests.exceptions.RequestException as e:
63
+ return f"Network error: {str(e)}"
64
+ except KeyError as e:
65
+ return f"Data error: Currency not found - {str(e)}"
66
+ except Exception as e:
67
+ return f"Unexpected error: {str(e)}"
68
  @tool
69
  def get_current_time_in_timezone(timezone: str) -> str:
70
  """A tool that fetches the current local time in a specified timezone.