milwright commited on
Commit
e8ab6c1
·
verified ·
1 Parent(s): 2c7d094

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -18
app.py CHANGED
@@ -584,33 +584,46 @@ def create_interface():
584
 
585
  # Export functionality
586
  with gr.Row():
587
- export_btn = gr.DownloadButton(
 
588
  "📥 Export Conversation",
589
  variant="secondary",
590
  size="sm"
591
  )
 
 
 
 
 
592
 
593
  # Export handler
594
- def prepare_export():
595
- if not chat_history_store:
 
596
  return None
597
 
598
- content = export_conversation_to_markdown(chat_history_store)
599
-
600
- # Create filename
601
- space_name_safe = re.sub(r'[^a-zA-Z0-9]+', '_', SPACE_NAME).lower()
602
- timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
603
- filename = f"{space_name_safe}_conversation_{timestamp}.md"
604
-
605
- # Save to temp file
606
- temp_path = Path(tempfile.gettempdir()) / filename
607
- temp_path.write_text(content, encoding='utf-8')
608
-
609
- return str(temp_path)
 
 
 
 
 
610
 
611
- export_btn.click(
612
  prepare_export,
613
- outputs=[export_btn]
 
614
  )
615
 
616
  # Examples section
@@ -649,7 +662,13 @@ def create_interface():
649
  # Wire up the interface
650
  msg.submit(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
651
  submit_btn.click(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
652
- clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
 
 
 
 
 
 
653
 
654
  # File upload accordion
655
  if ENABLE_FILE_UPLOAD:
 
584
 
585
  # Export functionality
586
  with gr.Row():
587
+ # Use a regular Button for triggering export
588
+ export_trigger_btn = gr.Button(
589
  "📥 Export Conversation",
590
  variant="secondary",
591
  size="sm"
592
  )
593
+ # Hidden file component for actual download
594
+ export_file = gr.File(
595
+ visible=False,
596
+ label="Download Export"
597
+ )
598
 
599
  # Export handler
600
+ def prepare_export(chat_history):
601
+ if not chat_history:
602
+ gr.Warning("No conversation history to export.")
603
  return None
604
 
605
+ try:
606
+ content = export_conversation_to_markdown(chat_history)
607
+
608
+ # Create filename
609
+ space_name_safe = re.sub(r'[^a-zA-Z0-9]+', '_', SPACE_NAME).lower()
610
+ timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
611
+ filename = f"{space_name_safe}_conversation_{timestamp}.md"
612
+
613
+ # Save to temp file
614
+ temp_path = Path(tempfile.gettempdir()) / filename
615
+ temp_path.write_text(content, encoding='utf-8')
616
+
617
+ # Return the file component with visibility and value
618
+ return gr.File(visible=True, value=str(temp_path))
619
+ except Exception as e:
620
+ gr.Error(f"Failed to export conversation: {str(e)}")
621
+ return None
622
 
623
+ export_trigger_btn.click(
624
  prepare_export,
625
+ inputs=[chatbot],
626
+ outputs=[export_file]
627
  )
628
 
629
  # Examples section
 
662
  # Wire up the interface
663
  msg.submit(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
664
  submit_btn.click(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
665
+
666
+ def clear_chat():
667
+ global chat_history_store
668
+ chat_history_store = []
669
+ return [], ""
670
+
671
+ clear_btn.click(clear_chat, outputs=[chatbot, msg])
672
 
673
  # File upload accordion
674
  if ENABLE_FILE_UPLOAD: