Spaces:
Sleeping
Sleeping
Keldos
commited on
Commit
•
03a9483
1
Parent(s):
d70f27b
WIP refactor: 改一下点击下载文件写到js文件里
Browse files- ChuanhuChatbot.py +3 -3
- modules/presets.py +0 -36
- web_assets/javascript/utils.js +37 -0
ChuanhuChatbot.py
CHANGED
@@ -73,9 +73,9 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
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,7 +583,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
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=
|
587 |
|
588 |
# Train
|
589 |
dataset_selection.upload(handle_dataset_selection, dataset_selection, [dataset_preview_json, upload_to_openai_btn, openai_train_status])
|
|
|
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("🗑️"), elem_id="gr-history-delete-btn")
|
77 |
with gr.Column(min_width=42, scale=1):
|
78 |
+
historyDownloadBtn = gr.Button(i18n("⏬"), elem_id="gr-history-download-btn")
|
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='(a,b)=>{return downloadHistory(a,b);}')
|
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
@@ -250,39 +250,3 @@ small_and_beautiful_theme = gr.themes.Soft(
|
|
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 |
-
"""
|
|
|
250 |
chatbot_code_background_color_dark="*neutral_950",
|
251 |
)
|
252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
web_assets/javascript/utils.js
CHANGED
@@ -15,6 +15,43 @@ function isImgUrl(url) {
|
|
15 |
return false;
|
16 |
}
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
/* NOTE: These reload functions are not used in the current version of the code.
|
20 |
* From stable-diffusion-webui
|
|
|
15 |
return false;
|
16 |
}
|
17 |
|
18 |
+
function downloadHistory(username, historyname) {
|
19 |
+
let fileUrl;
|
20 |
+
if (username === null) {
|
21 |
+
fileUrl = `/file=./history/${historyname}`;
|
22 |
+
} else {
|
23 |
+
fileUrl = `/file=./history/${username}/${historyname}`;
|
24 |
+
}
|
25 |
+
downloadFile(fileUrl, historyname);
|
26 |
+
}
|
27 |
+
|
28 |
+
function downloadFile(fileUrl, filename="") {
|
29 |
+
// 发送下载请求
|
30 |
+
fetch(fileUrl)
|
31 |
+
.then(response => response.blob())
|
32 |
+
.then(blob => {
|
33 |
+
// 创建一个临时的URL
|
34 |
+
const url = URL.createObjectURL(blob);
|
35 |
+
|
36 |
+
// 创建一个隐藏的<a>元素,设置下载属性
|
37 |
+
const a = document.createElement('a');
|
38 |
+
a.style.display = 'none';
|
39 |
+
a.href = url;
|
40 |
+
a.download = filename;
|
41 |
+
|
42 |
+
// 添加到DOM并触发点击事件
|
43 |
+
document.body.appendChild(a);
|
44 |
+
a.click();
|
45 |
+
|
46 |
+
// 清理临时URL和DOM中的<a>元素
|
47 |
+
URL.revokeObjectURL(url);
|
48 |
+
document.body.removeChild(a);
|
49 |
+
})
|
50 |
+
.catch(error => {
|
51 |
+
console.error('Failed to download file:', error);
|
52 |
+
});
|
53 |
+
}
|
54 |
+
|
55 |
|
56 |
/* NOTE: These reload functions are not used in the current version of the code.
|
57 |
* From stable-diffusion-webui
|