Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -584,69 +584,49 @@ def create_interface():
|
|
| 584 |
|
| 585 |
# Export functionality
|
| 586 |
with gr.Row():
|
| 587 |
-
|
|
|
|
| 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 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
|
| 611 |
-
|
| 612 |
prepare_export,
|
| 613 |
inputs=[chatbot],
|
| 614 |
-
outputs=[
|
| 615 |
)
|
| 616 |
|
| 617 |
# Examples section
|
| 618 |
-
if examples:
|
| 619 |
-
gr.Examples(examples=examples, inputs=msg)
|
| 620 |
-
|
| 621 |
-
# Chat functionality
|
| 622 |
-
def respond(message, chat_history, files_state, is_granted):
|
| 623 |
-
if not is_granted:
|
| 624 |
-
return chat_history, "", is_granted
|
| 625 |
-
|
| 626 |
-
if not message:
|
| 627 |
-
return chat_history, "", is_granted
|
| 628 |
-
|
| 629 |
-
# Format history for the generate_response function
|
| 630 |
-
formatted_history = []
|
| 631 |
-
for h in chat_history:
|
| 632 |
-
if isinstance(h, dict):
|
| 633 |
-
formatted_history.append(h)
|
| 634 |
-
|
| 635 |
-
# Get response
|
| 636 |
-
response = generate_response(message, formatted_history, files_state)
|
| 637 |
-
|
| 638 |
-
# Update chat history
|
| 639 |
-
chat_history = chat_history + [
|
| 640 |
-
{"role": "user", "content": message},
|
| 641 |
-
{"role": "assistant", "content": response}
|
| 642 |
-
]
|
| 643 |
-
|
| 644 |
-
# Update stored history for export
|
| 645 |
-
global chat_history_store
|
| 646 |
-
chat_history_store = chat_history
|
| 647 |
-
|
| 648 |
-
return chat_history, "", is_granted
|
| 649 |
-
|
| 650 |
# Wire up the interface
|
| 651 |
msg.submit(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
|
| 652 |
submit_btn.click(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
# Wire up the interface
|
| 631 |
msg.submit(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
|
| 632 |
submit_btn.click(respond, [msg, chatbot, uploaded_files, access_granted], [chatbot, msg, access_granted])
|