dlflannery commited on
Commit
1a4edd5
·
verified ·
1 Parent(s): 7497564

Update app.py

Browse files

Added stock stuff

Files changed (1) hide show
  1. app.py +48 -1
app.py CHANGED
@@ -17,6 +17,10 @@ from PIL import Image
17
  from io import BytesIO
18
  from pydantic import BaseModel
19
  import pprint
 
 
 
 
20
 
21
  load_dotenv(override=True)
22
  key = os.getenv('OPENAI_API_KEY')
@@ -34,6 +38,8 @@ else:
34
  dp = Path('/data')
35
  dp.mkdir(exist_ok=True)
36
  dataDir = '/data/'
 
 
37
 
38
  speak_file = dataDir + "speek.wav"
39
 
@@ -56,6 +62,42 @@ class MathReasoning(BaseModel):
56
  def Client():
57
  return OpenAI(api_key = key)
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  def solve(prompt, chatType):
60
  tokens_in = 0
61
  tokens_out = 0
@@ -270,6 +312,12 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
270
  (log_cnt, wav_cnt, other_cnt, others, log_list) = list_permanent_files()
271
  response = f'{log_cnt} log files\n{wav_cnt} .wav files\n{other_cnt} Other files:\n{others}\nlogs: {str(log_list)}'
272
  return [past, response, None, gptModel, uploaded_image_file]
 
 
 
 
 
 
273
  if user_window in unames and pwd_window == pwdList[unames.index(user_window)]:
274
  chatType = 'normal'
275
  prompt = prompt.strip()
@@ -504,7 +552,6 @@ def show_help():
504
  5.4 The image should appear. This can take some time with a slow internet connection and large image.
505
  5.5 Tap the "Submit Prompt/Question" button to start the analysis. This initiates a chat dialog and
506
  you can ask follow-up questions. However, the image is not re-analyzed for follow-up dialog.
507
-
508
  Hints:
509
  1. Better chat and image results are obtained by including detailed descriptions and instructions
510
  in the prompt.
 
17
  from io import BytesIO
18
  from pydantic import BaseModel
19
  import pprint
20
+ import pandas as pd
21
+ import yfinance as yf
22
+ from datetime import datetime, timedelta
23
+
24
 
25
  load_dotenv(override=True)
26
  key = os.getenv('OPENAI_API_KEY')
 
38
  dp = Path('/data')
39
  dp.mkdir(exist_ok=True)
40
  dataDir = '/data/'
41
+ stock_data_path = dataDir + 'Stocks.txt'
42
+
43
 
44
  speak_file = dataDir + "speek.wav"
45
 
 
62
  def Client():
63
  return OpenAI(api_key = key)
64
 
65
+ def get_stock_report():
66
+ stock_data = {}
67
+ global stock_data_path
68
+ with open(stock_data_path, 'rt') as fp:
69
+ lines = fp.readlines()
70
+ for line in lines:
71
+ (name, symbol, shares) = line.rstrip().split(',')
72
+ stock_data[symbol] = {"symbol": symbol, "name": name, "shares": shares, "closing": '0'}
73
+ for symbol in stock_data.keys():
74
+ stock_data[symbol]['closing'] = f'{get_last_closing(symbol):.2f}'
75
+ total_value = 0.0
76
+ rv = ''
77
+ for item in stock_data.values():
78
+ rv += str(item) + '\n'
79
+ total_value += float(item['closing']) * float(item['shares'])
80
+ rv += (f'\nTotal value = {total_value:.2f}')
81
+ return rv
82
+
83
+ def get_last_closing(symbol, timeout=10):
84
+ today = datetime.today()+timedelta(days=1)
85
+ five_days_ago = today - timedelta(days=5)
86
+ end = today.strftime('%Y-%m-%d')
87
+ start = five_days_ago.strftime('%Y-%m-%d')
88
+ df = yf.download(symbol,
89
+ start = start,
90
+ end = end,
91
+ progress = False,
92
+ timeout=timeout,
93
+ )
94
+ # print(df)
95
+ return df.iat[-1,1]
96
+
97
+ def create_stock_data_file(txt):
98
+ with open(stock_data_path, 'wt') as fp:
99
+ fp.write(txt)
100
+
101
  def solve(prompt, chatType):
102
  tokens_in = 0
103
  tokens_out = 0
 
312
  (log_cnt, wav_cnt, other_cnt, others, log_list) = list_permanent_files()
313
  response = f'{log_cnt} log files\n{wav_cnt} .wav files\n{other_cnt} Other files:\n{others}\nlogs: {str(log_list)}'
314
  return [past, response, None, gptModel, uploaded_image_file]
315
+ if prompt.startswith('stocks'):
316
+ response = get_stock_report()
317
+ return [past, response, None, gptModel, uploaded_image_file]
318
+ if prompt.startswith('stockload'):
319
+ create_stock_data_file(prompt[9:].lstrip())
320
+ return [past, 'Stock data file created', None, gptModel, uploaded_image_file]
321
  if user_window in unames and pwd_window == pwdList[unames.index(user_window)]:
322
  chatType = 'normal'
323
  prompt = prompt.strip()
 
552
  5.4 The image should appear. This can take some time with a slow internet connection and large image.
553
  5.5 Tap the "Submit Prompt/Question" button to start the analysis. This initiates a chat dialog and
554
  you can ask follow-up questions. However, the image is not re-analyzed for follow-up dialog.
 
555
  Hints:
556
  1. Better chat and image results are obtained by including detailed descriptions and instructions
557
  in the prompt.