Spaces:
Sleeping
Sleeping
Nils Durner
commited on
Commit
·
e8c6a19
1
Parent(s):
03f0948
chat history import/export
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import base64
|
| 3 |
import os
|
| 4 |
from openai import OpenAI
|
|
|
|
| 5 |
|
| 6 |
dump_controls = False
|
| 7 |
log_to_console = False
|
|
@@ -159,6 +160,20 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
|
|
| 159 |
|
| 160 |
return "", history
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
gr.Markdown("# OAI Chat (Nils' Version™️)")
|
| 164 |
|
|
@@ -238,4 +253,26 @@ with gr.Blocks() as demo:
|
|
| 238 |
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False, postprocess=False)
|
| 239 |
img_msg = img_btn.upload(add_img, [chatbot, img_btn], [chatbot], queue=False, postprocess=False)
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
demo.queue().launch()
|
|
|
|
| 2 |
import base64
|
| 3 |
import os
|
| 4 |
from openai import OpenAI
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
dump_controls = False
|
| 8 |
log_to_console = False
|
|
|
|
| 160 |
|
| 161 |
return "", history
|
| 162 |
|
| 163 |
+
def import_history(history, file):
|
| 164 |
+
with open(file.name, mode="rb") as f:
|
| 165 |
+
content = f.read()
|
| 166 |
+
|
| 167 |
+
if isinstance(content, bytes):
|
| 168 |
+
content = content.decode('utf-8', 'replace')
|
| 169 |
+
else:
|
| 170 |
+
content = str(content)
|
| 171 |
+
|
| 172 |
+
# Deserialize the JSON content to history
|
| 173 |
+
history = json.loads(content)
|
| 174 |
+
# The history is returned and will be set to the chatbot component
|
| 175 |
+
return history
|
| 176 |
+
|
| 177 |
with gr.Blocks() as demo:
|
| 178 |
gr.Markdown("# OAI Chat (Nils' Version™️)")
|
| 179 |
|
|
|
|
| 253 |
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False, postprocess=False)
|
| 254 |
img_msg = img_btn.upload(add_img, [chatbot, img_btn], [chatbot], queue=False, postprocess=False)
|
| 255 |
|
| 256 |
+
with gr.Accordion("Import/Export", open = False):
|
| 257 |
+
import_button = gr.UploadButton("Import")
|
| 258 |
+
export_button = gr.Button("Export")
|
| 259 |
+
export_button.click(lambda: None, [chatbot], js="""
|
| 260 |
+
(chat_history) => {
|
| 261 |
+
// Convert the chat history to a JSON string
|
| 262 |
+
const history_json = JSON.stringify(chat_history);
|
| 263 |
+
// Create a Blob from the JSON string
|
| 264 |
+
const blob = new Blob([history_json], {type: 'application/json'});
|
| 265 |
+
// Create a download link
|
| 266 |
+
const url = URL.createObjectURL(blob);
|
| 267 |
+
const a = document.createElement('a');
|
| 268 |
+
a.href = url;
|
| 269 |
+
a.download = 'chat_history.json';
|
| 270 |
+
document.body.appendChild(a);
|
| 271 |
+
a.click();
|
| 272 |
+
document.body.removeChild(a);
|
| 273 |
+
URL.revokeObjectURL(url);
|
| 274 |
+
}
|
| 275 |
+
""")
|
| 276 |
+
import_button.upload(import_history, inputs=[chatbot, import_button], outputs=[chatbot])
|
| 277 |
+
|
| 278 |
demo.queue().launch()
|