Tuchuanhuhuhu commited on
Commit
ebd3a13
1 Parent(s): d86a58c

feat: Added a button to download history

Browse files
Files changed (2) hide show
  1. ChuanhuChatbot.py +5 -2
  2. modules/presets.py +37 -0
ChuanhuChatbot.py CHANGED
@@ -71,9 +71,11 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
71
  )
72
  with gr.Row():
73
  with gr.Column(min_width=42, scale=1):
74
- historyRefreshBtn = gr.Button(i18n("🔄 刷新"))
75
  with gr.Column(min_width=42, scale=1):
76
- historyDeleteBtn = gr.Button(i18n("🗑️ 删除"))
 
 
77
  with gr.Row():
78
  with gr.Column(scale=6):
79
  saveFileName = gr.Textbox(
@@ -581,6 +583,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
581
  historyDeleteBtn.click(delete_chat_history, [current_model, historyFileSelectDropdown, user_name], [status_display, historyFileSelectDropdown, chatbot], _js='(a,b,c)=>{return showConfirmationDialog(a, b, c);}')
582
  historyFileSelectDropdown.change(**load_history_from_file_args)
583
  downloadFile.change(upload_chat_history, [current_model, downloadFile, user_name], [saveFileName, systemPromptTxt, chatbot])
 
584
 
585
  # Train
586
  dataset_selection.upload(handle_dataset_selection, dataset_selection, [dataset_preview_json, upload_to_openai_btn, openai_train_status])
 
71
  )
72
  with gr.Row():
73
  with gr.Column(min_width=42, scale=1):
74
+ historyRefreshBtn = gr.Button(i18n("🔄"))
75
  with gr.Column(min_width=42, scale=1):
76
+ historyDeleteBtn = gr.Button(i18n("🗑️"))
77
+ with gr.Column(min_width=42, scale=1):
78
+ historyDownloadBtn = gr.Button(i18n("⏬"))
79
  with gr.Row():
80
  with gr.Column(scale=6):
81
  saveFileName = gr.Textbox(
 
583
  historyDeleteBtn.click(delete_chat_history, [current_model, historyFileSelectDropdown, user_name], [status_display, historyFileSelectDropdown, chatbot], _js='(a,b,c)=>{return showConfirmationDialog(a, b, c);}')
584
  historyFileSelectDropdown.change(**load_history_from_file_args)
585
  downloadFile.change(upload_chat_history, [current_model, downloadFile, user_name], [saveFileName, systemPromptTxt, chatbot])
586
+ historyDownloadBtn.click(None, [user_name, historyFileSelectDropdown], None, _js=download_history_js)
587
 
588
  # Train
589
  dataset_selection.upload(handle_dataset_selection, dataset_selection, [dataset_preview_json, upload_to_openai_btn, openai_train_status])
modules/presets.py CHANGED
@@ -249,3 +249,40 @@ small_and_beautiful_theme = gr.themes.Soft(
249
  # gradio 会把这个几个chatbot打头的变量应用到其他md渲染的地方,鬼晓得怎么想的。。。
250
  chatbot_code_background_color_dark="*neutral_950",
251
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  # gradio 会把这个几个chatbot打头的变量应用到其他md渲染的地方,鬼晓得怎么想的。。。
250
  chatbot_code_background_color_dark="*neutral_950",
251
  )
252
+
253
+ download_history_js = """
254
+ function downloadFile(username, historyname) {
255
+ // 构建文件的URL
256
+ let fileUrl;
257
+ if (username === null) {
258
+ fileUrl = `/file=./history/${historyname}`;
259
+ } else {
260
+ fileUrl = `/file=./history/${username}/${historyname}`;
261
+ }
262
+
263
+ // 发送下载请求
264
+ fetch(fileUrl)
265
+ .then(response => response.blob())
266
+ .then(blob => {
267
+ // 创建一个临时的URL
268
+ const url = URL.createObjectURL(blob);
269
+
270
+ // 创建一个隐藏的<a>元素,设置下载属性
271
+ const a = document.createElement('a');
272
+ a.style.display = 'none';
273
+ a.href = url;
274
+ a.download = historyname;
275
+
276
+ // 添加到DOM并触发点击事件
277
+ document.body.appendChild(a);
278
+ a.click();
279
+
280
+ // 清理临时URL和DOM中的<a>元素
281
+ URL.revokeObjectURL(url);
282
+ document.body.removeChild(a);
283
+ })
284
+ .catch(error => {
285
+ console.error('下载文件出错:', error);
286
+ });
287
+ }
288
+ """