Reality123b commited on
Commit
8090856
·
verified ·
1 Parent(s): d8a976f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -3,6 +3,10 @@ import gradio as gr
3
  from huggingface_hub import InferenceClient
4
  import json
5
 
 
 
 
 
6
  class XylariaChat:
7
  def __init__(self):
8
  # Securely load HuggingFace token
@@ -155,7 +159,7 @@ class XylariaChat:
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');
@@ -257,9 +261,9 @@ class XylariaChat:
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
@@ -274,7 +278,7 @@ class XylariaChat:
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
  )
@@ -297,8 +301,8 @@ class XylariaChat:
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
 
@@ -308,7 +312,7 @@ class XylariaChat:
308
  label="Xylaria 1.4 Senoa",
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
 
@@ -316,13 +320,17 @@ class XylariaChat:
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:
@@ -332,7 +340,7 @@ class XylariaChat:
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
 
@@ -351,23 +359,23 @@ class XylariaChat:
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
  )
@@ -379,17 +387,10 @@ class XylariaChat:
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
395
  demo.load(fn=self.reset_conversation, inputs=None, outputs=None)
@@ -410,8 +411,6 @@ class XylariaChat:
410
  formatted_history += f"**Xylaria:** {chat['content']}\n\n"
411
 
412
  return formatted_history
413
-
414
- # Launch the interface
415
  def main():
416
  chat = XylariaChat()
417
  interface = chat.create_interface()
 
3
  from huggingface_hub import InferenceClient
4
  import json
5
 
6
+ # Global variables to track state (use with caution!)
7
+ chat_visible_global = False
8
+ sidebar_collapsed_global = False
9
+
10
  class XylariaChat:
11
  def __init__(self):
12
  # Securely load HuggingFace token
 
159
  formatted_history += f"**Xylaria:** {chat['content']}\n\n"
160
 
161
  return formatted_history
162
+
163
  # Custom CSS for improved colors and styling
164
  custom_css = """
165
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
 
261
 
262
  # Create the interface
263
  with gr.Blocks(css=custom_css) as demo:
264
+ # Access global variables (use with caution!)
265
+ global chat_visible_global
266
+ global sidebar_collapsed_global
267
 
268
  with gr.Row():
269
  # Sidebar for displaying chat history
 
278
  chat_list = gr.Markdown("No chat history found.")
279
 
280
  load_button.click(
281
+ fn=lambda *args: format_chat_history(),
282
  inputs=None,
283
  outputs=[chat_list]
284
  )
 
301
  with gr.Row():
302
  for prompt in example_prompts:
303
  gr.Button(prompt).click(
304
+ fn=lambda p=prompt: p,
305
+ inputs=gr.State(prompt),
306
  outputs=txt
307
  )
308
 
 
312
  label="Xylaria 1.4 Senoa",
313
  height=500,
314
  show_copy_button=True,
315
+ avatar_images=("user.png", "xylaria.png"),
316
  bubble_full_width=False
317
  )
318
 
 
320
  clear = gr.Button("Clear Conversation")
321
  clear_memory = gr.Button("Clear Memory")
322
 
323
+ # Toggle between start and chat pages (using visibility and global variable)
324
+ def toggle_page(*args):
325
+ global chat_visible_global
326
+ chat_visible_global = not chat_visible_global
327
+ return not chat_visible_global, chat_visible_global
328
+
329
+ # Toggle sidebar visibility (using global variable)
330
+ def toggle_sidebar(*args):
331
+ global sidebar_collapsed_global
332
+ sidebar_collapsed_global = not sidebar_collapsed_global
333
+ if sidebar_collapsed_global:
334
  # If currently collapsed, expand
335
  return False, "250px", "block" # Expand, normal width, display content
336
  else:
 
340
  # Collapse button click (handle sidebar toggling)
341
  collapse_button.click(
342
  fn=toggle_sidebar,
343
+ inputs=[],
344
  outputs=[sidebar_collapsed, sidebar, sidebar_content]
345
  )
346
 
 
359
  # Toggle to chat page after sending the first message
360
  submit_event.then(
361
  fn=toggle_page,
362
+ inputs=[],
363
  outputs=[start_page, chat_page]
364
  )
365
  txt_submit_event.then(
366
  fn=toggle_page,
367
+ inputs=[],
368
  outputs=[start_page, chat_page]
369
  )
370
 
371
  # Clear conversation
372
  clear.click(
373
+ fn=lambda *args: [],
374
  inputs=None,
375
  outputs=[chatbot],
376
  queue=False
377
  ).then(
378
+ fn=lambda *args: (True, False),
379
  inputs=None,
380
  outputs=[start_page, chat_page]
381
  )
 
387
  outputs=None,
388
  queue=False
389
  ).then(
390
+ fn=lambda *args: (True, False),
391
  inputs=None,
392
  outputs=[start_page, chat_page]
393
  )
 
 
 
 
 
 
 
394
 
395
  # Load on startup
396
  demo.load(fn=self.reset_conversation, inputs=None, outputs=None)
 
411
  formatted_history += f"**Xylaria:** {chat['content']}\n\n"
412
 
413
  return formatted_history
 
 
414
  def main():
415
  chat = XylariaChat()
416
  interface = chat.create_interface()