No1r97 commited on
Commit
b067069
1 Parent(s): 6150597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -69,7 +69,10 @@ def n_weeks_before(date_string, n):
69
 
70
  def get_stock_data(stock_symbol, steps):
71
 
72
- stock_data = yf.download(stock_symbol, steps[0], steps[-1])
 
 
 
73
 
74
  # print(stock_data)
75
 
@@ -101,7 +104,10 @@ def get_news(symbol, data):
101
  end_date = row['End Date'].strftime('%Y-%m-%d')
102
  # print(symbol, ': ', start_date, ' - ', end_date)
103
  time.sleep(1) # control qpm
104
- weekly_news = finnhub_client.company_news(symbol, _from=start_date, to=end_date)
 
 
 
105
  weekly_news = [
106
  {
107
  "date": datetime.fromtimestamp(n['datetime']).strftime('%Y%m%d%H%M%S'),
@@ -118,9 +124,12 @@ def get_news(symbol, data):
118
 
119
 
120
  def get_company_prompt(symbol):
121
-
122
- profile = finnhub_client.company_profile2(symbol=symbol)
123
 
 
 
 
 
 
124
  company_template = "[Company Introduction]:\n\n{name} is a leading entity in the {finnhubIndustry} sector. Incorporated and publicly traded since {ipo}, the company has established its reputation as one of the key players in the market. As of today, {name} has a market capitalization of {marketCapitalization:.2f} in {currency}, with {shareOutstanding:.2f} shares outstanding." \
125
  "\n\n{name} operates primarily in the {country}, trading under the ticker {ticker} on the {exchange}. As a dominant force in the {finnhubIndustry} space, the company continues to innovate and drive progress within the industry."
126
 
@@ -159,8 +168,11 @@ def sample_news(news, k=5):
159
 
160
  def get_current_basics(symbol, curday):
161
 
162
- basic_financials = finnhub_client.company_basic_financials(symbol, 'all')
163
-
 
 
 
164
  final_basics, basic_list, basic_dict = [], [], defaultdict(dict)
165
 
166
  for metric, value_list in basic_financials['series']['quarterly'].items():
@@ -220,7 +232,11 @@ def get_all_prompts_online(symbol, data, curday, with_basics=True):
220
 
221
  def construct_prompt(ticker, curday, n_weeks, use_basics):
222
 
223
- steps = [n_weeks_before(curday, n) for n in range(n_weeks + 1)][::-1]
 
 
 
 
224
  data = get_stock_data(ticker, steps)
225
  data = get_news(ticker, data)
226
  data['Basics'] = [json.dumps({})] * len(data)
 
69
 
70
  def get_stock_data(stock_symbol, steps):
71
 
72
+ try:
73
+ stock_data = yf.download(stock_symbol, steps[0], steps[-1])
74
+ except Exception:
75
+ raise gr.Error(f"Failed to download stock price data for symbol {stock_symbol} from yfinance!")
76
 
77
  # print(stock_data)
78
 
 
104
  end_date = row['End Date'].strftime('%Y-%m-%d')
105
  # print(symbol, ': ', start_date, ' - ', end_date)
106
  time.sleep(1) # control qpm
107
+ try:
108
+ weekly_news = finnhub_client.company_news(symbol, _from=start_date, to=end_date)
109
+ except Exception:
110
+ raise gr.Error(f"Failed to download company news for symbol {stock_symbol} from finnhub!")
111
  weekly_news = [
112
  {
113
  "date": datetime.fromtimestamp(n['datetime']).strftime('%Y%m%d%H%M%S'),
 
124
 
125
 
126
  def get_company_prompt(symbol):
 
 
127
 
128
+ try:
129
+ profile = finnhub_client.company_profile2(symbol=symbol)
130
+ except Exception:
131
+ raise gr.Error(f"Failed to download company profile for symbol {stock_symbol} from finnhub!")
132
+
133
  company_template = "[Company Introduction]:\n\n{name} is a leading entity in the {finnhubIndustry} sector. Incorporated and publicly traded since {ipo}, the company has established its reputation as one of the key players in the market. As of today, {name} has a market capitalization of {marketCapitalization:.2f} in {currency}, with {shareOutstanding:.2f} shares outstanding." \
134
  "\n\n{name} operates primarily in the {country}, trading under the ticker {ticker} on the {exchange}. As a dominant force in the {finnhubIndustry} space, the company continues to innovate and drive progress within the industry."
135
 
 
168
 
169
  def get_current_basics(symbol, curday):
170
 
171
+ try:
172
+ basic_financials = finnhub_client.company_basic_financials(symbol, 'all')
173
+ except Exception:
174
+ raise gr.Error(f"Failed to download basic financials for symbol {stock_symbol} from finnhub!")
175
+
176
  final_basics, basic_list, basic_dict = [], [], defaultdict(dict)
177
 
178
  for metric, value_list in basic_financials['series']['quarterly'].items():
 
232
 
233
  def construct_prompt(ticker, curday, n_weeks, use_basics):
234
 
235
+ try:
236
+ steps = [n_weeks_before(curday, n) for n in range(n_weeks + 1)][::-1]
237
+ except Exception:
238
+ raise gr.Error(f"Invalid date {curday}!")
239
+
240
  data = get_stock_data(ticker, steps)
241
  data = get_news(ticker, data)
242
  data['Basics'] = [json.dumps({})] * len(data)