Spaces:
Running
Running
Update app.py
Browse filesOutput window is markdown. Stock stuff improved.
app.py
CHANGED
|
@@ -70,6 +70,10 @@ def test_plot_df():
|
|
| 70 |
}
|
| 71 |
return pd.DataFrame(data)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def etz_now():
|
| 74 |
eastern = pytz.timezone('US/Eastern')
|
| 75 |
ltime = datetime.now(eastern)
|
|
@@ -81,24 +85,39 @@ def date_from_utime(utime):
|
|
| 81 |
eastern = pytz.timezone('US/Eastern')
|
| 82 |
return dt.astimezone(eastern).strftime('%Y-%m-%d')
|
| 83 |
|
| 84 |
-
def
|
| 85 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
try:
|
| 94 |
-
news = yf.Search(
|
| 95 |
except:
|
| 96 |
-
return f'No results for
|
| 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
|
|
|
|
| 102 |
return rv
|
| 103 |
|
| 104 |
def stock_history_df(num_weeks):
|
|
@@ -381,12 +400,14 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 381 |
image_gen_model = 'gpt-4o-2024-08-06'
|
| 382 |
user_window = user_window.lower().strip()
|
| 383 |
isBoss = False
|
|
|
|
|
|
|
| 384 |
plot = gr.LinePlot(visible=False)
|
| 385 |
if user_window == unames[0] and pwd_window == pwdList[0]:
|
| 386 |
isBoss = True
|
| 387 |
if prompt == 'stats':
|
| 388 |
response = genUsageStats()
|
| 389 |
-
return [past, response, None, gptModel, uploaded_image_file, plot]
|
| 390 |
if prompt == 'reset':
|
| 391 |
response = genUsageStats(True)
|
| 392 |
return [past, response, None, gptModel, uploaded_image_file, plot]
|
|
@@ -402,20 +423,46 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 402 |
(log_cnt, wav_cnt, other_cnt, others, log_list) = list_permanent_files()
|
| 403 |
response = f'{log_cnt} log files\n{wav_cnt} .wav files\n{other_cnt} Other files:\n{others}\nlogs: {str(log_list)}'
|
| 404 |
return [past, response, None, gptModel, uploaded_image_file, plot]
|
| 405 |
-
if prompt.startswith('
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
return [past, response, None, gptModel, uploaded_image_file,
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]
|
|
@@ -448,9 +495,9 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 448 |
tokens_in = completion.usage.prompt_tokens
|
| 449 |
tokens_out = completion.usage.completion_tokens
|
| 450 |
tokens = completion.usage.total_tokens
|
| 451 |
-
response += "\n\nYOU: " + prompt + "\nGPT: " + reply
|
| 452 |
if isBoss:
|
| 453 |
-
response += f"\n{reporting_model}: tokens in/out = {tokens_in}/{tokens_out}"
|
| 454 |
if tokens > 40000:
|
| 455 |
response += "\n\nTHIS DIALOG IS GETTING TOO LONG. PLEASE RESTART CONVERSATION SOON."
|
| 456 |
past.append({"role":"assistant", "content": reply})
|
|
@@ -469,7 +516,7 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 469 |
sleep(3)
|
| 470 |
if not accessOk:
|
| 471 |
response += f"\nDATA LOG FAILED, path = {dataFile}"
|
| 472 |
-
return [past, response , None, gptModel, uploaded_image_file, plot]
|
| 473 |
else:
|
| 474 |
return [[], "User name and/or password are incorrect", prompt, gptModel, uploaded_image_file, plot]
|
| 475 |
|
|
@@ -511,7 +558,7 @@ def pause_message():
|
|
| 511 |
|
| 512 |
def set_speak_button(txt):
|
| 513 |
vis = False
|
| 514 |
-
if len(txt) > 2:
|
| 515 |
vis = True
|
| 516 |
return gr.Button(visible=vis)
|
| 517 |
|
|
@@ -623,7 +670,7 @@ def make_image(prompt, user, pwd):
|
|
| 623 |
return [gr.Image(visible=True, value=fpath), msg]
|
| 624 |
|
| 625 |
def show_help():
|
| 626 |
-
|
| 627 |
1. Gemeral:
|
| 628 |
1.1 Login with user name and password (not case-sensitive)
|
| 629 |
1.2 Type prompts (questions, instructions) into "Prompt or Question" window (OR) you can speak prompts by
|
|
@@ -661,6 +708,7 @@ def show_help():
|
|
| 661 |
4. "Speak Dialog" will voice whatever is currently in the Dialog window. You can repeat it and you
|
| 662 |
can edit what's to be spoken. Except: In a chat conversation, spoken dialog will only include
|
| 663 |
the latest prompt/response ("YOU:/GPT:") sequence.'''
|
|
|
|
| 664 |
|
| 665 |
def upload_image(prompt, user, password):
|
| 666 |
if not (user in unames and password == pwdList[unames.index(user)]):
|
|
@@ -853,7 +901,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 853 |
submit_button = gr.Button(value="Submit Prompt/Question")
|
| 854 |
speak_output = gr.Button(value="Speak Dialog", visible=False)
|
| 855 |
prompt_window = gr.Textbox(label = "Prompt or Question")
|
| 856 |
-
output_window = gr.
|
| 857 |
with gr.Row():
|
| 858 |
with gr.Column():
|
| 859 |
image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze', type='filepath')
|
|
|
|
| 70 |
}
|
| 71 |
return pd.DataFrame(data)
|
| 72 |
|
| 73 |
+
def md(txt):
|
| 74 |
+
return str(txt).replace('```', ' ').replace(' ', ' ').replace(' ', ' ').replace(' ', ' ').replace('\n','<br>')
|
| 75 |
+
# return txt
|
| 76 |
+
|
| 77 |
def etz_now():
|
| 78 |
eastern = pytz.timezone('US/Eastern')
|
| 79 |
ltime = datetime.now(eastern)
|
|
|
|
| 85 |
eastern = pytz.timezone('US/Eastern')
|
| 86 |
return dt.astimezone(eastern).strftime('%Y-%m-%d')
|
| 87 |
|
| 88 |
+
def stock_list():
|
| 89 |
+
rv = ''
|
| 90 |
with open(stock_data_path, 'rt') as fp:
|
| 91 |
lines = fp.readlines()
|
| 92 |
for line in lines:
|
| 93 |
(name, symbol, shares) = line.rstrip().split(',')
|
| 94 |
name = name.strip()
|
| 95 |
symbol = symbol.strip()
|
| 96 |
+
rv += f'{symbol} {name}\n'
|
| 97 |
+
return rv
|
| 98 |
+
|
| 99 |
+
def get_stock_news(search_symbol):
|
| 100 |
+
name_list = []
|
| 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 |
+
name = name.strip()
|
| 107 |
+
name_list.append(name)
|
| 108 |
+
search_term = search_symbol.strip().upper()
|
| 109 |
+
if search_symbol in name_list:
|
| 110 |
+
fuzzy = False
|
| 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 |
return rv
|
| 122 |
|
| 123 |
def stock_history_df(num_weeks):
|
|
|
|
| 400 |
image_gen_model = 'gpt-4o-2024-08-06'
|
| 401 |
user_window = user_window.lower().strip()
|
| 402 |
isBoss = False
|
| 403 |
+
if not response:
|
| 404 |
+
response = ''
|
| 405 |
plot = gr.LinePlot(visible=False)
|
| 406 |
if user_window == unames[0] and pwd_window == pwdList[0]:
|
| 407 |
isBoss = True
|
| 408 |
if prompt == 'stats':
|
| 409 |
response = genUsageStats()
|
| 410 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
| 411 |
if prompt == 'reset':
|
| 412 |
response = genUsageStats(True)
|
| 413 |
return [past, response, None, gptModel, uploaded_image_file, plot]
|
|
|
|
| 423 |
(log_cnt, wav_cnt, other_cnt, others, log_list) = list_permanent_files()
|
| 424 |
response = f'{log_cnt} log files\n{wav_cnt} .wav files\n{other_cnt} Other files:\n{others}\nlogs: {str(log_list)}'
|
| 425 |
return [past, response, None, gptModel, uploaded_image_file, plot]
|
| 426 |
+
if prompt.startswith('stock'):
|
| 427 |
+
args = prompt.split(' ')
|
| 428 |
+
num = len(args)
|
| 429 |
+
if num == 1:
|
| 430 |
+
response = stock_list()
|
| 431 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
| 432 |
+
elif num == 2:
|
| 433 |
+
response = get_stock_report()
|
| 434 |
+
if args[1] == 'value':
|
| 435 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
| 436 |
+
elif args[1] == 'history':
|
| 437 |
+
(plot_df, ymax) = stock_history_df(12)
|
| 438 |
+
# ymax = float(ymax)
|
| 439 |
+
return [past, md(response), None, gptModel, uploaded_image_file,
|
| 440 |
+
gr.LinePlot(plot_df, x="date", y="value", visible=True,
|
| 441 |
+
y_lim=[500000, 700000], label="Portfolio Value History")]
|
| 442 |
+
elif num >= 3:
|
| 443 |
+
if args[1] == 'news':
|
| 444 |
+
symbol = ' '.join(args[2:])
|
| 445 |
+
response = get_stock_news(symbol)
|
| 446 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
| 447 |
+
# elif arg[1] == 'history':
|
| 448 |
+
# symbol = arg[2]
|
| 449 |
+
# response = 'ok' # get_
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
# if prompt.startswith('stock values'):
|
| 453 |
+
# response = get_stock_report()
|
| 454 |
+
# if 'history' in prompt:
|
| 455 |
+
# (plot_df, ymax) = stock_history_df(12)
|
| 456 |
+
# ymax = float(ymax)
|
| 457 |
+
# return [past, response, None, gptModel, uploaded_image_file,
|
| 458 |
+
# gr.LinePlot(plot_df, x="date", y="value", visible=True,
|
| 459 |
+
# y_lim=[500000, 700000], label="Portfolio Value History")]
|
| 460 |
+
# else:
|
| 461 |
+
# return [past, response, None, gptModel, uploaded_image_file, plot]
|
| 462 |
+
# if prompt.startswith('stock news'):
|
| 463 |
+
# symbol = prompt[11:]
|
| 464 |
+
# response = get_stock_news(symbol)
|
| 465 |
+
# return [past, response, None, gptModel, uploaded_image_file, plot]
|
| 466 |
if prompt.startswith('stockload'):
|
| 467 |
create_stock_data_file(prompt[9:].lstrip())
|
| 468 |
return [past, 'Stock data file created', None, gptModel, uploaded_image_file, plot]
|
|
|
|
| 495 |
tokens_in = completion.usage.prompt_tokens
|
| 496 |
tokens_out = completion.usage.completion_tokens
|
| 497 |
tokens = completion.usage.total_tokens
|
| 498 |
+
response += md("\n\nYOU: " + prompt + "\nGPT: " + reply)
|
| 499 |
if isBoss:
|
| 500 |
+
response += md(f"\n{reporting_model}: tokens in/out = {tokens_in}/{tokens_out}")
|
| 501 |
if tokens > 40000:
|
| 502 |
response += "\n\nTHIS DIALOG IS GETTING TOO LONG. PLEASE RESTART CONVERSATION SOON."
|
| 503 |
past.append({"role":"assistant", "content": reply})
|
|
|
|
| 516 |
sleep(3)
|
| 517 |
if not accessOk:
|
| 518 |
response += f"\nDATA LOG FAILED, path = {dataFile}"
|
| 519 |
+
return [past, md(response) , None, gptModel, uploaded_image_file, plot]
|
| 520 |
else:
|
| 521 |
return [[], "User name and/or password are incorrect", prompt, gptModel, uploaded_image_file, plot]
|
| 522 |
|
|
|
|
| 558 |
|
| 559 |
def set_speak_button(txt):
|
| 560 |
vis = False
|
| 561 |
+
if txt and len(txt) > 2:
|
| 562 |
vis = True
|
| 563 |
return gr.Button(visible=vis)
|
| 564 |
|
|
|
|
| 670 |
return [gr.Image(visible=True, value=fpath), msg]
|
| 671 |
|
| 672 |
def show_help():
|
| 673 |
+
txt = '''
|
| 674 |
1. Gemeral:
|
| 675 |
1.1 Login with user name and password (not case-sensitive)
|
| 676 |
1.2 Type prompts (questions, instructions) into "Prompt or Question" window (OR) you can speak prompts by
|
|
|
|
| 708 |
4. "Speak Dialog" will voice whatever is currently in the Dialog window. You can repeat it and you
|
| 709 |
can edit what's to be spoken. Except: In a chat conversation, spoken dialog will only include
|
| 710 |
the latest prompt/response ("YOU:/GPT:") sequence.'''
|
| 711 |
+
return md(txt)
|
| 712 |
|
| 713 |
def upload_image(prompt, user, password):
|
| 714 |
if not (user in unames and password == pwdList[unames.index(user)]):
|
|
|
|
| 901 |
submit_button = gr.Button(value="Submit Prompt/Question")
|
| 902 |
speak_output = gr.Button(value="Speak Dialog", visible=False)
|
| 903 |
prompt_window = gr.Textbox(label = "Prompt or Question")
|
| 904 |
+
output_window = gr.Markdown(label = "Dialog")
|
| 905 |
with gr.Row():
|
| 906 |
with gr.Column():
|
| 907 |
image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze', type='filepath')
|