Spaces:
Running
Running
Update app.py
Browse filesadded file uploads pdf and txt
app.py
CHANGED
|
@@ -723,7 +723,7 @@ def new_conversation(user):
|
|
| 723 |
clean_up(user) # .wav files
|
| 724 |
flist = []
|
| 725 |
for ext in ['png','docx','xlsx','pdf','pptx', 'csv']:
|
| 726 |
-
flist.extend(glob(f'{dataDir}{user}.{ext}'))
|
| 727 |
flist.extend(glob(f'{dataDir}{user}_image.b64'))
|
| 728 |
for fpath in flist:
|
| 729 |
if os.path.exists(fpath):
|
|
@@ -735,7 +735,9 @@ def new_conversation(user):
|
|
| 735 |
return [None, [], gr.Markdown(value='', label='Dialog', container=True),
|
| 736 |
gr.Image(visible=False, value=None), gr.Image(visible=False, value=None), '',
|
| 737 |
gr.LinePlot(visible=False), gr.Dropdown(value='pd', visible=False),
|
| 738 |
-
gr.Dropdown(choices=mode_list, value=mode_list[0]),
|
|
|
|
|
|
|
| 739 |
|
| 740 |
def updatePassword(txt):
|
| 741 |
password = txt.lower().strip()
|
|
@@ -763,7 +765,7 @@ def updatePassword(txt):
|
|
| 763 |
# ref = len(txt[ref:loc]) + len(frag)
|
| 764 |
# return txt
|
| 765 |
|
| 766 |
-
def get_response(inputs, previous_response_id, container_id, image_file):
|
| 767 |
instructions = '''
|
| 768 |
You are a helpful assistant who knows how to browse the web for info and to write and run python
|
| 769 |
code.
|
|
@@ -775,6 +777,25 @@ If a powerpoint slide is created, return it as an image but do not offer a downl
|
|
| 775 |
If the user asks you to output a file, You must include the file you generate in the annotation
|
| 776 |
of the output text.
|
| 777 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 778 |
if image_file != '':
|
| 779 |
with open(image_file, 'rt') as fp:
|
| 780 |
b64data = fp.read()
|
|
@@ -823,7 +844,7 @@ of the output text.
|
|
| 823 |
return response
|
| 824 |
|
| 825 |
def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_image_file='',
|
| 826 |
-
plot=None, news_interval = 'pd', mode = 'Chat'):
|
| 827 |
image_out = gr.Image(visible=False, value=None)
|
| 828 |
file_out = gr.DownloadButton(value=None)
|
| 829 |
image_gen_model = 'gpt-4o-2024-08-06'
|
|
@@ -838,10 +859,10 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 838 |
isBoss = True
|
| 839 |
if prompt == 'stats':
|
| 840 |
response = genUsageStats()
|
| 841 |
-
return [past, str(response), None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 842 |
if prompt == 'reset':
|
| 843 |
response = genUsageStats(True)
|
| 844 |
-
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 845 |
if prompt.startswith('gpt4'):
|
| 846 |
gptModel = 'gpt-4o-2024-08-06'
|
| 847 |
prompt = prompt[5:]
|
|
@@ -852,31 +873,31 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 852 |
user = prompt[6:]
|
| 853 |
response = f'cleaned all .wav and .b64 files for {user}'
|
| 854 |
final_clean_up(user, True)
|
| 855 |
-
return [past, response, None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 856 |
if prompt.startswith('files'):
|
| 857 |
(log_cnt, wav_cnt, other_cnt, others, log_list) = list_permanent_files()
|
| 858 |
response = f'{log_cnt} log files\n{wav_cnt} .wav files\n{other_cnt} Other files:\n{others}\nlogs: {str(log_list)}'
|
| 859 |
-
return [past, response, None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 860 |
if prompt.startswith('stock'):
|
| 861 |
args = prompt.split(' ')
|
| 862 |
num = len(args)
|
| 863 |
if num == 1:
|
| 864 |
response = stock_list()
|
| 865 |
-
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 866 |
elif num == 2:
|
| 867 |
if args[1] == 'alerts':
|
| 868 |
response = get_alerts()
|
| 869 |
-
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 870 |
else:
|
| 871 |
response = get_stock_report()
|
| 872 |
if args[1] == 'value':
|
| 873 |
-
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 874 |
elif args[1] == 'history':
|
| 875 |
(plot_df, ymax) = get_total_daily_closing_sequence(40) #stock_history_df(12)
|
| 876 |
# ymax = float(ymax)
|
| 877 |
return [past, md(response), None, gptModel, uploaded_image_file, # plot]
|
| 878 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 879 |
-
y_lim=[400000, 800000], label="Portfolio Value History"), image_out, file_out]
|
| 880 |
elif num >= 3:
|
| 881 |
if args[1] == 'news':
|
| 882 |
symbol = ' '.join(args[2:])
|
|
@@ -884,15 +905,15 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 884 |
ymax *= 1.1
|
| 885 |
mdtxt = md(f'News for {symbol}:\nTrends: Month = {dm:.1f}%, Week = {dw:.1f}%, Day = {dd:.1f}%\n\n' + response)
|
| 886 |
if plot_df.empty:
|
| 887 |
-
return [past, mdtxt, None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 888 |
else:
|
| 889 |
return [past, mdtxt, None, gptModel, uploaded_image_file,
|
| 890 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 891 |
y_lim=[0, ymax],label=f"{symbol.upper()} Recent Prices",
|
| 892 |
-
color_map={''}), image_out, file_out]
|
| 893 |
if prompt.startswith('stockload'):
|
| 894 |
create_stock_data_file(prompt[9:].lstrip())
|
| 895 |
-
return [past, 'Stock data file created', None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 896 |
if user_window in unames and pwd_window == pwdList[unames.index(user_window)]:
|
| 897 |
chatType = 'normal'
|
| 898 |
deepseek = False
|
|
@@ -914,19 +935,28 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 914 |
need_turn = False
|
| 915 |
if len(past) == 0:
|
| 916 |
container_id = create_openai_container('My Container')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 917 |
inputs.append(
|
| 918 |
-
{"role": "user", "content": f"{prompt}"}
|
| 919 |
)
|
| 920 |
else:
|
| 921 |
(prev_id, container_id) = past.pop()
|
| 922 |
for item in past:
|
| 923 |
response += item
|
| 924 |
try:
|
| 925 |
-
result = get_response(inputs, prev_id, container_id,
|
|
|
|
| 926 |
uploaded_image_file = ''
|
|
|
|
| 927 |
except:
|
| 928 |
return [[], "Sorry, there was an error getting the AI response",
|
| 929 |
-
prompt, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 930 |
|
| 931 |
image_done = False
|
| 932 |
ann_files = [] # (container_id, file_id, filename)
|
|
@@ -1046,7 +1076,7 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 1046 |
f.write(f'{user_window}:{tokens_in}/{tokens_out}-4omini\n')
|
| 1047 |
if isBoss:
|
| 1048 |
response += md(f"\n\ngpt-5-mini: tokens in/out = {tokens_in}/{tokens_out}\n")
|
| 1049 |
-
return [past, response , None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 1050 |
if mode == 'Search':
|
| 1051 |
loc = prompt.find('q:')
|
| 1052 |
if loc > -1:
|
|
@@ -1136,7 +1166,7 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 1136 |
uploaded_image_file= ''
|
| 1137 |
reporting_model = image_gen_model
|
| 1138 |
if not msg == 'ok':
|
| 1139 |
-
return [past, msg, None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 1140 |
if not chatType in special_chat_types:
|
| 1141 |
reply = completion.choices[0].message.content
|
| 1142 |
# if 'groq' in reporting_model:
|
|
@@ -1179,9 +1209,9 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 1179 |
sleep(3)
|
| 1180 |
if not accessOk:
|
| 1181 |
response += f"\nDATA LOG FAILED, path = {dataFile}"
|
| 1182 |
-
return [past, response , None, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 1183 |
else:
|
| 1184 |
-
return [[], "User name and/or password are incorrect", prompt, gptModel, uploaded_image_file, plot, image_out, file_out]
|
| 1185 |
|
| 1186 |
def new_func(user):
|
| 1187 |
dataFile = dataDir + user + '_log.txt'
|
|
@@ -1384,6 +1414,7 @@ def show_help():
|
|
| 1384 |
can edit what's to be spoken. Except: In a chat conversation, spoken dialog will only include
|
| 1385 |
the latest prompt/response ("YOU:/GPT:") sequence.'''
|
| 1386 |
return str(txt).replace('```', ' ').replace(' ', ' ').replace(' ', ' ').replace(' ', ' ').replace('\n','<br>')
|
|
|
|
| 1387 |
def upload_image(prompt, user, password, mode):
|
| 1388 |
if not (user in unames and password == pwdList[unames.index(user)]):
|
| 1389 |
return [gr.Image(visible=False, interactive=True), "Incorrect user name and/or password"]
|
|
@@ -1442,6 +1473,24 @@ def mode_change(mode):
|
|
| 1442 |
else:
|
| 1443 |
return gr.Dropdown(visible=True, value='pd')
|
| 1444 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1445 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 1446 |
history = gr.State([])
|
| 1447 |
password = gr.State("")
|
|
@@ -1450,6 +1499,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 1450 |
q = gr.State([])
|
| 1451 |
qsave = gr.State([])
|
| 1452 |
uploaded_image_file = gr.State('')
|
|
|
|
| 1453 |
|
| 1454 |
def clean_up(user):
|
| 1455 |
flist = glob(dataDir + f'{user}_speech*.wav')
|
|
@@ -1575,6 +1625,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 1575 |
# gpt_chooser=gr.Radio(choices=[("GPT-3.5","gpt-3.5-turbo"),("GPT-4o","gpt-4o-mini")],
|
| 1576 |
# value="gpt-3.5-turbo", label="GPT Model", interactive=True)
|
| 1577 |
button_do_image = gr.Button(value='Make Image')
|
|
|
|
| 1578 |
button_get_image = gr.Button(value='Upload Image to Analyze')
|
| 1579 |
speak_output = gr.Button(value="Speak Dialog", visible=True)
|
| 1580 |
submit_button = gr.Button(value="Submit Prompt/Question")
|
|
@@ -1592,16 +1643,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 1592 |
image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze', type='filepath')
|
| 1593 |
with gr.Column():
|
| 1594 |
image_window = gr.Image(visible=False, label='Generated Image')
|
|
|
|
|
|
|
| 1595 |
with gr.Row():
|
| 1596 |
plot = gr.LinePlot(test_plot_df(), x="month", y="value", visible=False, label="Portfolio Value History")
|
| 1597 |
submit_button.click(chat,
|
| 1598 |
inputs=[prompt_window, user_window, password, history, output_window, model,
|
| 1599 |
-
uploaded_image_file, plot, news_period, mode],
|
| 1600 |
outputs=[history, output_window, prompt_window, model, uploaded_image_file, plot,
|
| 1601 |
-
image_window, file_download])
|
| 1602 |
clear_button.click(fn=new_conversation, inputs=user_window,
|
| 1603 |
outputs=[prompt_window, history, output_window, image_window, image_window2,
|
| 1604 |
-
|
|
|
|
| 1605 |
audio_widget.stop_recording(fn=transcribe, inputs=[user_window, password, audio_widget],
|
| 1606 |
outputs=[prompt_window])
|
| 1607 |
audio_widget.pause_recording(fn=pause_message, outputs=[prompt_window])
|
|
@@ -1618,6 +1672,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 1618 |
image_window2.upload(fn=load_image, inputs=[image_window2, user], outputs=[uploaded_image_file, output_window])
|
| 1619 |
mode.change(fn=mode_change, inputs=mode,outputs=news_period)
|
| 1620 |
pwd_window.blur(updatePassword, inputs = pwd_window, outputs = [password, pwd_window, mode])
|
| 1621 |
-
|
|
|
|
|
|
|
| 1622 |
# demo.unload(final_clean_up(user))
|
| 1623 |
demo.launch(share=True, allowed_paths=[dataDir], ssr_mode=False)
|
|
|
|
| 723 |
clean_up(user) # .wav files
|
| 724 |
flist = []
|
| 725 |
for ext in ['png','docx','xlsx','pdf','pptx', 'csv']:
|
| 726 |
+
flist.extend(glob(f'{dataDir}{user}*.{ext}'))
|
| 727 |
flist.extend(glob(f'{dataDir}{user}_image.b64'))
|
| 728 |
for fpath in flist:
|
| 729 |
if os.path.exists(fpath):
|
|
|
|
| 735 |
return [None, [], gr.Markdown(value='', label='Dialog', container=True),
|
| 736 |
gr.Image(visible=False, value=None), gr.Image(visible=False, value=None), '',
|
| 737 |
gr.LinePlot(visible=False), gr.Dropdown(value='pd', visible=False),
|
| 738 |
+
gr.Dropdown(choices=mode_list, value=mode_list[0]),
|
| 739 |
+
gr.DownloadButton(label='Download File', visible=False, value=None), None,
|
| 740 |
+
gr.File(label='Upload File', visible=False)]
|
| 741 |
|
| 742 |
def updatePassword(txt):
|
| 743 |
password = txt.lower().strip()
|
|
|
|
| 765 |
# ref = len(txt[ref:loc]) + len(frag)
|
| 766 |
# return txt
|
| 767 |
|
| 768 |
+
def get_response(inputs, previous_response_id, container_id, image_file, uploaded_file_path):
|
| 769 |
instructions = '''
|
| 770 |
You are a helpful assistant who knows how to browse the web for info and to write and run python
|
| 771 |
code.
|
|
|
|
| 777 |
If the user asks you to output a file, You must include the file you generate in the annotation
|
| 778 |
of the output text.
|
| 779 |
'''
|
| 780 |
+
if uploaded_file_path != '' and uploaded_file_path.casefold().split('.')[-1] == 'pdf':
|
| 781 |
+
pdf_b64 = ''
|
| 782 |
+
with open(uploaded_file_path, 'rb') as fp:
|
| 783 |
+
data = fp.read()
|
| 784 |
+
b64data = base64.b64encode(data)
|
| 785 |
+
pdf_b64 = b64data.decode('utf-8')
|
| 786 |
+
inputs.append(
|
| 787 |
+
{
|
| 788 |
+
"role" :"user",
|
| 789 |
+
"content": [
|
| 790 |
+
{
|
| 791 |
+
"type": "input_file",
|
| 792 |
+
"filename": f'{os.path.basename(uploaded_file_path)}',
|
| 793 |
+
"file_data": f'data:application/pdf;base64,{pdf_b64}',
|
| 794 |
+
}
|
| 795 |
+
]
|
| 796 |
+
}
|
| 797 |
+
)
|
| 798 |
+
|
| 799 |
if image_file != '':
|
| 800 |
with open(image_file, 'rt') as fp:
|
| 801 |
b64data = fp.read()
|
|
|
|
| 844 |
return response
|
| 845 |
|
| 846 |
def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_image_file='',
|
| 847 |
+
plot=None, news_interval = 'pd', mode = 'Chat', uploaded_file_path=''):
|
| 848 |
image_out = gr.Image(visible=False, value=None)
|
| 849 |
file_out = gr.DownloadButton(value=None)
|
| 850 |
image_gen_model = 'gpt-4o-2024-08-06'
|
|
|
|
| 859 |
isBoss = True
|
| 860 |
if prompt == 'stats':
|
| 861 |
response = genUsageStats()
|
| 862 |
+
return [past, str(response), None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 863 |
if prompt == 'reset':
|
| 864 |
response = genUsageStats(True)
|
| 865 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 866 |
if prompt.startswith('gpt4'):
|
| 867 |
gptModel = 'gpt-4o-2024-08-06'
|
| 868 |
prompt = prompt[5:]
|
|
|
|
| 873 |
user = prompt[6:]
|
| 874 |
response = f'cleaned all .wav and .b64 files for {user}'
|
| 875 |
final_clean_up(user, True)
|
| 876 |
+
return [past, response, None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 877 |
if prompt.startswith('files'):
|
| 878 |
(log_cnt, wav_cnt, other_cnt, others, log_list) = list_permanent_files()
|
| 879 |
response = f'{log_cnt} log files\n{wav_cnt} .wav files\n{other_cnt} Other files:\n{others}\nlogs: {str(log_list)}'
|
| 880 |
+
return [past, response, None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 881 |
if prompt.startswith('stock'):
|
| 882 |
args = prompt.split(' ')
|
| 883 |
num = len(args)
|
| 884 |
if num == 1:
|
| 885 |
response = stock_list()
|
| 886 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 887 |
elif num == 2:
|
| 888 |
if args[1] == 'alerts':
|
| 889 |
response = get_alerts()
|
| 890 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 891 |
else:
|
| 892 |
response = get_stock_report()
|
| 893 |
if args[1] == 'value':
|
| 894 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 895 |
elif args[1] == 'history':
|
| 896 |
(plot_df, ymax) = get_total_daily_closing_sequence(40) #stock_history_df(12)
|
| 897 |
# ymax = float(ymax)
|
| 898 |
return [past, md(response), None, gptModel, uploaded_image_file, # plot]
|
| 899 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 900 |
+
y_lim=[400000, 800000], label="Portfolio Value History"), image_out, file_out, uploaded_file_path]
|
| 901 |
elif num >= 3:
|
| 902 |
if args[1] == 'news':
|
| 903 |
symbol = ' '.join(args[2:])
|
|
|
|
| 905 |
ymax *= 1.1
|
| 906 |
mdtxt = md(f'News for {symbol}:\nTrends: Month = {dm:.1f}%, Week = {dw:.1f}%, Day = {dd:.1f}%\n\n' + response)
|
| 907 |
if plot_df.empty:
|
| 908 |
+
return [past, mdtxt, None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 909 |
else:
|
| 910 |
return [past, mdtxt, None, gptModel, uploaded_image_file,
|
| 911 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 912 |
y_lim=[0, ymax],label=f"{symbol.upper()} Recent Prices",
|
| 913 |
+
color_map={''}), image_out, file_out, uploaded_file_path]
|
| 914 |
if prompt.startswith('stockload'):
|
| 915 |
create_stock_data_file(prompt[9:].lstrip())
|
| 916 |
+
return [past, 'Stock data file created', None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 917 |
if user_window in unames and pwd_window == pwdList[unames.index(user_window)]:
|
| 918 |
chatType = 'normal'
|
| 919 |
deepseek = False
|
|
|
|
| 935 |
need_turn = False
|
| 936 |
if len(past) == 0:
|
| 937 |
container_id = create_openai_container('My Container')
|
| 938 |
+
file_text = ''
|
| 939 |
+
if uploaded_file_path != '':
|
| 940 |
+
upfile_ext = uploaded_file_path.casefold().split('.')[-1]
|
| 941 |
+
if upfile_ext == 'txt':
|
| 942 |
+
with open(uploaded_file_path, 'rt') as fp:
|
| 943 |
+
file_text = fp.read() + '\n'
|
| 944 |
+
uploaded_file_path = ''
|
| 945 |
inputs.append(
|
| 946 |
+
{"role": "user", "content": f"{file_text + prompt}"}
|
| 947 |
)
|
| 948 |
else:
|
| 949 |
(prev_id, container_id) = past.pop()
|
| 950 |
for item in past:
|
| 951 |
response += item
|
| 952 |
try:
|
| 953 |
+
result = get_response(inputs, prev_id, container_id,
|
| 954 |
+
uploaded_image_file, uploaded_file_path)
|
| 955 |
uploaded_image_file = ''
|
| 956 |
+
uploaded_file_path = ''
|
| 957 |
except:
|
| 958 |
return [[], "Sorry, there was an error getting the AI response",
|
| 959 |
+
prompt, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 960 |
|
| 961 |
image_done = False
|
| 962 |
ann_files = [] # (container_id, file_id, filename)
|
|
|
|
| 1076 |
f.write(f'{user_window}:{tokens_in}/{tokens_out}-4omini\n')
|
| 1077 |
if isBoss:
|
| 1078 |
response += md(f"\n\ngpt-5-mini: tokens in/out = {tokens_in}/{tokens_out}\n")
|
| 1079 |
+
return [past, response , None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 1080 |
if mode == 'Search':
|
| 1081 |
loc = prompt.find('q:')
|
| 1082 |
if loc > -1:
|
|
|
|
| 1166 |
uploaded_image_file= ''
|
| 1167 |
reporting_model = image_gen_model
|
| 1168 |
if not msg == 'ok':
|
| 1169 |
+
return [past, msg, None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 1170 |
if not chatType in special_chat_types:
|
| 1171 |
reply = completion.choices[0].message.content
|
| 1172 |
# if 'groq' in reporting_model:
|
|
|
|
| 1209 |
sleep(3)
|
| 1210 |
if not accessOk:
|
| 1211 |
response += f"\nDATA LOG FAILED, path = {dataFile}"
|
| 1212 |
+
return [past, response , None, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 1213 |
else:
|
| 1214 |
+
return [[], "User name and/or password are incorrect", prompt, gptModel, uploaded_image_file, plot, image_out, file_out, uploaded_file_path]
|
| 1215 |
|
| 1216 |
def new_func(user):
|
| 1217 |
dataFile = dataDir + user + '_log.txt'
|
|
|
|
| 1414 |
can edit what's to be spoken. Except: In a chat conversation, spoken dialog will only include
|
| 1415 |
the latest prompt/response ("YOU:/GPT:") sequence.'''
|
| 1416 |
return str(txt).replace('```', ' ').replace(' ', ' ').replace(' ', ' ').replace(' ', ' ').replace('\n','<br>')
|
| 1417 |
+
|
| 1418 |
def upload_image(prompt, user, password, mode):
|
| 1419 |
if not (user in unames and password == pwdList[unames.index(user)]):
|
| 1420 |
return [gr.Image(visible=False, interactive=True), "Incorrect user name and/or password"]
|
|
|
|
| 1473 |
else:
|
| 1474 |
return gr.Dropdown(visible=True, value='pd')
|
| 1475 |
|
| 1476 |
+
def upload_file(user, password):
|
| 1477 |
+
if not (user in unames and password == pwdList[unames.index(user)]):
|
| 1478 |
+
return [gr.File(visible=False, label='Upload File'), 'Incorrect user and/or password']
|
| 1479 |
+
return [gr.File(visible=True, label='UploadFile'), '']
|
| 1480 |
+
|
| 1481 |
+
def load_file(file, user):
|
| 1482 |
+
fname = os.path.basename(file.name)
|
| 1483 |
+
out_path = dataDir + user + '-' + fname
|
| 1484 |
+
with open(file.name, 'rb') as fp:
|
| 1485 |
+
data = fp.read()
|
| 1486 |
+
with open(out_path, 'wb') as fp2:
|
| 1487 |
+
fp2.write(data)
|
| 1488 |
+
return [out_path, f'File {fname} uploaded\n']
|
| 1489 |
+
|
| 1490 |
+
# return [value
|
| 1491 |
+
|
| 1492 |
+
# outputs=[uploaded_file_path, output_window]
|
| 1493 |
+
|
| 1494 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 1495 |
history = gr.State([])
|
| 1496 |
password = gr.State("")
|
|
|
|
| 1499 |
q = gr.State([])
|
| 1500 |
qsave = gr.State([])
|
| 1501 |
uploaded_image_file = gr.State('')
|
| 1502 |
+
uploaded_file_path = gr.State('')
|
| 1503 |
|
| 1504 |
def clean_up(user):
|
| 1505 |
flist = glob(dataDir + f'{user}_speech*.wav')
|
|
|
|
| 1625 |
# gpt_chooser=gr.Radio(choices=[("GPT-3.5","gpt-3.5-turbo"),("GPT-4o","gpt-4o-mini")],
|
| 1626 |
# value="gpt-3.5-turbo", label="GPT Model", interactive=True)
|
| 1627 |
button_do_image = gr.Button(value='Make Image')
|
| 1628 |
+
button_upload_file = gr.Button(value='Upload Input File')
|
| 1629 |
button_get_image = gr.Button(value='Upload Image to Analyze')
|
| 1630 |
speak_output = gr.Button(value="Speak Dialog", visible=True)
|
| 1631 |
submit_button = gr.Button(value="Submit Prompt/Question")
|
|
|
|
| 1643 |
image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze', type='filepath')
|
| 1644 |
with gr.Column():
|
| 1645 |
image_window = gr.Image(visible=False, label='Generated Image')
|
| 1646 |
+
with gr.Row():
|
| 1647 |
+
file_uploader = gr.File(visible=False, label='Upload File', type='filepath')
|
| 1648 |
with gr.Row():
|
| 1649 |
plot = gr.LinePlot(test_plot_df(), x="month", y="value", visible=False, label="Portfolio Value History")
|
| 1650 |
submit_button.click(chat,
|
| 1651 |
inputs=[prompt_window, user_window, password, history, output_window, model,
|
| 1652 |
+
uploaded_image_file, plot, news_period, mode, uploaded_file_path],
|
| 1653 |
outputs=[history, output_window, prompt_window, model, uploaded_image_file, plot,
|
| 1654 |
+
image_window, file_download, uploaded_file_path])
|
| 1655 |
clear_button.click(fn=new_conversation, inputs=user_window,
|
| 1656 |
outputs=[prompt_window, history, output_window, image_window, image_window2,
|
| 1657 |
+
uploaded_image_file, plot, news_period, mode, file_download, uploaded_file_path,
|
| 1658 |
+
file_uploader])
|
| 1659 |
audio_widget.stop_recording(fn=transcribe, inputs=[user_window, password, audio_widget],
|
| 1660 |
outputs=[prompt_window])
|
| 1661 |
audio_widget.pause_recording(fn=pause_message, outputs=[prompt_window])
|
|
|
|
| 1672 |
image_window2.upload(fn=load_image, inputs=[image_window2, user], outputs=[uploaded_image_file, output_window])
|
| 1673 |
mode.change(fn=mode_change, inputs=mode,outputs=news_period)
|
| 1674 |
pwd_window.blur(updatePassword, inputs = pwd_window, outputs = [password, pwd_window, mode])
|
| 1675 |
+
button_upload_file.click(fn=upload_file, inputs=[user, password],
|
| 1676 |
+
outputs=[file_uploader, output_window])
|
| 1677 |
+
file_uploader.upload(fn=load_file, inputs=[file_uploader, user], outputs=[uploaded_file_path, output_window])
|
| 1678 |
# demo.unload(final_clean_up(user))
|
| 1679 |
demo.launch(share=True, allowed_paths=[dataDir], ssr_mode=False)
|