melk2025 commited on
Commit
f3a372e
·
verified ·
1 Parent(s): 5af7699

saving chat in file

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -271,24 +271,21 @@ def chatbot(query):
271
  #demo.launch(share=False)
272
  # Initialize chat history with a welcome message
273
 
274
- import gradio as gr
275
- import json
276
- import time
277
-
278
- # This function will save chat history to a file only when the button is pressed
279
  def save_chat_to_file(chat_history):
280
- timestamp = time.strftime("%Y%m%d-%H%M%S") # Create a timestamp
281
- filename = f"chat_history_{timestamp}.json" # Filename with timestamp
282
- with open(filename, "w", encoding="utf-8") as f:
283
- json.dump(chat_history, f, ensure_ascii=False, indent=2)
284
- print(f"Chat history saved to {filename}")
285
- return "Chat saved!" # Return a message to show the status on the UI
 
 
286
 
287
  def ask(user_message, chat_history):
288
  if not user_message:
289
  return chat_history, chat_history, ""
290
 
291
- response = chatbot(user_message) # Your chatbot function
292
  chat_history.append((user_message, response))
293
 
294
  return chat_history, chat_history, ""
@@ -301,12 +298,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
301
  chatbot_ui = gr.Chatbot(value=[initial_message])
302
  question = gr.Textbox(placeholder="Ask me anything about Moodle...", show_label=False)
303
  clear_button = gr.Button("Clear")
304
- save_button = gr.Button("Save Chat") # Save the chat button
305
- save_status = gr.Textbox(placeholder="Status will appear here", show_label=False, interactive=False) # Display save status
306
 
307
  question.submit(ask, [question, chat_history], [chatbot_ui, chat_history, question])
308
  clear_button.click(lambda: ([initial_message], [initial_message], ""), None, [chatbot_ui, chat_history, question], queue=False)
309
- save_button.click(save_chat_to_file, [chat_history], save_status) # Save the chat and show status message
 
 
310
 
311
  demo.queue()
312
  demo.launch(share=False)
 
271
  #demo.launch(share=False)
272
  # Initialize chat history with a welcome message
273
 
 
 
 
 
 
274
  def save_chat_to_file(chat_history):
275
+ timestamp = time.strftime("%Y%m%d-%H%M%S")
276
+ filename = f"chat_history_{timestamp}.json"
277
+
278
+ # Prepare the file content
279
+ file_content = json.dumps(chat_history, ensure_ascii=False, indent=2)
280
+
281
+ # Return the file as a download link
282
+ return (filename, file_content)
283
 
284
  def ask(user_message, chat_history):
285
  if not user_message:
286
  return chat_history, chat_history, ""
287
 
288
+ response = chatbot(user_message)
289
  chat_history.append((user_message, response))
290
 
291
  return chat_history, chat_history, ""
 
298
  chatbot_ui = gr.Chatbot(value=[initial_message])
299
  question = gr.Textbox(placeholder="Ask me anything about Moodle...", show_label=False)
300
  clear_button = gr.Button("Clear")
301
+ save_button = gr.Button("Save Chat") # to save the chat
 
302
 
303
  question.submit(ask, [question, chat_history], [chatbot_ui, chat_history, question])
304
  clear_button.click(lambda: ([initial_message], [initial_message], ""), None, [chatbot_ui, chat_history, question], queue=False)
305
+
306
+ # Clicking the save button will trigger file download
307
+ save_button.click(save_chat_to_file, [chat_history], gr.File())
308
 
309
  demo.queue()
310
  demo.launch(share=False)