Update app.py
Browse files
app.py
CHANGED
|
@@ -270,20 +270,20 @@ def chatbot(query):
|
|
| 270 |
#demo.queue()
|
| 271 |
#demo.launch(share=False)
|
| 272 |
# Initialize chat history with a welcome message
|
| 273 |
-
import io
|
| 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 |
|
| 279 |
-
#
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
| 287 |
|
| 288 |
def ask(user_message, chat_history):
|
| 289 |
if not user_message:
|
|
@@ -302,14 +302,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 302 |
chatbot_ui = gr.Chatbot(value=[initial_message])
|
| 303 |
question = gr.Textbox(placeholder="Ask me anything about Moodle...", show_label=False)
|
| 304 |
clear_button = gr.Button("Clear")
|
| 305 |
-
save_button = gr.Button("Save Chat")
|
| 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 |
|
| 310 |
-
|
| 311 |
-
save_button.click(save_chat_to_file, [chat_history], gr.File())
|
| 312 |
|
| 313 |
demo.queue()
|
| 314 |
demo.launch(share=False)
|
| 315 |
-
|
|
|
|
| 270 |
#demo.queue()
|
| 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 |
+
# Create a temporary file
|
| 279 |
+
temp_dir = tempfile.gettempdir()
|
| 280 |
+
file_path = os.path.join(temp_dir, filename)
|
| 281 |
+
|
| 282 |
+
# Write the chat history into the file
|
| 283 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 284 |
+
json.dump(chat_history, f, ensure_ascii=False, indent=2)
|
| 285 |
+
|
| 286 |
+
return file_path # THIS should be only the path, not a tuple!
|
| 287 |
|
| 288 |
def ask(user_message, chat_history):
|
| 289 |
if not user_message:
|
|
|
|
| 302 |
chatbot_ui = gr.Chatbot(value=[initial_message])
|
| 303 |
question = gr.Textbox(placeholder="Ask me anything about Moodle...", show_label=False)
|
| 304 |
clear_button = gr.Button("Clear")
|
| 305 |
+
save_button = gr.Button("Save Chat")
|
| 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 |
|
| 310 |
+
save_button.click(save_chat_to_file, [chat_history], gr.File(label="Download your chat history"))
|
|
|
|
| 311 |
|
| 312 |
demo.queue()
|
| 313 |
demo.launch(share=False)
|
|
|