Spaces:
Build error
Build error
Commit
·
c6dc9b9
1
Parent(s):
47fb7a0
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,17 +58,18 @@ def bot_respond(chat_history, openai_gpt_key, model_choice):
|
|
| 58 |
time.sleep(0.02)
|
| 59 |
yield chat_history
|
| 60 |
|
| 61 |
-
def save_chat_history(
|
| 62 |
global messages
|
| 63 |
-
messages
|
| 64 |
-
|
| 65 |
-
formatted_chat = "\n".join([f"User: {message[0]}\nBot: {message[1]}" for message in chat_history])
|
| 66 |
|
| 67 |
# Use a timestamp for a unique filename for each conversation
|
| 68 |
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 69 |
with open(f'chat_history_{timestamp}.txt', 'w') as f:
|
| 70 |
f.write(formatted_chat)
|
| 71 |
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
|
|
@@ -105,7 +106,7 @@ with gr.Blocks() as demo:
|
|
| 105 |
|
| 106 |
# Clear button click event
|
| 107 |
clear_btn.click(
|
| 108 |
-
lambda: save_chat_history(
|
| 109 |
None,
|
| 110 |
chat_history,
|
| 111 |
queue=False
|
|
|
|
| 58 |
time.sleep(0.02)
|
| 59 |
yield chat_history
|
| 60 |
|
| 61 |
+
def save_chat_history():
|
| 62 |
global messages
|
| 63 |
+
# Reset messages after saving the history
|
| 64 |
+
formatted_chat = "\n".join([f"{message['role']}: {message['content']}" for message in messages])
|
|
|
|
| 65 |
|
| 66 |
# Use a timestamp for a unique filename for each conversation
|
| 67 |
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 68 |
with open(f'chat_history_{timestamp}.txt', 'w') as f:
|
| 69 |
f.write(formatted_chat)
|
| 70 |
|
| 71 |
+
# Clear the messages list for a new conversation
|
| 72 |
+
messages = []
|
| 73 |
|
| 74 |
|
| 75 |
|
|
|
|
| 106 |
|
| 107 |
# Clear button click event
|
| 108 |
clear_btn.click(
|
| 109 |
+
lambda: save_chat_history(),
|
| 110 |
None,
|
| 111 |
chat_history,
|
| 112 |
queue=False
|