arad1367 commited on
Commit
d74d64b
·
verified ·
1 Parent(s): 563ecd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -59,19 +59,20 @@ def chat(message, history, response_id):
59
  return history + [[message, error_message]], None
60
 
61
  def export_chat():
62
- """Export conversation history to JSON file"""
63
  global conversation_history
64
 
65
  if not conversation_history:
66
- return "No conversation to export"
67
 
68
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
69
  filename = f"chat_export_{timestamp}.json"
70
 
71
- with open(filename, "w") as f:
72
- json.dump(conversation_history, f, indent=2)
73
 
74
- return f"Conversation exported to {filename}"
 
75
 
76
  def clear_chat(history):
77
  """Clear the chat history"""
@@ -115,6 +116,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
115
  clear_btn = gr.Button("Clear Chat")
116
  export_btn = gr.Button("Export Chat (JSON)")
117
 
 
 
118
  export_message = gr.Textbox(label="Export Status", visible=True)
119
 
120
  # Set up event handlers
@@ -152,7 +155,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
152
 
153
  export_btn.click(
154
  export_chat,
155
- outputs=[export_message]
 
 
 
 
156
  )
157
 
158
  # Footer
@@ -170,4 +177,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
170
  if __name__ == "__main__":
171
  app.launch()
172
 
173
- print("Gradio app code generated successfully. Run this code to launch your chatbot application.")
 
59
  return history + [[message, error_message]], None
60
 
61
  def export_chat():
62
+ """Export conversation history to JSON file for download"""
63
  global conversation_history
64
 
65
  if not conversation_history:
66
+ return None, "No conversation to export"
67
 
68
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
69
  filename = f"chat_export_{timestamp}.json"
70
 
71
+ # Create JSON content
72
+ json_content = json.dumps(conversation_history, indent=2)
73
 
74
+ # Return file for download and status message
75
+ return (filename, json_content), f"Conversation ready for download"
76
 
77
  def clear_chat(history):
78
  """Clear the chat history"""
 
116
  clear_btn = gr.Button("Clear Chat")
117
  export_btn = gr.Button("Export Chat (JSON)")
118
 
119
+ # File download component
120
+ chat_file = gr.File(label="Download Chat", visible=False)
121
  export_message = gr.Textbox(label="Export Status", visible=True)
122
 
123
  # Set up event handlers
 
155
 
156
  export_btn.click(
157
  export_chat,
158
+ outputs=[chat_file, export_message]
159
+ ).then(
160
+ lambda: gr.update(visible=True),
161
+ None,
162
+ [chat_file]
163
  )
164
 
165
  # Footer
 
177
  if __name__ == "__main__":
178
  app.launch()
179
 
180
+ print("Updated Gradio app code with downloadable JSON export functionality.")