Spaces:
Build error
Build error
Commit ·
0d2f728
1
Parent(s): 6762a06
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,6 +58,20 @@ def bot_respond(chat_history, openai_gpt_key, model_choice):
|
|
| 58 |
time.sleep(0.02)
|
| 59 |
yield chat_history
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Create a Gradio interface
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
# Textbox for OpenAI GPT API Key
|
|
@@ -85,8 +99,18 @@ with gr.Blocks() as demo:
|
|
| 85 |
bot_respond, [chat_history, openai_gpt_key, model_choice], chat_history).then(
|
| 86 |
lambda: gr.update(interactive=True), None, [user_text], queue=False)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Clear button click event
|
| 89 |
-
clear_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
# Queue the Gradio interface
|
|
|
|
| 58 |
time.sleep(0.02)
|
| 59 |
yield chat_history
|
| 60 |
|
| 61 |
+
def save_chat_history(chat_history):
|
| 62 |
+
global messages
|
| 63 |
+
messages = []
|
| 64 |
+
# You could format this as you like. This will just create a text representation.
|
| 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 |
+
|
| 75 |
# Create a Gradio interface
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
# Textbox for OpenAI GPT API Key
|
|
|
|
| 99 |
bot_respond, [chat_history, openai_gpt_key, model_choice], chat_history).then(
|
| 100 |
lambda: gr.update(interactive=True), None, [user_text], queue=False)
|
| 101 |
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
# Clear button click event
|
| 107 |
+
clear_btn.click(
|
| 108 |
+
lambda: save_chat_history(chat_history.get_value()),
|
| 109 |
+
None,
|
| 110 |
+
chat_history,
|
| 111 |
+
queue=False
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
# Queue the Gradio interface
|