Nils Durner commited on
Commit
39ee51b
·
1 Parent(s): 32ad276

chat history export/import

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py CHANGED
@@ -95,6 +95,21 @@ def bot(message, history, aws_access, aws_secret, aws_token, temperature, max_to
95
 
96
  return "", history
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  with gr.Blocks() as demo:
99
  gr.Markdown("# Amazon™️ Bedrock™️ Chat™️ (Nils' Version™️) feat. Anthropic™️ Claude-2™️")
100
 
@@ -163,6 +178,28 @@ with gr.Blocks() as demo:
163
  txt_dmp = gr.Textbox("Dump")
164
  dmp_btn.click(dump, inputs=[chatbot], outputs=[txt_dmp])
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
167
  bot, [txt, chatbot, aws_access, aws_secret, aws_token, temp, max_tokens], [txt, chatbot],
168
  )
 
95
 
96
  return "", history
97
 
98
+
99
+ def import_history(history, file):
100
+ with open(file.name, mode="rb") as f:
101
+ content = f.read()
102
+
103
+ if isinstance(content, bytes):
104
+ content = content.decode('utf-8', 'replace')
105
+ else:
106
+ content = str(content)
107
+
108
+ # Deserialize the JSON content to history
109
+ history = json.loads(content)
110
+ # The history is returned and will be set to the chatbot component
111
+ return history
112
+
113
  with gr.Blocks() as demo:
114
  gr.Markdown("# Amazon™️ Bedrock™️ Chat™️ (Nils' Version™️) feat. Anthropic™️ Claude-2™️")
115
 
 
178
  txt_dmp = gr.Textbox("Dump")
179
  dmp_btn.click(dump, inputs=[chatbot], outputs=[txt_dmp])
180
 
181
+ with gr.Accordion("Import/Export", open = False):
182
+ import_button = gr.UploadButton("Import")
183
+ export_button = gr.Button("Export")
184
+ export_button.click(lambda: None, [chatbot], js="""
185
+ (chat_history) => {
186
+ // Convert the chat history to a JSON string
187
+ const history_json = JSON.stringify(chat_history);
188
+ // Create a Blob from the JSON string
189
+ const blob = new Blob([history_json], {type: 'application/json'});
190
+ // Create a download link
191
+ const url = URL.createObjectURL(blob);
192
+ const a = document.createElement('a');
193
+ a.href = url;
194
+ a.download = 'chat_history.json';
195
+ document.body.appendChild(a);
196
+ a.click();
197
+ document.body.removeChild(a);
198
+ URL.revokeObjectURL(url);
199
+ }
200
+ """)
201
+ import_button.upload(import_history, inputs=[chatbot, import_button], outputs=[chatbot])
202
+
203
  txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
204
  bot, [txt, chatbot, aws_access, aws_secret, aws_token, temp, max_tokens], [txt, chatbot],
205
  )