milwright commited on
Commit
b29a2df
·
1 Parent(s): a796b1b

fix export conversation functionality - use button with file component for proper downloads

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -582,36 +582,48 @@ def create_interface():
582
  submit_btn = gr.Button("Send", variant="primary")
583
  clear_btn = gr.Button("Clear")
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(chat_history):
595
  if not chat_history:
 
596
  return None
597
 
598
- content = export_conversation_to_markdown(chat_history)
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
  inputs=[chatbot],
614
- outputs=[export_btn]
615
  )
616
 
617
  # Examples section
 
582
  submit_btn = gr.Button("Send", variant="primary")
583
  clear_btn = gr.Button("Clear")
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 path for download
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