Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -88,6 +88,22 @@ Capabilities:
|
|
| 88 |
return f"Error generating response: {str(e)}"
|
| 89 |
|
| 90 |
def create_interface(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
def streaming_response(message, chat_history):
|
| 92 |
# Clear input textbox
|
| 93 |
response_stream = self.get_response(message)
|
|
@@ -143,13 +159,14 @@ Capabilities:
|
|
| 143 |
}
|
| 144 |
"""
|
| 145 |
|
| 146 |
-
with gr.Blocks(theme='soft', css=custom_css) as demo:
|
| 147 |
# Chat interface with improved styling
|
| 148 |
with gr.Column():
|
| 149 |
chatbot = gr.Chatbot(
|
| 150 |
label="Xylaria 1.4 Senoa",
|
| 151 |
height=500,
|
| 152 |
-
show_copy_button=True
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
# Input row with improved layout
|
|
@@ -166,30 +183,42 @@ Capabilities:
|
|
| 166 |
clear = gr.Button("Clear Conversation")
|
| 167 |
clear_memory = gr.Button("Clear Memory")
|
| 168 |
|
| 169 |
-
# Submit functionality
|
| 170 |
btn.click(
|
| 171 |
fn=streaming_response,
|
| 172 |
inputs=[txt, chatbot],
|
| 173 |
outputs=[txt, chatbot]
|
|
|
|
|
|
|
|
|
|
| 174 |
)
|
| 175 |
txt.submit(
|
| 176 |
fn=streaming_response,
|
| 177 |
inputs=[txt, chatbot],
|
| 178 |
outputs=[txt, chatbot]
|
|
|
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
-
# Clear conversation history
|
| 182 |
clear.click(
|
| 183 |
fn=lambda: [],
|
| 184 |
inputs=None,
|
| 185 |
outputs=[chatbot]
|
|
|
|
|
|
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
-
# Clear persistent memory and reset conversation
|
| 189 |
clear_memory.click(
|
| 190 |
fn=lambda: [],
|
| 191 |
inputs=None,
|
| 192 |
outputs=[chatbot]
|
|
|
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
return demo
|
|
|
|
| 88 |
return f"Error generating response: {str(e)}"
|
| 89 |
|
| 90 |
def create_interface(self):
|
| 91 |
+
# Local storage JavaScript functions
|
| 92 |
+
local_storage_js = """
|
| 93 |
+
function saveToLocalStorage(chatHistory) {
|
| 94 |
+
localStorage.setItem('xylaria_chat_history', JSON.stringify(chatHistory));
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
function loadFromLocalStorage() {
|
| 98 |
+
const savedHistory = localStorage.getItem('xylaria_chat_history');
|
| 99 |
+
return savedHistory ? JSON.parse(savedHistory) : [];
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
function clearLocalStorage() {
|
| 103 |
+
localStorage.removeItem('xylaria_chat_history');
|
| 104 |
+
}
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
def streaming_response(message, chat_history):
|
| 108 |
# Clear input textbox
|
| 109 |
response_stream = self.get_response(message)
|
|
|
|
| 159 |
}
|
| 160 |
"""
|
| 161 |
|
| 162 |
+
with gr.Blocks(theme='soft', css=custom_css, js=local_storage_js) as demo:
|
| 163 |
# Chat interface with improved styling
|
| 164 |
with gr.Column():
|
| 165 |
chatbot = gr.Chatbot(
|
| 166 |
label="Xylaria 1.4 Senoa",
|
| 167 |
height=500,
|
| 168 |
+
show_copy_button=True,
|
| 169 |
+
value=lambda: gr.load("json", "loadFromLocalStorage()")
|
| 170 |
)
|
| 171 |
|
| 172 |
# Input row with improved layout
|
|
|
|
| 183 |
clear = gr.Button("Clear Conversation")
|
| 184 |
clear_memory = gr.Button("Clear Memory")
|
| 185 |
|
| 186 |
+
# Submit functionality with local storage save
|
| 187 |
btn.click(
|
| 188 |
fn=streaming_response,
|
| 189 |
inputs=[txt, chatbot],
|
| 190 |
outputs=[txt, chatbot]
|
| 191 |
+
).then(
|
| 192 |
+
fn=None, # JavaScript callback
|
| 193 |
+
_js='(chatHistory) => saveToLocalStorage(chatHistory)'
|
| 194 |
)
|
| 195 |
txt.submit(
|
| 196 |
fn=streaming_response,
|
| 197 |
inputs=[txt, chatbot],
|
| 198 |
outputs=[txt, chatbot]
|
| 199 |
+
).then(
|
| 200 |
+
fn=None, # JavaScript callback
|
| 201 |
+
_js='(chatHistory) => saveToLocalStorage(chatHistory)'
|
| 202 |
)
|
| 203 |
|
| 204 |
+
# Clear conversation history with local storage clear
|
| 205 |
clear.click(
|
| 206 |
fn=lambda: [],
|
| 207 |
inputs=None,
|
| 208 |
outputs=[chatbot]
|
| 209 |
+
).then(
|
| 210 |
+
fn=None, # JavaScript callback
|
| 211 |
+
_js='() => clearLocalStorage()'
|
| 212 |
)
|
| 213 |
|
| 214 |
+
# Clear persistent memory and reset conversation with local storage clear
|
| 215 |
clear_memory.click(
|
| 216 |
fn=lambda: [],
|
| 217 |
inputs=None,
|
| 218 |
outputs=[chatbot]
|
| 219 |
+
).then(
|
| 220 |
+
fn=None, # JavaScript callback
|
| 221 |
+
_js='() => clearLocalStorage()'
|
| 222 |
)
|
| 223 |
|
| 224 |
return demo
|