Spaces:
Running
Running
Update app.py
Browse filesAdded plot to stock news. Fixed: quotes were highs rather than closes
app.py
CHANGED
|
@@ -97,28 +97,36 @@ def stock_list():
|
|
| 97 |
return rv
|
| 98 |
|
| 99 |
def get_stock_news(search_symbol):
|
| 100 |
-
|
| 101 |
fuzzy = True
|
|
|
|
|
|
|
| 102 |
with open(stock_data_path, 'rt') as fp:
|
| 103 |
lines = fp.readlines()
|
| 104 |
for line in lines:
|
| 105 |
(name, symbol, shares) = line.rstrip().split(',')
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
try:
|
| 112 |
news = yf.Search(search_term, news_count=5, enable_fuzzy_query=fuzzy).news
|
| 113 |
except:
|
| 114 |
-
return f'No results for search term {search_term}, check spelling'
|
| 115 |
rv = ''
|
| 116 |
for item in news:
|
| 117 |
rv += f'Title: {item["title"]}\n'
|
| 118 |
rv += f'Publisher: {item["publisher"]}\n'
|
| 119 |
rv += f'Date published: {date_from_utime(item["providerPublishTime"])}\n'
|
| 120 |
rv += f'Link: [URL]({item["link"]})\n\n'
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
def stock_history_df(num_weeks):
|
| 124 |
values = []
|
|
@@ -139,6 +147,60 @@ def stock_history_df(num_weeks):
|
|
| 139 |
}
|
| 140 |
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def get_stock_report(verbose = True, offset = 0):
|
| 143 |
try:
|
| 144 |
stock_data = {}
|
|
@@ -197,7 +259,7 @@ def get_last_closing(symbol, offset=0, timeout=10):
|
|
| 197 |
for row in data_top.index:
|
| 198 |
closing_date = row.strftime('%Y-%m-%d')
|
| 199 |
# print(closing_date)
|
| 200 |
-
return (df.iat[-1,
|
| 201 |
except:
|
| 202 |
return (0.0, "0000-00-00")
|
| 203 |
|
|
@@ -442,8 +504,14 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 442 |
elif num >= 3:
|
| 443 |
if args[1] == 'news':
|
| 444 |
symbol = ' '.join(args[2:])
|
| 445 |
-
response = get_stock_news(symbol)
|
| 446 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
# elif arg[1] == 'history':
|
| 448 |
# symbol = arg[2]
|
| 449 |
# response = 'ok' # get_
|
|
@@ -898,8 +966,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 898 |
# value="gpt-3.5-turbo", label="GPT Model", interactive=True)
|
| 899 |
button_do_image = gr.Button(value='Make Image')
|
| 900 |
button_get_image = gr.Button(value='Upload Image to Analyze')
|
| 901 |
-
submit_button = gr.Button(value="Submit Prompt/Question")
|
| 902 |
speak_output = gr.Button(value="Speak Dialog", visible=True)
|
|
|
|
| 903 |
prompt_window = gr.Textbox(label = "Prompt or Question")
|
| 904 |
gr.Markdown('### **Dialog:**')
|
| 905 |
output_window = gr.Markdown(container=True)
|
|
|
|
| 97 |
return rv
|
| 98 |
|
| 99 |
def get_stock_news(search_symbol):
|
| 100 |
+
stock_list = {}
|
| 101 |
fuzzy = True
|
| 102 |
+
have_symbol = False
|
| 103 |
+
search_symbol = search_symbol.strip().upper()
|
| 104 |
with open(stock_data_path, 'rt') as fp:
|
| 105 |
lines = fp.readlines()
|
| 106 |
for line in lines:
|
| 107 |
(name, symbol, shares) = line.rstrip().split(',')
|
| 108 |
+
stock_list[symbol.strip()] = name.strip()
|
| 109 |
+
search_term = search_symbol
|
| 110 |
+
if search_symbol in stock_list.keys():
|
| 111 |
+
have_symbol = True
|
| 112 |
+
search_term = stock_list[search_symbol]
|
| 113 |
try:
|
| 114 |
news = yf.Search(search_term, news_count=5, enable_fuzzy_query=fuzzy).news
|
| 115 |
except:
|
| 116 |
+
return (f'No results for search term {search_term}, check spelling', None)
|
| 117 |
rv = ''
|
| 118 |
for item in news:
|
| 119 |
rv += f'Title: {item["title"]}\n'
|
| 120 |
rv += f'Publisher: {item["publisher"]}\n'
|
| 121 |
rv += f'Date published: {date_from_utime(item["providerPublishTime"])}\n'
|
| 122 |
rv += f'Link: [URL]({item["link"]})\n\n'
|
| 123 |
+
|
| 124 |
+
if have_symbol:
|
| 125 |
+
(plot_df, ymax) = stock_week_df(search_symbol)
|
| 126 |
+
else:
|
| 127 |
+
(plot_df, ymax) = (pd.DataFrame(), 0.0)
|
| 128 |
+
|
| 129 |
+
return (rv, plot_df, ymax)
|
| 130 |
|
| 131 |
def stock_history_df(num_weeks):
|
| 132 |
values = []
|
|
|
|
| 147 |
}
|
| 148 |
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
| 149 |
|
| 150 |
+
def stock_week_df(symbol):
|
| 151 |
+
try:
|
| 152 |
+
dates = []
|
| 153 |
+
values = []
|
| 154 |
+
ymax = 0
|
| 155 |
+
etime = etz_now()
|
| 156 |
+
if etime.hour >= 16:
|
| 157 |
+
etime = etime + timedelta(days=1)
|
| 158 |
+
week_ago = etime - timedelta(days=8)
|
| 159 |
+
end = etime.strftime('%Y-%m-%d')
|
| 160 |
+
start = week_ago.strftime('%Y-%m-%d')
|
| 161 |
+
df = yf.download(symbol.upper(),
|
| 162 |
+
start = start,
|
| 163 |
+
end = end,
|
| 164 |
+
progress = False,
|
| 165 |
+
)
|
| 166 |
+
vals2d = df.values.tolist()
|
| 167 |
+
valsTxt = []
|
| 168 |
+
numDays = len(vals2d)
|
| 169 |
+
for i in range(numDays):
|
| 170 |
+
valsTxt.append(vals2d[i][0])
|
| 171 |
+
for val in valsTxt:
|
| 172 |
+
v = float(val)
|
| 173 |
+
values.append(v)
|
| 174 |
+
if v > ymax:
|
| 175 |
+
ymax = v
|
| 176 |
+
for row in df.index:
|
| 177 |
+
dates.append(row.strftime('%Y-%m-%d'))
|
| 178 |
+
data = {
|
| 179 |
+
"date": dates,
|
| 180 |
+
"value" : values
|
| 181 |
+
}
|
| 182 |
+
return (pd.DataFrame(data), ymax)
|
| 183 |
+
except:
|
| 184 |
+
return (pd.DataFrame(), ymax)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
for offset in range(num_weeks+1):
|
| 188 |
+
(value, date) = get_stock_report(False, offset)
|
| 189 |
+
date = date[5:]
|
| 190 |
+
values.append(value)
|
| 191 |
+
dates.append(date)
|
| 192 |
+
if float(value) > xmax:
|
| 193 |
+
xmax = float(value)
|
| 194 |
+
values.reverse()
|
| 195 |
+
dates.reverse()
|
| 196 |
+
data = {
|
| 197 |
+
"date": dates,
|
| 198 |
+
"value" : values
|
| 199 |
+
}
|
| 200 |
+
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
def get_stock_report(verbose = True, offset = 0):
|
| 205 |
try:
|
| 206 |
stock_data = {}
|
|
|
|
| 259 |
for row in data_top.index:
|
| 260 |
closing_date = row.strftime('%Y-%m-%d')
|
| 261 |
# print(closing_date)
|
| 262 |
+
return (df.iat[-1,0], closing_date)
|
| 263 |
except:
|
| 264 |
return (0.0, "0000-00-00")
|
| 265 |
|
|
|
|
| 504 |
elif num >= 3:
|
| 505 |
if args[1] == 'news':
|
| 506 |
symbol = ' '.join(args[2:])
|
| 507 |
+
(response, plot_df, ymax) = get_stock_news(symbol)
|
| 508 |
+
ymax *= 1.1
|
| 509 |
+
if plot_df.empty:
|
| 510 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
| 511 |
+
else:
|
| 512 |
+
return [past, md(response), None, gptModel, uploaded_image_file,
|
| 513 |
+
gr.LinePlot(plot_df, x="date", y="value", visible=True,
|
| 514 |
+
y_lim=[0, ymax],label=f"{symbol} Recent Prices")]
|
| 515 |
# elif arg[1] == 'history':
|
| 516 |
# symbol = arg[2]
|
| 517 |
# response = 'ok' # get_
|
|
|
|
| 966 |
# value="gpt-3.5-turbo", label="GPT Model", interactive=True)
|
| 967 |
button_do_image = gr.Button(value='Make Image')
|
| 968 |
button_get_image = gr.Button(value='Upload Image to Analyze')
|
|
|
|
| 969 |
speak_output = gr.Button(value="Speak Dialog", visible=True)
|
| 970 |
+
submit_button = gr.Button(value="Submit Prompt/Question")
|
| 971 |
prompt_window = gr.Textbox(label = "Prompt or Question")
|
| 972 |
gr.Markdown('### **Dialog:**')
|
| 973 |
output_window = gr.Markdown(container=True)
|