diff --git a/__main__.py b/__main__.py index 6e5a3b2..19dfd34 100644 --- a/__main__.py +++ b/__main__.py @@ -79,7 +79,7 @@ class ChatBot(ChatBotFrame): with gr.Box(elem_id='chat_box'): with gr.Row(): gr.Button(elem_classes='sm_btn').style(size='sm', full_width=False) - gr.HTML(func_box.get_html("appearance_switcher.html").format(label=""), elem_classes="insert_block") + gr.HTML(func_box.get_html("appearance_switcher.html").format(label=""), elem_id='user_input_tb', elem_classes="insert_block") with gr.Row(): self.txt = gr.Textbox(show_label=False, placeholder="Input question here.", elem_classes='chat_input').style(container=False) self.input_copy = gr.State('') @@ -186,7 +186,7 @@ class ChatBot(ChatBotFrame): self.pro_func_prompt, self.pro_fp_state]) self.pro_reuse_btn.click( fn=func_box.reuse_chat, - inputs=[self.pro_results, self.chatbot, self.history, self.pro_name_txt], + inputs=[self.pro_results, self.chatbot, self.history, self.pro_name_txt, self.txt], outputs=[self.chatbot, self.history, self.txt, self.tabs_chatbot, self.pro_name_txt, self.examples_column] ) @@ -312,7 +312,7 @@ class ChatBot(ChatBotFrame): self.cancel_handles.append(self.txt.submit(**self.clear_agrs).then(**self.predict_args)) self.cancel_handles.append(self.submitBtn.click(**self.clear_agrs).then(**self.predict_args)) # self.cpopyBtn.click(fn=func_box.copy_result, inputs=[self.history], outputs=[self.status]) - self.resetBtn.click(lambda: ([], [], "已重置"), None, [self.chatbot, self.history, self.status]) + self.resetBtn.click(lambda: ([], [], "已重置"), None, [self.chatbot, self.history, self.status], _js='()=>{clearHistoryHtml();}') def signals_function(self): # 基础功能区的回调函数注册 diff --git a/docs/assets/custom.js b/docs/assets/custom.js index 84e0068..3a0a889 100644 --- a/docs/assets/custom.js +++ b/docs/assets/custom.js @@ -58,8 +58,8 @@ function gradioLoaded(mutations) { chat_txt = document.getElementById('chat_txt'); userInfoDiv = document.getElementById("user_info"); appTitleDiv = document.getElementById("app_title"); - chatbot = document.querySelector('#main_chatbot'); - chatbotWrap = document.querySelector('#main_chatbot > .wrap'); + chatbot = document.querySelector('#废弃'); + chatbotWrap = document.querySelector('#废弃 > .wrap'); apSwitch = document.querySelector('.apSwitch input[type="checkbox"]'); if (loginUserForm) { @@ -414,7 +414,7 @@ var mObserver = new MutationObserver(function (mutationsList) { for (var node of mmutation.addedNodes) { if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') { saveHistoryHtml(); - document.querySelectorAll('#main_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); + document.querySelectorAll('#废弃>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); } if (node.tagName === 'INPUT' && node.getAttribute('type') === 'range') { setSlider(); @@ -423,7 +423,7 @@ var mObserver = new MutationObserver(function (mutationsList) { for (var node of mmutation.removedNodes) { if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') { saveHistoryHtml(); - document.querySelectorAll('#main_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); + document.querySelectorAll('#废弃>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); } } } else if (mmutation.type === 'attributes') { @@ -433,7 +433,7 @@ var mObserver = new MutationObserver(function (mutationsList) { clearTimeout(timeoutId); timeoutId = setTimeout(() => { isThrottled = false; - document.querySelectorAll('#main_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); + document.querySelectorAll('#废弃>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); saveHistoryHtml(); }, 500); } @@ -442,59 +442,6 @@ var mObserver = new MutationObserver(function (mutationsList) { }); mObserver.observe(document.documentElement, { attributes: true, childList: true, subtree: true }); -var loadhistorytime = 0; // for debugging -function saveHistoryHtml() { - var historyHtml = document.querySelector('#main_chatbot > .wrap'); - localStorage.setItem('chatHistory', historyHtml.innerHTML); - // console.log("History Saved") - historyLoaded = false; -} -function loadHistoryHtml() { - var historyHtml = localStorage.getItem('chatHistory'); - if (!historyHtml) { - historyLoaded = true; - return; // no history, do nothing - } - userLogged = localStorage.getItem('userLogged'); - if (userLogged){ - historyLoaded = true; - return; // logged in, do nothing - } - if (!historyLoaded) { - var tempDiv = document.createElement('div'); - tempDiv.innerHTML = historyHtml; - var buttons = tempDiv.querySelectorAll('button.chuanhu-btn'); - var gradioCopyButtons = tempDiv.querySelectorAll('button.copy_code_button'); - for (var i = 0; i < buttons.length; i++) { - buttons[i].parentNode.removeChild(buttons[i]); - } - for (var i = 0; i < gradioCopyButtons.length; i++) { - gradioCopyButtons[i].parentNode.removeChild(gradioCopyButtons[i]); - } - var fakeHistory = document.createElement('div'); - fakeHistory.classList.add('history-message'); - fakeHistory.innerHTML = tempDiv.innerHTML; - webLocale(); - chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild); - // var fakeHistory = document.createElement('div'); - // fakeHistory.classList.add('history-message'); - // fakeHistory.innerHTML = historyHtml; - // chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild); - historyLoaded = true; - console.log("History Loaded"); - loadhistorytime += 1; // for debugging - } else { - historyLoaded = false; - } -} -function clearHistoryHtml() { - localStorage.removeItem("chatHistory"); - historyMessages = chatbotWrap.querySelector('.history-message'); - if (historyMessages) { - chatbotWrap.removeChild(historyMessages); - console.log("History Cleared"); - } -} // 监视页面内部 DOM 变动 var observer = new MutationObserver(function (mutations) { diff --git a/func_box.py b/func_box.py index 6113e28..a6df439 100644 --- a/func_box.py +++ b/func_box.py @@ -32,6 +32,7 @@ import random import gradio as gr import toolbox from prompt_generator import SqliteHandle +from bs4 import BeautifulSoup """contextlib 是 Python 标准库中的一个模块,提供了一些工具函数和装饰器,用于支持编写上下文管理器和处理上下文的常见任务,例如资源管理、异常处理等。 官网:https://docs.python.org/3/library/contextlib.html""" @@ -500,19 +501,22 @@ def show_prompt_result(index, data: gr.Dataset, chatbot): return chatbot -pattern_markdown = re.compile(r'^
|<\/p><\/div>$') -pattern_markdown_p = re.compile(r'^