Keldos commited on
Commit
27fbd0c
·
1 Parent(s): 30e4bc9

WIP: export+download markdown

Browse files
ChuanhuChatbot.py CHANGED
@@ -99,7 +99,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
99
  saveHistoryBtn = gr.Button(
100
  i18n("💾 保存对话"), elem_id="gr-history-save-btn")
101
  exportMarkdownBtn = gr.Button(
102
- i18n("📝 导出为Markdown"))
103
  gr.Markdown(i18n("默认保存于history文件夹"))
104
  with gr.Row():
105
  with gr.Column():
@@ -667,9 +667,9 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
667
  downloadFile.change(upload_chat_history, [current_model, downloadFile, user_name], [
668
  saveFileName, systemPromptTxt, chatbot])
669
  historyDownloadBtn.click(None, [
670
- user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b);}')
671
  historyMarkdownDownloadBtn.click(None, [
672
- user_name, historySelectList], None, _js='(a,b)=>{return downloadHistoryMarkdown(a,b);}')
673
  historySearchTextbox.input(
674
  filter_history,
675
  [user_name, historySearchTextbox],
 
99
  saveHistoryBtn = gr.Button(
100
  i18n("💾 保存对话"), elem_id="gr-history-save-btn")
101
  exportMarkdownBtn = gr.Button(
102
+ i18n("📝 导出为Markdown"), elem_id="gr-markdown-export-btn")
103
  gr.Markdown(i18n("默认保存于history文件夹"))
104
  with gr.Row():
105
  with gr.Column():
 
667
  downloadFile.change(upload_chat_history, [current_model, downloadFile, user_name], [
668
  saveFileName, systemPromptTxt, chatbot])
669
  historyDownloadBtn.click(None, [
670
+ user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b,".json");}')
671
  historyMarkdownDownloadBtn.click(None, [
672
+ user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b,".md");}')
673
  historySearchTextbox.input(
674
  filter_history,
675
  [user_name, historySearchTextbox],
web_assets/javascript/ChuanhuChat.js CHANGED
@@ -93,10 +93,10 @@ function initialize() {
93
  toolbox = gradioApp().querySelector('#toolbox-area');
94
  // trainBody = gradioApp().querySelector('#train-body');
95
 
96
- if (loginUserForm) {
97
- localStorage.setItem("userLogged", true);
98
- userLogged = true;
99
- }
100
 
101
  adjustDarkMode();
102
  adjustSide();
 
93
  toolbox = gradioApp().querySelector('#toolbox-area');
94
  // trainBody = gradioApp().querySelector('#train-body');
95
 
96
+ // if (loginUserForm) {
97
+ // localStorage.setItem("userLogged", true);
98
+ // userLogged = true;
99
+ // }
100
 
101
  adjustDarkMode();
102
  adjustSide();
web_assets/javascript/chat-list.js CHANGED
@@ -1,9 +1,22 @@
1
 
 
 
2
  function setChatList() {
3
  var selectedChat = null;
4
  var chatList = gradioApp().querySelector('fieldset#history-select-dropdown');
5
  selectedChat = chatList.querySelector(".wrap label.selected")
6
- if (!selectedChat || selectedChat.classList.contains('added-chat-btns')) {
 
 
 
 
 
 
 
 
 
 
 
7
  return;
8
  }
9
 
 
1
 
2
+ var currentChatName = null;
3
+
4
  function setChatList() {
5
  var selectedChat = null;
6
  var chatList = gradioApp().querySelector('fieldset#history-select-dropdown');
7
  selectedChat = chatList.querySelector(".wrap label.selected")
8
+ if (!selectedChat) {
9
+ currentChatName = null;
10
+ return;
11
+ }
12
+
13
+ // if (userLogged) {
14
+ // currentChatName = username + "/" + selectedChat.querySelector('span').innerText;
15
+ // } else {
16
+ currentChatName = selectedChat.querySelector('span').innerText;
17
+ // }
18
+
19
+ if (selectedChat.classList.contains('added-chat-btns')) {
20
  return;
21
  }
22
 
web_assets/javascript/fake-gradio.js CHANGED
@@ -9,7 +9,10 @@ function jsonDownloadClick() {
9
  gradioApp().querySelector('#gr-history-download-btn').click();
10
  }
11
  function mdDownloadClick() {
12
- return;
 
 
 
13
  }
14
 
15
  // index files
 
9
  gradioApp().querySelector('#gr-history-download-btn').click();
10
  }
11
  function mdDownloadClick() {
12
+ gradioApp().querySelector('#gr-markdown-export-btn').click();
13
+ gradioApp().querySelector('#gr-history-mardown-download-btn').click();
14
+
15
+ // downloadHistory(username, currentChatName, ".md");
16
  }
17
 
18
  // index files
web_assets/javascript/user-info.js CHANGED
@@ -1,6 +1,7 @@
1
 
2
- var userLogged = false;
3
  var usernameGotten = false;
 
4
  var username = null;
5
 
6
 
@@ -8,27 +9,28 @@ function getUserInfo() {
8
  if (usernameGotten) {
9
  return;
10
  }
11
- userLogged = localStorage.getItem('userLogged');
12
- if (userLogged) {
13
- username = userInfoDiv.innerText;
14
- if (username) {
15
- if (username.includes("getting user info")) {
16
- setTimeout(getUserInfo, 500);
17
- return;
18
- } else if (username === " ") {
19
- localStorage.removeItem("username");
20
- localStorage.removeItem("userLogged")
21
- userLogged = false;
22
- usernameGotten = true;
23
- return;
24
- } else {
25
- username = username.match(/User:\s*(.*)/)[1] || username;
26
- localStorage.setItem("username", username);
27
- usernameGotten = true;
28
- clearHistoryHtml();
29
- }
30
  }
31
  }
 
32
  }
33
 
34
  function showOrHideUserInfo() {
 
1
 
2
+ // var userLogged = false;
3
  var usernameGotten = false;
4
+ var usernameTmp = null;
5
  var username = null;
6
 
7
 
 
9
  if (usernameGotten) {
10
  return;
11
  }
12
+ // userLogged = localStorage.getItem('userLogged');
13
+ // if (userLogged) {
14
+ usernameTmp = userInfoDiv.innerText;
15
+ if (usernameTmp) {
16
+ if (usernameTmp.includes("getting user info")) {
17
+ setTimeout(getUserInfo, 500);
18
+ return;
19
+ } else if (usernameTmp === " ") {
20
+ localStorage.removeItem("username");
21
+ // localStorage.removeItem("userLogged")
22
+ // userLogged = false;
23
+ usernameGotten = true;
24
+ return;
25
+ } else {
26
+ usernameTmp = usernameTmp.match(/User:\s*(.*)/)[1] || usernameTmp;
27
+ localStorage.setItem("username", usernameTmp);
28
+ username = usernameTmp;
29
+ usernameGotten = true;
30
+ clearHistoryHtml();
31
  }
32
  }
33
+ // }
34
  }
35
 
36
  function showOrHideUserInfo() {
web_assets/javascript/utils.js CHANGED
@@ -15,52 +15,62 @@ function isImgUrl(url) {
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 + ".json", historyname + ".json");
 
26
  }
27
 
28
- function downloadHistoryMarkdown(username, historyname) {
29
- let fileUrl;
30
- if (username === null) {
31
- fileUrl = `/file=./history/${historyname}`;
32
- } else {
33
- fileUrl = `/file=./history/${username}/${historyname}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  }
35
- downloadFile(fileUrl + ".md", historyname + ".md");
36
- }
37
 
38
- function downloadFile(fileUrl, filename="") {
39
- // 发送下载请求
40
- fetch(fileUrl)
41
- .then(response => response.blob())
42
- .then(blob => {
43
- // 创建一个临时的URL
44
- const url = URL.createObjectURL(blob);
45
-
46
- // 创建一个隐藏的<a>元素,设置下载属性
47
- const a = document.createElement('a');
48
- a.style.display = 'none';
49
- a.href = url;
50
- a.download = filename;
51
-
52
- // 添加到DOM并触发点击事件
53
- document.body.appendChild(a);
54
- a.click();
55
-
56
- // 清理临时URL和DOM中的<a>元素
57
- URL.revokeObjectURL(url);
58
- document.body.removeChild(a);
59
- })
60
- .catch(error => {
61
- console.error('Failed to download file:', error);
62
- });
63
  }
 
64
 
65
 
66
  /* NOTE: These reload functions are not used in the current version of the code.
 
15
  return false;
16
  }
17
 
18
+ function downloadHistory(gradioUsername, historyname, format=".json") {
19
  let fileUrl;
20
+ if (gradioUsername === null || usegradioUsernamername.trim() === "") {
21
  fileUrl = `/file=./history/${historyname}`;
22
  } else {
23
+ fileUrl = `/file=./history/${gradioUsername}/${historyname}`;
24
  }
25
+ console.log("gradioUsername",gradioUsername, "fileUrl ",fileUrl, "historyname", historyname, "format", format);
26
+ downloadFile(fileUrl, historyname, format);
27
  }
28
 
29
+ function downloadFile(fileUrl, filename = "", format = "", retryTimeout = 200, maxAttempts = 10) {
30
+
31
+ fileUrl = fileUrl + format;
32
+ filename = filename + format;
33
+
34
+ let attempts = 0;
35
+
36
+ async function tryDownload() {
37
+ if (attempts >= maxAttempts) {
38
+ console.error('Max attempts reached, download failed.');
39
+ alert('Download failed:' + filename);
40
+ return;
41
+ }
42
+ try {
43
+ const response = await fetch(fileUrl);
44
+ if (!response.ok) {
45
+ attempts++;
46
+ console.error("Error fetching file, retrying...");
47
+ setTimeout(tryDownload, retryTimeout);
48
+ } else {
49
+ response.blob()
50
+ .then(blob => {
51
+ const url = URL.createObjectURL(blob);
52
+ const a = document.createElement('a');
53
+ a.style.display = 'none';
54
+ a.href = url;
55
+ a.download = filename;
56
+ document.body.appendChild(a);
57
+ a.click();
58
+ URL.revokeObjectURL(url);
59
+ document.body.removeChild(a);
60
+ })
61
+ .catch(error => {
62
+ console.error('Error downloading file:', error);
63
+ });
64
+ }
65
+ } catch (error) {
66
+ attempts++;
67
+ setTimeout(tryDownload, retryTimeout);
68
+ }
69
  }
 
 
70
 
71
+ tryDownload();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  }
73
+
74
 
75
 
76
  /* NOTE: These reload functions are not used in the current version of the code.