doss1232 commited on
Commit
4fa8f9e
·
verified ·
1 Parent(s): ffc0bda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -1
app.py CHANGED
@@ -4,9 +4,110 @@ import requests
4
  import pytz
5
  import yaml
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
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
12
 
@@ -73,7 +174,7 @@ with open("prompts.yaml", 'r') as stream:
73
 
74
  agent = CodeAgent(
75
  model=model,
76
- tools=[generate_pictures_with_watermelon_in_it], ## add your tools here (don't remove final answer)
77
  max_steps=6,
78
  verbosity_level=1,
79
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ import os
8
 
9
  from Gradio_UI import GradioUI
10
 
11
+ from stocksymbol import StockSymbol
12
+ from polygon import RESTClient
13
+ import time
14
+ import difflib
15
+ from datetime import datetime
16
+ from dateutil.relativedelta import relativedelta
17
+
18
+ # API
19
+ symbol_api_key = os.getenv("STOCK_SYMBOL")
20
+ polygon_api = os.getenv('POLYGON_API')
21
+
22
+ # Get the us market stock name
23
+ ss = StockSymbol(symbol_api_key)
24
+ symbol_list_us = ss.get_symbol_list(market="US")
25
+
26
+ # Create stock price retriever
27
+ client = RESTClient(api_key=polygon_api)
28
+
29
+ # utility function
30
+ def convert_timestamp_to_date(timestamp):
31
+ # Convert milliseconds to seconds
32
+ timestamp_seconds = timestamp / 1000.0
33
+ # Create a datetime object
34
+ date_time = datetime.fromtimestamp(timestamp_seconds)
35
+ # Format the date to a readable string
36
+ return date_time.strftime('%B %d, %Y')
37
+
38
+ def find_stock_symbol(company_name, stock_list):
39
+ """
40
+ Find the stock symbol based on the closest matching company name.
41
+
42
+ Args:
43
+ company_name (str): The name of the company to search for.
44
+ stock_list (list): A list of dictionaries containing stock information.
45
+
46
+ Returns:
47
+ str: The stock symbol of the closest matching company name or None if no match is found.
48
+ """
49
+ # Extract long names from the stock list
50
+ long_names = [stock['longName'] for stock in stock_list]
51
+
52
+ # Find the closest match
53
+ closest_match = difflib.get_close_matches(company_name, long_names, n=1)
54
+
55
+ if closest_match:
56
+ # Get the index of the closest match
57
+ match_index = long_names.index(closest_match[0])
58
+ # Return the corresponding symbol
59
+ return stock_list[match_index]['symbol']
60
+ else:
61
+ return None
62
+
63
+ def get_dates():
64
+ # Get current date
65
+ current_date = datetime.now()
66
+ # Get the date 5 months before
67
+ five_months_ago = current_date - relativedelta(months=6)
68
+ one_months_ago = current_date - relativedelta(months=1)
69
+ # Format dates as YYYY-MM-DD
70
+ one_months_ago_formatted = one_months_ago.strftime('%Y-%m-%d')
71
+ five_months_ago_formatted = five_months_ago.strftime('%Y-%m-%d')
72
+
73
+ return one_months_ago_formatted, five_months_ago_formatted
74
+ ### Finally my tool
75
+ @tool
76
+ def get_stock_price(name_of_company: str):
77
+ """
78
+ Retrive the stock price of the company for the last 5 months.
79
+
80
+ Args:
81
+ name_of_company: the name of the company
82
+
83
+ Returns:
84
+ List: a list of aggregate stock price data. Each agg contains the following
85
+ Each Agg entry contains the following fields:
86
+ open: The price at which the stock opened at the beginning of the trading period.
87
+ high: The highest price reached by the stock during the trading period.
88
+ low: The lowest price reached by the stock during the trading period.
89
+ close: The price at which the stock closed at the end of the trading period.
90
+ volume: The total number of shares traded during the period.
91
+ vwap: The Volume Weighted Average Price, which gives an average price of the stock weighted by volume traded.
92
+ timestamp: The time at which the data point was recorded, represented as a Unix timestamp (milliseconds since January 1, 1970).
93
+ transactions: The total number of transactions that occurred during the trading period.
94
+ otc: Indicates if the trade was over-the-counter (OTC), which is typically not applicable for major stocks and is None here.
95
+ """
96
+ symbol = find_stock_symbol(name_of_company, symbol_list_us)
97
+ # The dates
98
+ current_date, five_months_ago = get_dates()
99
+ ticker = "AAPL"
100
+
101
+ # List Aggregates (Bars)
102
+ aggs = []
103
+ for a in client.list_aggs(ticker=ticker, multiplier=1, timespan="month", from_=five_months_ago, to=current_date, limit=5):
104
+ aggs.append(a)
105
+
106
+ for agg in aggs:
107
+ agg.timestamp = convert_timestamp_to_date(agg.timestamp)
108
+
109
+ return aggs
110
+
111
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
112
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
113
 
 
174
 
175
  agent = CodeAgent(
176
  model=model,
177
+ tools=[final_answer, get_stock_price], ## add your tools here (don't remove final answer)
178
  max_steps=6,
179
  verbosity_level=1,
180
  grammar=None,