Spaces:
Sleeping
Sleeping
Keldos
commited on
Commit
·
cfb1733
1
Parent(s):
d2cbf72
feat: 登录用户不再自动加载假历史
Browse files- ChuanhuChatbot.py +1 -1
- assets/custom.js +64 -23
ChuanhuChatbot.py
CHANGED
@@ -274,7 +274,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
274 |
logging.info(f"Get User Name: {request.username}")
|
275 |
return gr.Markdown.update(value=f"User: {request.username}"), request.username
|
276 |
else:
|
277 |
-
return gr.Markdown.update(value=f"
|
278 |
demo.load(create_greeting, inputs=None, outputs=[user_info, user_name])
|
279 |
demo.load(refresh_ui_elements_on_load, [current_model, model_select_dropdown, user_name], [like_dislike_area, systemPromptTxt, chatbot], show_progress=False)
|
280 |
chatgpt_predict_args = dict(
|
|
|
274 |
logging.info(f"Get User Name: {request.username}")
|
275 |
return gr.Markdown.update(value=f"User: {request.username}"), request.username
|
276 |
else:
|
277 |
+
return gr.Markdown.update(value=f"", visible=False), ""
|
278 |
demo.load(create_greeting, inputs=None, outputs=[user_info, user_name])
|
279 |
demo.load(refresh_ui_elements_on_load, [current_model, model_select_dropdown, user_name], [like_dislike_area, systemPromptTxt, chatbot], show_progress=False)
|
280 |
chatgpt_predict_args = dict(
|
assets/custom.js
CHANGED
@@ -18,6 +18,11 @@ var apSwitch = null;
|
|
18 |
var empty_botton = null;
|
19 |
var messageBotDivs = null;
|
20 |
var renderLatex = null;
|
|
|
|
|
|
|
|
|
|
|
21 |
var shouldRenderLatex = false;
|
22 |
var historyLoaded = false;
|
23 |
|
@@ -29,6 +34,7 @@ var isInIframe = (window.self !== window.top);
|
|
29 |
function gradioLoaded(mutations) {
|
30 |
for (var i = 0; i < mutations.length; i++) {
|
31 |
if (mutations[i].addedNodes.length) {
|
|
|
32 |
gradioContainer = document.querySelector(".gradio-container");
|
33 |
user_input_tb = document.getElementById('user_input_tb');
|
34 |
userInfoDiv = document.getElementById("user_info");
|
@@ -39,6 +45,11 @@ function gradioLoaded(mutations) {
|
|
39 |
renderLatex = document.querySelector("#render_latex_checkbox > label > input");
|
40 |
empty_botton = document.getElementById("empty_btn")
|
41 |
|
|
|
|
|
|
|
|
|
|
|
42 |
if (gradioContainer && apSwitch) { // gradioCainter 加载出来了没?
|
43 |
adjustDarkMode();
|
44 |
}
|
@@ -46,13 +57,16 @@ function gradioLoaded(mutations) {
|
|
46 |
selectHistory();
|
47 |
}
|
48 |
if (userInfoDiv && appTitleDiv) { // userInfoDiv 和 appTitleDiv 加载出来了没?
|
|
|
|
|
|
|
49 |
setTimeout(showOrHideUserInfo(), 2000);
|
50 |
}
|
51 |
if (chatbot) { // chatbot 加载出来了没?
|
52 |
setChatbotHeight();
|
53 |
}
|
54 |
if (chatbotWrap) {
|
55 |
-
if (!historyLoaded){
|
56 |
loadHistoryHtml();
|
57 |
}
|
58 |
}
|
@@ -115,6 +129,33 @@ function selectHistory() {
|
|
115 |
}
|
116 |
}
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
function toggleUserInfoVisibility(shouldHide) {
|
119 |
if (userInfoDiv) {
|
120 |
if (shouldHide) {
|
@@ -329,44 +370,44 @@ mObserver.observe(targetNode, { attributes: true, childList: true, subtree: true
|
|
329 |
var loadhistorytime = 0; // for debugging
|
330 |
function saveHistoryHtml() {
|
331 |
var historyHtml = document.querySelector('#chuanhu_chatbot > .wrap');
|
332 |
-
// innerHTML;
|
333 |
localStorage.setItem('chatHistory', historyHtml.innerHTML);
|
334 |
-
console.log("
|
335 |
historyLoaded = false;
|
336 |
}
|
337 |
-
|
338 |
-
var fakeHistory;
|
339 |
function loadHistoryHtml() {
|
340 |
var historyHtml = localStorage.getItem('chatHistory');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
341 |
if (!historyLoaded) {
|
342 |
-
|
343 |
-
if (!fakeHistory) {
|
344 |
-
fakeHistory = document.createElement('div');
|
345 |
fakeHistory.classList.add('history-message');
|
346 |
fakeHistory.innerHTML = historyHtml;
|
347 |
chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
|
348 |
-
// chatbotWrap.appendChild(fakeHistory);
|
349 |
-
} else {
|
350 |
-
chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
|
351 |
-
// chatbotWrap.appendChild(fakeHistory);
|
352 |
-
}
|
353 |
historyLoaded = true;
|
354 |
-
// localStorage.removeItem("chatHistory");
|
355 |
console.log("History Loaded");
|
356 |
loadhistorytime += 1; // for debugging
|
357 |
} else {
|
358 |
historyLoaded = false;
|
359 |
}
|
360 |
}
|
361 |
-
|
362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
empty_botton.addEventListener("click", function () {
|
364 |
-
|
365 |
-
historyMessages = chatbotWrap.querySelector('.history-message');
|
366 |
-
if (historyMessages) {
|
367 |
-
chatbotWrap.removeChild(historyMessages);
|
368 |
-
console.log("History Cleared");
|
369 |
-
}
|
370 |
});
|
371 |
}
|
372 |
|
@@ -379,7 +420,7 @@ observer.observe(targetNode, { childList: true, subtree: true });
|
|
379 |
// 监视页面变化
|
380 |
window.addEventListener("DOMContentLoaded", function () {
|
381 |
isInIframe = (window.self !== window.top);
|
382 |
-
historyLoaded = false
|
383 |
});
|
384 |
window.addEventListener('resize', setChatbotHeight);
|
385 |
window.addEventListener('scroll', setChatbotHeight);
|
|
|
18 |
var empty_botton = null;
|
19 |
var messageBotDivs = null;
|
20 |
var renderLatex = null;
|
21 |
+
var loginUserForm = null;
|
22 |
+
var logginUser = null;
|
23 |
+
|
24 |
+
var userLogged = false;
|
25 |
+
var usernameGotten = false;
|
26 |
var shouldRenderLatex = false;
|
27 |
var historyLoaded = false;
|
28 |
|
|
|
34 |
function gradioLoaded(mutations) {
|
35 |
for (var i = 0; i < mutations.length; i++) {
|
36 |
if (mutations[i].addedNodes.length) {
|
37 |
+
loginUserForm = document.querySelector(".gradio-container > .main > .wrap > .panel > .form")
|
38 |
gradioContainer = document.querySelector(".gradio-container");
|
39 |
user_input_tb = document.getElementById('user_input_tb');
|
40 |
userInfoDiv = document.getElementById("user_info");
|
|
|
45 |
renderLatex = document.querySelector("#render_latex_checkbox > label > input");
|
46 |
empty_botton = document.getElementById("empty_btn")
|
47 |
|
48 |
+
if (loginUserForm) {
|
49 |
+
localStorage.setItem("userLogged", true);
|
50 |
+
userLogged = true;
|
51 |
+
}
|
52 |
+
|
53 |
if (gradioContainer && apSwitch) { // gradioCainter 加载出来了没?
|
54 |
adjustDarkMode();
|
55 |
}
|
|
|
57 |
selectHistory();
|
58 |
}
|
59 |
if (userInfoDiv && appTitleDiv) { // userInfoDiv 和 appTitleDiv 加载出来了没?
|
60 |
+
if (!usernameGotten) {
|
61 |
+
getUserInfo();
|
62 |
+
}
|
63 |
setTimeout(showOrHideUserInfo(), 2000);
|
64 |
}
|
65 |
if (chatbot) { // chatbot 加载出来了没?
|
66 |
setChatbotHeight();
|
67 |
}
|
68 |
if (chatbotWrap) {
|
69 |
+
if (!historyLoaded) {
|
70 |
loadHistoryHtml();
|
71 |
}
|
72 |
}
|
|
|
129 |
}
|
130 |
}
|
131 |
|
132 |
+
var username = null;
|
133 |
+
function getUserInfo() {
|
134 |
+
if (usernameGotten) {
|
135 |
+
return;
|
136 |
+
}
|
137 |
+
if (userLogged) {
|
138 |
+
username = userInfoDiv.innerText;
|
139 |
+
if (username) {
|
140 |
+
if (username === "getting user info…\n") {
|
141 |
+
setTimeout(getUserInfo, 500);
|
142 |
+
return;
|
143 |
+
} else if (username === " ") {
|
144 |
+
localStorage.removeItem("username");
|
145 |
+
localStorage.removeItem("userLogged")
|
146 |
+
userLogged = false;
|
147 |
+
usernameGotten = true;
|
148 |
+
return;
|
149 |
+
} else {
|
150 |
+
username = username.match(/User:\s*(.*)/)[1] || username;
|
151 |
+
localStorage.setItem("username", username);
|
152 |
+
usernameGotten = true;
|
153 |
+
clearHistoryHtml();
|
154 |
+
}
|
155 |
+
}
|
156 |
+
}
|
157 |
+
}
|
158 |
+
|
159 |
function toggleUserInfoVisibility(shouldHide) {
|
160 |
if (userInfoDiv) {
|
161 |
if (shouldHide) {
|
|
|
370 |
var loadhistorytime = 0; // for debugging
|
371 |
function saveHistoryHtml() {
|
372 |
var historyHtml = document.querySelector('#chuanhu_chatbot > .wrap');
|
|
|
373 |
localStorage.setItem('chatHistory', historyHtml.innerHTML);
|
374 |
+
console.log("History Saved")
|
375 |
historyLoaded = false;
|
376 |
}
|
|
|
|
|
377 |
function loadHistoryHtml() {
|
378 |
var historyHtml = localStorage.getItem('chatHistory');
|
379 |
+
if (!historyHtml) {
|
380 |
+
historyLoaded = true;
|
381 |
+
return; // no history, do nothing
|
382 |
+
}
|
383 |
+
userLogged = localStorage.getItem('userLogged');
|
384 |
+
if (userLogged){
|
385 |
+
historyLoaded = true;
|
386 |
+
return; // logged in, do nothing
|
387 |
+
}
|
388 |
if (!historyLoaded) {
|
389 |
+
var fakeHistory = document.createElement('div');
|
|
|
|
|
390 |
fakeHistory.classList.add('history-message');
|
391 |
fakeHistory.innerHTML = historyHtml;
|
392 |
chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
|
|
|
|
|
|
|
|
|
|
|
393 |
historyLoaded = true;
|
|
|
394 |
console.log("History Loaded");
|
395 |
loadhistorytime += 1; // for debugging
|
396 |
} else {
|
397 |
historyLoaded = false;
|
398 |
}
|
399 |
}
|
400 |
+
function clearHistoryHtml() {
|
401 |
+
localStorage.removeItem("chatHistory");
|
402 |
+
historyMessages = chatbotWrap.querySelector('.history-message');
|
403 |
+
if (historyMessages) {
|
404 |
+
chatbotWrap.removeChild(historyMessages);
|
405 |
+
console.log("History Cleared");
|
406 |
+
}
|
407 |
+
}
|
408 |
+
function emptyHistory() {
|
409 |
empty_botton.addEventListener("click", function () {
|
410 |
+
clearHistoryHtml();
|
|
|
|
|
|
|
|
|
|
|
411 |
});
|
412 |
}
|
413 |
|
|
|
420 |
// 监视页面变化
|
421 |
window.addEventListener("DOMContentLoaded", function () {
|
422 |
isInIframe = (window.self !== window.top);
|
423 |
+
historyLoaded = false;
|
424 |
});
|
425 |
window.addEventListener('resize', setChatbotHeight);
|
426 |
window.addEventListener('scroll', setChatbotHeight);
|