Repository navigation
Expand file tree
/
Copy pathcommon_functions_WebUI.py
More file actions
52 lines (43 loc) · 1.99 KB
/
Copy pathcommon_functions_WebUI.py
File metadata and controls
52 lines (43 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import common_functions as cf
import streamlit as st
def get_runid():
if 'webui_runid' not in st.session_state:
st.session_state['webui_runid'] = cf.get_timeUTC()
runid = st.session_state['webui_runid']
return runid
#####
def show_history_core(hist, allow_history_deletion, last_prompt_key, last_query_key, mode:str = "DallE"):
hk = [x for x in hist.keys() if cf.isNotBlank(x)]
hk = sorted(hk, reverse=True)
hk_opt = [hist[x][0] for x in hk]
hk_q = {hist[x][0]: hist[x][1] for x in hk}
prev = st.selectbox("Prompt History (showing last GPT answer, most recent first)", options=hk_opt, index=0, key="history")
if st.button("Load Selected Prompt", key="load_history"):
if mode == "GPT":
file = hk_q[prev]
err = cf.check_file_r(file)
if cf.isNotBlank(err):
st.error(f"While checking file {file}: {err}")
st.session_state.gpt_messages = cf.read_json(file)
else:
st.session_state[last_prompt_key] = prev
st.session_state[last_query_key] = hk_q[prev]
if allow_history_deletion:
if st.button("Delete Selected Prompt", key="delete_history"):
if cf.isNotBlank(prev):
dir = os.path.dirname(hk_q[prev])
err = cf.directory_rmtree(dir)
if cf.isNotBlank(err):
st.error(f"While deleting {dir}: {err}")
else:
if os.path.exists(dir):
st.error(f"Directory {dir} still exists")
else:
st.success(f"Deleted")
else:
st.error("Please select a prompt to delete")
def show_history(hist, allow_history_deletion, last_prompt_key, last_query_key):
return show_history_core(hist, allow_history_deletion, last_prompt_key, last_query_key, "DallE")
def show_gpt_history(hist, allow_history_deletion):
return show_history_core(hist, allow_history_deletion, None, None, "GPT")