返回提交历史
Modified
gui/streamlit_chat_app.py
+21
-5
XFEstudio/gpt4free
added option to remove conversations
1379ec96
代码差异
1 个文件
+21
-5
@@ -38,6 +38,17 @@ def save_conversations(conversations, current_conversation):
38
38
39
39
os.replace(temp_conversations_file, conversations_file)
40
40
41
def delete_conversation(conversations, current_conversation):
42
for idx, conversation in enumerate(conversations):
43
conversations[idx] = current_conversation
44
break
45
conversations.remove(current_conversation)
46
47
temp_conversations_file = "temp_" + conversations_file
48
with open(temp_conversations_file, "wb") as f:
49
pickle.dump(conversations, f)
50
51
os.replace(temp_conversations_file, conversations_file)
41
52
42
53
def exit_handler():
43
54
print("Exiting, saving data...")
@@ -118,17 +129,22 @@ if search_query:
118
129
119
130
sidebar_header = f"Search Results ({len(conversations)})"
120
131
else:
121
conversations = enumerate(st.session_state.conversations)
132
conversations = st.session_state.conversations
122
133
sidebar_header = "Conversation History"
123
134
124
135
# Sidebar
125
136
st.sidebar.header(sidebar_header)
126
127
for idx, conversation in conversations:
128
if st.sidebar.button(f"Conversation {idx + 1}: {conversation['user_inputs'][0]}", key=f"sidebar_btn_{idx}"):
137
sidebar_col1, sidebar_col2 = st.sidebar.columns([5,1])
138
for idx, conversation in enumerate(conversations):
139
if sidebar_col1.button(f"Conversation {idx + 1}: {conversation['user_inputs'][0]}", key=f"sidebar_btn_{idx}"):
129
140
st.session_state['selected_conversation'] = idx
130
141
st.session_state['current_conversation'] = conversation
131
142
if sidebar_col2.button('🗑️', key=f"sidebar_btn_delete_{idx}"):
143
if st.session_state['selected_conversation'] == idx:
144
st.session_state['selected_conversation'] = None
145
st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []}
146
delete_conversation(conversations, conversation)
147
st.experimental_rerun()
132
148
if st.session_state['selected_conversation'] is not None:
133
149
conversation_to_display = conversations[st.session_state['selected_conversation']]
134
150
else: