Update app.py
Browse files
app.py
CHANGED
|
@@ -5,26 +5,6 @@ chat_histories = []
|
|
| 5 |
current_chat = []
|
| 6 |
saved_chats_choices = []
|
| 7 |
|
| 8 |
-
class App:
|
| 9 |
-
def __init__(self):
|
| 10 |
-
with gr.Blocks() as self.demo:
|
| 11 |
-
self.chatbot = gr.Chatbot()
|
| 12 |
-
self.msg = gr.Textbox()
|
| 13 |
-
self.save_button = gr.Button("Save Chat History")
|
| 14 |
-
self.saved_chats_dropdown = gr.Dropdown(choices=saved_chats_choices, label="Saved Chat Histories:")
|
| 15 |
-
|
| 16 |
-
self.msg.submit(respond, [self.msg, self.chatbot], [self.msg, self.chatbot])
|
| 17 |
-
self.save_button.click(save_chat, [self.chatbot, self], [self.chatbot, self.saved_chats_dropdown])
|
| 18 |
-
self.saved_chats_dropdown.change(load_chat, [self.chatbot])
|
| 19 |
-
|
| 20 |
-
def update_dropdown(self, choices):
|
| 21 |
-
self.saved_chats_dropdown.set_choices(choices)
|
| 22 |
-
|
| 23 |
-
def launch(self):
|
| 24 |
-
self.demo.launch()
|
| 25 |
-
|
| 26 |
-
app = App()
|
| 27 |
-
|
| 28 |
def respond(message, chat_history):
|
| 29 |
global current_chat
|
| 30 |
message = message.lower()
|
|
@@ -33,16 +13,16 @@ def respond(message, chat_history):
|
|
| 33 |
return "", chat_history
|
| 34 |
|
| 35 |
# Save the current chat history
|
| 36 |
-
def save_chat(chat_history,
|
| 37 |
global chat_histories, current_chat, saved_chats_choices
|
| 38 |
title = f"Chat History {len(chat_histories) + 1}"
|
| 39 |
chat_histories.append({'title': title, 'content': current_chat.copy()})
|
| 40 |
current_chat.clear()
|
| 41 |
saved_chats_choices.append(title)
|
| 42 |
-
|
|
|
|
| 43 |
# Manually update the dropdown choices
|
| 44 |
-
|
| 45 |
-
return chat_history, saved_chats_choices
|
| 46 |
|
| 47 |
# Load a chat history when selected
|
| 48 |
def load_chat(selection):
|
|
@@ -51,4 +31,14 @@ def load_chat(selection):
|
|
| 51 |
return chat['content']
|
| 52 |
return []
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
current_chat = []
|
| 6 |
saved_chats_choices = []
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def respond(message, chat_history):
|
| 9 |
global current_chat
|
| 10 |
message = message.lower()
|
|
|
|
| 13 |
return "", chat_history
|
| 14 |
|
| 15 |
# Save the current chat history
|
| 16 |
+
def save_chat(chat_history, saved_chats_dropdown):
|
| 17 |
global chat_histories, current_chat, saved_chats_choices
|
| 18 |
title = f"Chat History {len(chat_histories) + 1}"
|
| 19 |
chat_histories.append({'title': title, 'content': current_chat.copy()})
|
| 20 |
current_chat.clear()
|
| 21 |
saved_chats_choices.append(title)
|
| 22 |
+
print(saved_chats_choices)
|
| 23 |
+
saved_chats_dropdown = gr.Dropdown(saved_chats_choices, label="Saved Chat Histories:",interactive=True)
|
| 24 |
# Manually update the dropdown choices
|
| 25 |
+
return chat_history, saved_chats_dropdown
|
|
|
|
| 26 |
|
| 27 |
# Load a chat history when selected
|
| 28 |
def load_chat(selection):
|
|
|
|
| 31 |
return chat['content']
|
| 32 |
return []
|
| 33 |
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
chatbot = gr.Chatbot()
|
| 36 |
+
msg = gr.Textbox()
|
| 37 |
+
save_button = gr.Button("Save Chat History")
|
| 38 |
+
saved_chats_dropdown = gr.Dropdown(saved_chats_choices, label="Saved Chat Histories:",interactive=True)
|
| 39 |
+
|
| 40 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 41 |
+
save_button.click(save_chat, [chatbot, saved_chats_dropdown], [chatbot, saved_chats_dropdown])
|
| 42 |
+
saved_chats_dropdown.change(load_chat, [chatbot])
|
| 43 |
+
|
| 44 |
+
demo.launch()
|