dlflannery commited on
Commit
402fdca
·
verified ·
1 Parent(s): 872872a

Update app.py

Browse files

added stock news

Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import gradio as gr
3
  import openai
4
- from numpy._core.defchararray import endswith, isdecimal
5
  from openai import OpenAI
6
  from dotenv import load_dotenv
7
  from pathlib import Path
@@ -75,6 +75,32 @@ def etz_now():
75
  ltime = datetime.now(eastern)
76
  return ltime
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  def stock_history_df(num_weeks):
79
  values = []
80
  dates = []
@@ -386,6 +412,10 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
386
  y_lim=[500000, 700000], label="Portfolio Value History")]
387
  else:
388
  return [past, response, None, gptModel, uploaded_image_file, plot]
 
 
 
 
389
  if prompt.startswith('stockload'):
390
  create_stock_data_file(prompt[9:].lstrip())
391
  return [past, 'Stock data file created', None, gptModel, uploaded_image_file, plot]
 
1
  import os
2
  import gradio as gr
3
  import openai
4
+ from numpy._core.defchararray import endswith, isdecimal, startswith
5
  from openai import OpenAI
6
  from dotenv import load_dotenv
7
  from pathlib import Path
 
75
  ltime = datetime.now(eastern)
76
  return ltime
77
 
78
+ def date_from_utime(utime):
79
+ ts = int(utime)
80
+ dt = datetime.utcfromtimestamp(ts)
81
+ eastern = pytz.timezone('US/Eastern')
82
+ return dt.astimezone(eastern).strftime('%Y-%m-%d')
83
+
84
+ def get_stock_news(search_symbol):
85
+ name_dict = {}
86
+ with open(stock_data_path, 'rt') as fp:
87
+ lines = fp.readlines()
88
+ for line in lines:
89
+ (name, symbol, shares) = line.rstrip().split(',')
90
+ name = name.strip()
91
+ symbol = symbol.strip()
92
+ name_dict[symbol] = name
93
+ try:
94
+ news = yf.Search(name_dict[search_symbol.upper()], news_count=5).news
95
+ except:
96
+ return f'No results for symbol {search_symbol}, check spelling'
97
+ rv = ''
98
+ for item in news:
99
+ rv += f'Title: {item["title"]}\n'
100
+ rv += f'Publisher: {item["publisher"]}\n'
101
+ rv += f'Date published: {date_from_utime(item["providerPublishTime"])}\n\n'
102
+ return rv
103
+
104
  def stock_history_df(num_weeks):
105
  values = []
106
  dates = []
 
412
  y_lim=[500000, 700000], label="Portfolio Value History")]
413
  else:
414
  return [past, response, None, gptModel, uploaded_image_file, plot]
415
+ if prompt.startswith('stock news'):
416
+ symbol = prompt[11:]
417
+ response = get_stock_news(symbol)
418
+ return [past, response, None, gptModel, uploaded_image_file, plot]
419
  if prompt.startswith('stockload'):
420
  create_stock_data_file(prompt[9:].lstrip())
421
  return [past, 'Stock data file created', None, gptModel, uploaded_image_file, plot]