Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -116,6 +116,7 @@ class XylariaChat:
|
|
116 |
response_stream = self.get_response(message)
|
117 |
|
118 |
if isinstance(response_stream, str):
|
|
|
119 |
return "", chat_history + [[message, response_stream]]
|
120 |
|
121 |
full_response = ""
|
@@ -140,10 +141,21 @@ class XylariaChat:
|
|
140 |
|
141 |
self.save_chat() # Save after each interaction
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
# Custom CSS for improved colors and styling
|
148 |
custom_css = """
|
149 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
@@ -243,27 +255,32 @@ class XylariaChat:
|
|
243 |
def forward_prompt(prompt):
|
244 |
return prompt
|
245 |
|
246 |
-
|
|
|
|
|
|
|
|
|
|
|
247 |
with gr.Row():
|
248 |
# Sidebar for displaying chat history
|
249 |
-
with gr.Column(
|
250 |
# Collapse button
|
251 |
collapse_button = gr.Button("<<", elem_id="collapse-button")
|
252 |
|
253 |
# Sidebar content (in a nested column for easier hiding)
|
254 |
-
with gr.Column(elem_id="sidebar-content"):
|
255 |
gr.Markdown("### Chat History")
|
256 |
load_button = gr.Button("Load Chat History")
|
257 |
chat_list = gr.Markdown("No chat history found.")
|
258 |
|
259 |
load_button.click(
|
260 |
-
fn=lambda:
|
261 |
inputs=None,
|
262 |
outputs=[chat_list]
|
263 |
)
|
264 |
|
265 |
# Main chat interface
|
266 |
-
with gr.Column(
|
267 |
# Input row (stays visible)
|
268 |
with gr.Row():
|
269 |
txt = gr.Textbox(
|
@@ -280,8 +297,8 @@ class XylariaChat:
|
|
280 |
with gr.Row():
|
281 |
for prompt in example_prompts:
|
282 |
gr.Button(prompt).click(
|
283 |
-
fn=
|
284 |
-
inputs=gr.
|
285 |
outputs=txt
|
286 |
)
|
287 |
|
@@ -292,60 +309,67 @@ class XylariaChat:
|
|
292 |
height=500,
|
293 |
show_copy_button=True,
|
294 |
avatar_images=("user.png", "xylaria.png"), # Replace with your image paths
|
295 |
-
bubble_full_width=False
|
296 |
-
type="messages"
|
297 |
)
|
298 |
|
299 |
# Clear history and memory buttons
|
300 |
clear = gr.Button("Clear Conversation")
|
301 |
clear_memory = gr.Button("Clear Memory")
|
302 |
|
303 |
-
# Toggle between start and chat pages
|
304 |
-
def toggle_page(
|
305 |
-
return
|
306 |
|
307 |
# Toggle sidebar visibility
|
308 |
-
def toggle_sidebar():
|
309 |
-
|
310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
collapse_button.click(
|
312 |
-
fn=
|
313 |
-
|
314 |
-
|
315 |
-
outputs=[gr.Column(elem_id="sidebar"),
|
316 |
-
gr.Column(elem_id="sidebar-content")]
|
317 |
)
|
318 |
|
319 |
# Submit prompt
|
320 |
-
btn.click(
|
321 |
fn=streaming_response,
|
322 |
inputs=[txt, chatbot],
|
323 |
outputs=[txt, chatbot]
|
324 |
-
).then(
|
325 |
-
fn=lambda: toggle_page("chat"),
|
326 |
-
inputs=gr.State("chat"),
|
327 |
-
outputs=[chat_page, start_page]
|
328 |
)
|
329 |
-
txt.submit(
|
330 |
fn=streaming_response,
|
331 |
inputs=[txt, chatbot],
|
332 |
outputs=[txt, chatbot]
|
333 |
-
)
|
334 |
-
|
335 |
-
|
336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
)
|
338 |
|
339 |
# Clear conversation
|
340 |
clear.click(
|
341 |
-
fn=lambda:
|
342 |
inputs=None,
|
343 |
outputs=[chatbot],
|
344 |
queue=False
|
345 |
).then(
|
346 |
-
fn=lambda:
|
347 |
inputs=None,
|
348 |
-
outputs=[
|
349 |
)
|
350 |
|
351 |
# Clear memory
|
@@ -355,9 +379,16 @@ class XylariaChat:
|
|
355 |
outputs=None,
|
356 |
queue=False
|
357 |
).then(
|
358 |
-
fn=lambda:
|
359 |
-
inputs=
|
360 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
)
|
362 |
|
363 |
# Load on startup
|
|
|
116 |
response_stream = self.get_response(message)
|
117 |
|
118 |
if isinstance(response_stream, str):
|
119 |
+
# Error handling: directly append error message to chat history
|
120 |
return "", chat_history + [[message, response_stream]]
|
121 |
|
122 |
full_response = ""
|
|
|
141 |
|
142 |
self.save_chat() # Save after each interaction
|
143 |
|
144 |
+
# Function to format and display chat history
|
145 |
+
def format_chat_history():
|
146 |
+
self.load_chat() # Load the chat history first
|
147 |
+
if not self.conversation_history:
|
148 |
+
return "No chat history found."
|
149 |
+
|
150 |
+
formatted_history = ""
|
151 |
+
for chat in self.conversation_history:
|
152 |
+
if chat["role"] == "user":
|
153 |
+
formatted_history += f"**You:** {chat['content']}\n\n"
|
154 |
+
elif chat["role"] == "assistant":
|
155 |
+
formatted_history += f"**Xylaria:** {chat['content']}\n\n"
|
156 |
+
|
157 |
+
return formatted_history
|
158 |
+
|
159 |
# Custom CSS for improved colors and styling
|
160 |
custom_css = """
|
161 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
|
|
255 |
def forward_prompt(prompt):
|
256 |
return prompt
|
257 |
|
258 |
+
# Create the interface
|
259 |
+
with gr.Blocks(css=custom_css) as demo:
|
260 |
+
# State variables to track visibility (for older Gradio versions)
|
261 |
+
chat_visible = gr.Variable(False) # Use Variable instead of State
|
262 |
+
sidebar_collapsed = gr.Variable(False) # Use Variable instead of State
|
263 |
+
|
264 |
with gr.Row():
|
265 |
# Sidebar for displaying chat history
|
266 |
+
with gr.Column(scale=1) as sidebar:
|
267 |
# Collapse button
|
268 |
collapse_button = gr.Button("<<", elem_id="collapse-button")
|
269 |
|
270 |
# Sidebar content (in a nested column for easier hiding)
|
271 |
+
with gr.Column(elem_id="sidebar-content") as sidebar_content:
|
272 |
gr.Markdown("### Chat History")
|
273 |
load_button = gr.Button("Load Chat History")
|
274 |
chat_list = gr.Markdown("No chat history found.")
|
275 |
|
276 |
load_button.click(
|
277 |
+
fn=lambda *args: format_chat_history(), # Use format_chat_history directly
|
278 |
inputs=None,
|
279 |
outputs=[chat_list]
|
280 |
)
|
281 |
|
282 |
# Main chat interface
|
283 |
+
with gr.Column(scale=3) as main_chat:
|
284 |
# Input row (stays visible)
|
285 |
with gr.Row():
|
286 |
txt = gr.Textbox(
|
|
|
297 |
with gr.Row():
|
298 |
for prompt in example_prompts:
|
299 |
gr.Button(prompt).click(
|
300 |
+
fn=lambda p=prompt: p, # Capture prompt in a closure
|
301 |
+
inputs=gr.Variable(prompt), # Pass prompt as Variable
|
302 |
outputs=txt
|
303 |
)
|
304 |
|
|
|
309 |
height=500,
|
310 |
show_copy_button=True,
|
311 |
avatar_images=("user.png", "xylaria.png"), # Replace with your image paths
|
312 |
+
bubble_full_width=False
|
|
|
313 |
)
|
314 |
|
315 |
# Clear history and memory buttons
|
316 |
clear = gr.Button("Clear Conversation")
|
317 |
clear_memory = gr.Button("Clear Memory")
|
318 |
|
319 |
+
# Toggle between start and chat pages (using visibility)
|
320 |
+
def toggle_page(show_chat, *args):
|
321 |
+
return not show_chat, show_chat # Invert visibility
|
322 |
|
323 |
# Toggle sidebar visibility
|
324 |
+
def toggle_sidebar(collapsed, *args):
|
325 |
+
if collapsed:
|
326 |
+
# If currently collapsed, expand
|
327 |
+
return False, "250px", "block" # Expand, normal width, display content
|
328 |
+
else:
|
329 |
+
# If currently expanded, collapse
|
330 |
+
return True, "50px", "none" # Collapse, narrow width, hide content
|
331 |
+
|
332 |
+
# Collapse button click (handle sidebar toggling)
|
333 |
collapse_button.click(
|
334 |
+
fn=toggle_sidebar,
|
335 |
+
inputs=[sidebar_collapsed],
|
336 |
+
outputs=[sidebar_collapsed, sidebar, sidebar_content]
|
|
|
|
|
337 |
)
|
338 |
|
339 |
# Submit prompt
|
340 |
+
submit_event = btn.click(
|
341 |
fn=streaming_response,
|
342 |
inputs=[txt, chatbot],
|
343 |
outputs=[txt, chatbot]
|
|
|
|
|
|
|
|
|
344 |
)
|
345 |
+
txt_submit_event = txt.submit(
|
346 |
fn=streaming_response,
|
347 |
inputs=[txt, chatbot],
|
348 |
outputs=[txt, chatbot]
|
349 |
+
)
|
350 |
+
|
351 |
+
# Toggle to chat page after sending the first message
|
352 |
+
submit_event.then(
|
353 |
+
fn=toggle_page,
|
354 |
+
inputs=[chat_visible],
|
355 |
+
outputs=[start_page, chat_page]
|
356 |
+
)
|
357 |
+
txt_submit_event.then(
|
358 |
+
fn=toggle_page,
|
359 |
+
inputs=[chat_visible],
|
360 |
+
outputs=[start_page, chat_page]
|
361 |
)
|
362 |
|
363 |
# Clear conversation
|
364 |
clear.click(
|
365 |
+
fn=lambda *args: [], # Clear chatbot by returning an empty list
|
366 |
inputs=None,
|
367 |
outputs=[chatbot],
|
368 |
queue=False
|
369 |
).then(
|
370 |
+
fn=lambda *args: (True, False), # Show start page, hide chat page
|
371 |
inputs=None,
|
372 |
+
outputs=[start_page, chat_page]
|
373 |
)
|
374 |
|
375 |
# Clear memory
|
|
|
379 |
outputs=None,
|
380 |
queue=False
|
381 |
).then(
|
382 |
+
fn=lambda *args: (True, False), # Show start page, hide chat page
|
383 |
+
inputs=None,
|
384 |
+
outputs=[start_page, chat_page]
|
385 |
+
)
|
386 |
+
|
387 |
+
# Update chat_visible state when chat_page visibility changes
|
388 |
+
chat_page.change(
|
389 |
+
fn=lambda visible, *args: visible,
|
390 |
+
inputs=[chat_page],
|
391 |
+
outputs=[chat_visible]
|
392 |
)
|
393 |
|
394 |
# Load on startup
|