Spaces:
Sleeping
Sleeping
Nils Durner
commited on
Commit
·
679fce2
1
Parent(s):
e8c6a19
file download
Browse files
app.py
CHANGED
@@ -254,8 +254,8 @@ with gr.Blocks() as demo:
|
|
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
|
@@ -273,6 +273,35 @@ with gr.Blocks() as demo:
|
|
273 |
URL.revokeObjectURL(url);
|
274 |
}
|
275 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
import_button.upload(import_history, inputs=[chatbot, import_button], outputs=[chatbot])
|
277 |
|
278 |
demo.queue().launch()
|
|
|
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("History Import")
|
258 |
+
export_button = gr.Button("History Export")
|
259 |
export_button.click(lambda: None, [chatbot], js="""
|
260 |
(chat_history) => {
|
261 |
// Convert the chat history to a JSON string
|
|
|
273 |
URL.revokeObjectURL(url);
|
274 |
}
|
275 |
""")
|
276 |
+
dl_button = gr.Button("File download")
|
277 |
+
dl_button.click(lambda: None, [chatbot], js="""
|
278 |
+
(chat_history) => {
|
279 |
+
// Attempt to extract content enclosed in backticks with an optional filename
|
280 |
+
const contentRegex = /```(\\S*\\.(\\S+))?\\n?([\\s\\S]*?)```/;
|
281 |
+
const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);
|
282 |
+
if (match && match[3]) {
|
283 |
+
// Extract the content and the file extension
|
284 |
+
const content = match[3];
|
285 |
+
const fileExtension = match[2] || 'txt'; // Default to .txt if extension is not found
|
286 |
+
const filename = match[1] || `download.${fileExtension}`;
|
287 |
+
// Create a Blob from the content
|
288 |
+
const blob = new Blob([content], {type: `text/${fileExtension}`});
|
289 |
+
// Create a download link for the Blob
|
290 |
+
const url = URL.createObjectURL(blob);
|
291 |
+
const a = document.createElement('a');
|
292 |
+
a.href = url;
|
293 |
+
// If the filename from the chat history doesn't have an extension, append the default
|
294 |
+
a.download = filename.includes('.') ? filename : `${filename}.${fileExtension}`;
|
295 |
+
document.body.appendChild(a);
|
296 |
+
a.click();
|
297 |
+
document.body.removeChild(a);
|
298 |
+
URL.revokeObjectURL(url);
|
299 |
+
} else {
|
300 |
+
// Inform the user if the content is malformed or missing
|
301 |
+
alert('Sorry, the file content could not be found or is in an unrecognized format.');
|
302 |
+
}
|
303 |
+
}
|
304 |
+
""")
|
305 |
import_button.upload(import_history, inputs=[chatbot, import_button], outputs=[chatbot])
|
306 |
|
307 |
demo.queue().launch()
|