Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -95,9 +95,8 @@ class XylariaChat:
|
|
95 |
"photosynthesis": "process used by plants to convert light to energy"
|
96 |
}
|
97 |
|
98 |
-
# Initialize chat history file
|
99 |
self.chat_history_file = "chat_history.json"
|
100 |
-
self.load_all_chats()
|
101 |
|
102 |
|
103 |
def update_internal_state(self, emotion_deltas, cognitive_load_deltas, introspection_delta, engagement_delta):
|
@@ -571,6 +570,7 @@ class XylariaChat:
|
|
571 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
572 |
"conversation": self.conversation_history
|
573 |
}
|
|
|
574 |
try:
|
575 |
with open(self.chat_history_file, "r") as f:
|
576 |
all_chats = json.load(f)
|
@@ -601,6 +601,7 @@ class XylariaChat:
|
|
601 |
return self.conversation_history
|
602 |
else:
|
603 |
raise ValueError("Invalid chat index")
|
|
|
604 |
|
605 |
def delete_chat(self, chat_index):
|
606 |
all_chats = self.load_all_chats()
|
@@ -753,7 +754,7 @@ class XylariaChat:
|
|
753 |
self.save_chat()
|
754 |
all_chats = self.load_all_chats()
|
755 |
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
756 |
-
yield "", updated_history, None, None, gr.update(choices=chat_titles)
|
757 |
|
758 |
def load_selected_chat(chat_index, evt: gr.SelectData):
|
759 |
if chat_index is not None:
|
@@ -766,10 +767,15 @@ class XylariaChat:
|
|
766 |
if chat_index is not None:
|
767 |
all_chats = self.delete_chat(evt.index)
|
768 |
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
769 |
-
return gr.update(choices=chat_titles)
|
770 |
else:
|
771 |
return gr.update()
|
772 |
|
|
|
|
|
|
|
|
|
|
|
773 |
custom_css = """
|
774 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
775 |
body, .gradio-container {
|
@@ -848,19 +854,33 @@ class XylariaChat:
|
|
848 |
height: 500px;
|
849 |
overflow-y: auto;
|
850 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
851 |
"""
|
852 |
|
853 |
with gr.Blocks(theme='soft', css=custom_css) as demo:
|
854 |
with gr.Row():
|
855 |
-
with gr.Column(scale=1):
|
|
|
856 |
all_chats = self.load_all_chats()
|
857 |
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
858 |
|
859 |
-
chat_list = gr.Radio(label="Chat History", choices=chat_titles, type="index", elem_id="chat_list")
|
860 |
|
861 |
load_button = gr.Button("Load Selected Chat")
|
862 |
delete_button = gr.Button("Delete Selected Chat")
|
863 |
-
with gr.Column(scale=4):
|
864 |
chatbot = gr.Chatbot(
|
865 |
label="Xylaria 1.5 Senoa",
|
866 |
height=500,
|
@@ -896,7 +916,21 @@ class XylariaChat:
|
|
896 |
with gr.Row():
|
897 |
clear = gr.Button("Clear Conversation")
|
898 |
clear_memory = gr.Button("Clear Memory")
|
|
|
899 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
900 |
load_button.click(fn=load_selected_chat, inputs=[chat_list], outputs=[chatbot])
|
901 |
delete_button.click(fn=delete_selected_chat, inputs=[chat_list], outputs=[chat_list])
|
902 |
|
|
|
95 |
"photosynthesis": "process used by plants to convert light to energy"
|
96 |
}
|
97 |
|
98 |
+
# Initialize chat history file in the client's local storage
|
99 |
self.chat_history_file = "chat_history.json"
|
|
|
100 |
|
101 |
|
102 |
def update_internal_state(self, emotion_deltas, cognitive_load_deltas, introspection_delta, engagement_delta):
|
|
|
570 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
571 |
"conversation": self.conversation_history
|
572 |
}
|
573 |
+
|
574 |
try:
|
575 |
with open(self.chat_history_file, "r") as f:
|
576 |
all_chats = json.load(f)
|
|
|
601 |
return self.conversation_history
|
602 |
else:
|
603 |
raise ValueError("Invalid chat index")
|
604 |
+
|
605 |
|
606 |
def delete_chat(self, chat_index):
|
607 |
all_chats = self.load_all_chats()
|
|
|
754 |
self.save_chat()
|
755 |
all_chats = self.load_all_chats()
|
756 |
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
757 |
+
yield "", updated_history, None, None, gr.update(choices=chat_titles, visible=True)
|
758 |
|
759 |
def load_selected_chat(chat_index, evt: gr.SelectData):
|
760 |
if chat_index is not None:
|
|
|
767 |
if chat_index is not None:
|
768 |
all_chats = self.delete_chat(evt.index)
|
769 |
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
770 |
+
return gr.update(choices=chat_titles, visible=True)
|
771 |
else:
|
772 |
return gr.update()
|
773 |
|
774 |
+
def toggle_sidebar():
|
775 |
+
all_chats = self.load_all_chats()
|
776 |
+
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
777 |
+
return gr.update(visible=True), gr.update(choices=chat_titles, visible=True)
|
778 |
+
|
779 |
custom_css = """
|
780 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
781 |
body, .gradio-container {
|
|
|
854 |
height: 500px;
|
855 |
overflow-y: auto;
|
856 |
}
|
857 |
+
.sidebar-open #sidebar, .sidebar-open #main-content {
|
858 |
+
flex: 0 0 20%;
|
859 |
+
transition: flex 0.3s ease;
|
860 |
+
}
|
861 |
+
#sidebar {
|
862 |
+
flex: 0 0 0%;
|
863 |
+
overflow: hidden;
|
864 |
+
transition: flex 0.3s ease;
|
865 |
+
}
|
866 |
+
#main-content {
|
867 |
+
flex: 1;
|
868 |
+
transition: flex 0.3s ease;
|
869 |
+
}
|
870 |
"""
|
871 |
|
872 |
with gr.Blocks(theme='soft', css=custom_css) as demo:
|
873 |
with gr.Row():
|
874 |
+
with gr.Column(scale=1, elem_id="sidebar"):
|
875 |
+
toggle_button = gr.Button("Toggle Sidebar")
|
876 |
all_chats = self.load_all_chats()
|
877 |
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
878 |
|
879 |
+
chat_list = gr.Radio(label="Chat History", choices=chat_titles, type="index", elem_id="chat_list", visible=False)
|
880 |
|
881 |
load_button = gr.Button("Load Selected Chat")
|
882 |
delete_button = gr.Button("Delete Selected Chat")
|
883 |
+
with gr.Column(scale=4, elem_id="main-content"):
|
884 |
chatbot = gr.Chatbot(
|
885 |
label="Xylaria 1.5 Senoa",
|
886 |
height=500,
|
|
|
916 |
with gr.Row():
|
917 |
clear = gr.Button("Clear Conversation")
|
918 |
clear_memory = gr.Button("Clear Memory")
|
919 |
+
|
920 |
|
921 |
+
toggle_button.click(
|
922 |
+
fn=toggle_sidebar,
|
923 |
+
inputs=None,
|
924 |
+
outputs=[chat_list, chat_list],
|
925 |
+
js="""
|
926 |
+
() => {
|
927 |
+
const sidebar = document.getElementById('sidebar');
|
928 |
+
const mainContent = document.getElementById('main-content');
|
929 |
+
document.body.classList.toggle('sidebar-open');
|
930 |
+
}
|
931 |
+
"""
|
932 |
+
)
|
933 |
+
|
934 |
load_button.click(fn=load_selected_chat, inputs=[chat_list], outputs=[chatbot])
|
935 |
delete_button.click(fn=delete_selected_chat, inputs=[chat_list], outputs=[chat_list])
|
936 |
|