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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -271,26 +271,26 @@ def chatbot(query):
271
  #demo.launch(share=False)
272
  # Initialize chat history with a welcome message
273
 
 
 
 
274
 
 
275
  def save_chat_to_file(chat_history):
276
- timestamp = time.strftime("%Y%m%d-%H%M%S")
277
- filename = f"chat_history_{timestamp}.json"
278
  with open(filename, "w", encoding="utf-8") as f:
279
  json.dump(chat_history, f, ensure_ascii=False, indent=2)
280
  print(f"Chat history saved to {filename}")
281
- return None
282
 
283
  def ask(user_message, chat_history):
284
  if not user_message:
285
  return chat_history, chat_history, ""
286
 
287
- response = chatbot(user_message)
288
  chat_history.append((user_message, response))
289
 
290
- # Save chat history to file
291
- with open(HISTORY_FILE, "w", encoding="utf-8") as f:
292
- json.dump(chat_history, f, ensure_ascii=False, indent=2)
293
-
294
  return chat_history, chat_history, ""
295
 
296
  initial_message = (None, "Hello, how can I help you with Moodle?")
@@ -301,11 +301,12 @@ 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") # to save the chat
 
305
 
306
  question.submit(ask, [question, chat_history], [chatbot_ui, chat_history, question])
307
  clear_button.click(lambda: ([initial_message], [initial_message], ""), None, [chatbot_ui, chat_history, question], queue=False)
308
- save_button.click(save_chat_to_file, [chat_history], None) # to save the chat
309
 
310
  demo.queue()
311
  demo.launch(share=False)
 
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, ""
295
 
296
  initial_message = (None, "Hello, how can I help you with Moodle?")
 
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)