dveerasa commited on
Commit
2b36273
·
verified ·
1 Parent(s): 5fd3a32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -4,6 +4,8 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
 
7
  import os
8
  from huggingface_hub import login
9
  login(token = os.getenv('HF_TOKEN'))
@@ -21,6 +23,54 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
21
  """
22
  return "What magic will you build ?"
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  @tool
25
  def get_current_time_in_timezone(timezone: str) -> str:
26
  """A tool that fetches the current local time in a specified timezone.
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from web_search import DuckDuckGoSearchTool
8
+ from forex_python.converter import CurrencyRates
9
  import os
10
  from huggingface_hub import login
11
  login(token = os.getenv('HF_TOKEN'))
 
23
  """
24
  return "What magic will you build ?"
25
 
26
+ @tool
27
+ def get_quote_in_euros(query: str, currency: str = "USD") -> str:
28
+ """
29
+ Searches DuckDuckGo for a company's stock quote or reference price
30
+ (assumed in the specified currency) and attempts to parse & convert
31
+ the price into EUR.
32
+
33
+ Args:
34
+ query: The stock ticker or company name (e.g., "AAPL" or "Tesla").
35
+ currency: The presumed currency code of the quote (default is "USD").
36
+
37
+ Returns:
38
+ A string displaying the approximate price in the original currency
39
+ and its equivalent in EUR, or an error message if not found.
40
+ """
41
+ try:
42
+ # 1. Search for the query (e.g. "AAPL stock price USD").
43
+ duck_tool = DuckDuckGoSearchTool(max_results=5)
44
+ results = duck_tool.forward(f"{query} stock price {currency}")
45
+
46
+ # 2. Try to naively parse a price from the search results.
47
+ # This example is specifically looking for something like: $123.45
48
+ # If you're not dealing with USD, adapt the pattern accordingly.
49
+ match = re.search(r"[\$€£]([\d.,]+)", results)
50
+ if not match:
51
+ return (
52
+ f"Could not find a recognizable stock/quote price for '{query}' in the results."
53
+ )
54
+
55
+ # 3. Convert the extracted price string to a float.
56
+ # Remove any commas (e.g., "1,234.56" -> 1234.56).
57
+ price_str = match.group(1).replace(",", "")
58
+ original_price = float(price_str)
59
+
60
+ # 4. Use forex_python to convert from the original currency to EUR.
61
+ c = CurrencyRates()
62
+ # get_rate("USD", "EUR") -> float (example: 0.90 means 1 USD = 0.90 EUR)
63
+ rate = c.get_rate(currency.upper(), "EUR")
64
+ price_in_euros = original_price * rate
65
+
66
+ return (
67
+ f"The approximate price of '{query}' is {currency.upper()} {original_price:.2f}, "
68
+ f"which is about EUR {price_in_euros:.2f}."
69
+ )
70
+
71
+ except Exception as e:
72
+ return f"An error occurred: {str(e)}"
73
+
74
  @tool
75
  def get_current_time_in_timezone(timezone: str) -> str:
76
  """A tool that fetches the current local time in a specified timezone.