合并川虎js, 舍弃他的对话历史逻辑
This commit is contained in:
@ -79,7 +79,7 @@ class ChatBot(ChatBotFrame):
|
|||||||
with gr.Box(elem_id='chat_box'):
|
with gr.Box(elem_id='chat_box'):
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
gr.Button(elem_classes='sm_btn').style(size='sm', full_width=False)
|
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():
|
with gr.Row():
|
||||||
self.txt = gr.Textbox(show_label=False, placeholder="Input question here.", elem_classes='chat_input').style(container=False)
|
self.txt = gr.Textbox(show_label=False, placeholder="Input question here.", elem_classes='chat_input').style(container=False)
|
||||||
self.input_copy = gr.State('')
|
self.input_copy = gr.State('')
|
||||||
@ -186,7 +186,7 @@ class ChatBot(ChatBotFrame):
|
|||||||
self.pro_func_prompt, self.pro_fp_state])
|
self.pro_func_prompt, self.pro_fp_state])
|
||||||
self.pro_reuse_btn.click(
|
self.pro_reuse_btn.click(
|
||||||
fn=func_box.reuse_chat,
|
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]
|
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.txt.submit(**self.clear_agrs).then(**self.predict_args))
|
||||||
self.cancel_handles.append(self.submitBtn.click(**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.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):
|
def signals_function(self):
|
||||||
# 基础功能区的回调函数注册
|
# 基础功能区的回调函数注册
|
||||||
|
|||||||
@ -58,8 +58,8 @@ function gradioLoaded(mutations) {
|
|||||||
chat_txt = document.getElementById('chat_txt');
|
chat_txt = document.getElementById('chat_txt');
|
||||||
userInfoDiv = document.getElementById("user_info");
|
userInfoDiv = document.getElementById("user_info");
|
||||||
appTitleDiv = document.getElementById("app_title");
|
appTitleDiv = document.getElementById("app_title");
|
||||||
chatbot = document.querySelector('#main_chatbot');
|
chatbot = document.querySelector('#废弃');
|
||||||
chatbotWrap = document.querySelector('#main_chatbot > .wrap');
|
chatbotWrap = document.querySelector('#废弃 > .wrap');
|
||||||
apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
|
apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
|
||||||
|
|
||||||
if (loginUserForm) {
|
if (loginUserForm) {
|
||||||
@ -414,7 +414,7 @@ var mObserver = new MutationObserver(function (mutationsList) {
|
|||||||
for (var node of mmutation.addedNodes) {
|
for (var node of mmutation.addedNodes) {
|
||||||
if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') {
|
if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') {
|
||||||
saveHistoryHtml();
|
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') {
|
if (node.tagName === 'INPUT' && node.getAttribute('type') === 'range') {
|
||||||
setSlider();
|
setSlider();
|
||||||
@ -423,7 +423,7 @@ var mObserver = new MutationObserver(function (mutationsList) {
|
|||||||
for (var node of mmutation.removedNodes) {
|
for (var node of mmutation.removedNodes) {
|
||||||
if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') {
|
if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') {
|
||||||
saveHistoryHtml();
|
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') {
|
} else if (mmutation.type === 'attributes') {
|
||||||
@ -433,7 +433,7 @@ var mObserver = new MutationObserver(function (mutationsList) {
|
|||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
timeoutId = setTimeout(() => {
|
timeoutId = setTimeout(() => {
|
||||||
isThrottled = false;
|
isThrottled = false;
|
||||||
document.querySelectorAll('#main_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton);
|
document.querySelectorAll('#废弃>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton);
|
||||||
saveHistoryHtml();
|
saveHistoryHtml();
|
||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
@ -442,59 +442,6 @@ var mObserver = new MutationObserver(function (mutationsList) {
|
|||||||
});
|
});
|
||||||
mObserver.observe(document.documentElement, { attributes: true, childList: true, subtree: true });
|
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 变动
|
// 监视页面内部 DOM 变动
|
||||||
var observer = new MutationObserver(function (mutations) {
|
var observer = new MutationObserver(function (mutations) {
|
||||||
|
|||||||
27
func_box.py
27
func_box.py
@ -32,6 +32,7 @@ import random
|
|||||||
import gradio as gr
|
import gradio as gr
|
||||||
import toolbox
|
import toolbox
|
||||||
from prompt_generator import SqliteHandle
|
from prompt_generator import SqliteHandle
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
"""contextlib 是 Python 标准库中的一个模块,提供了一些工具函数和装饰器,用于支持编写上下文管理器和处理上下文的常见任务,例如资源管理、异常处理等。
|
"""contextlib 是 Python 标准库中的一个模块,提供了一些工具函数和装饰器,用于支持编写上下文管理器和处理上下文的常见任务,例如资源管理、异常处理等。
|
||||||
官网:https://docs.python.org/3/library/contextlib.html"""
|
官网:https://docs.python.org/3/library/contextlib.html"""
|
||||||
@ -500,19 +501,22 @@ def show_prompt_result(index, data: gr.Dataset, chatbot):
|
|||||||
return chatbot
|
return chatbot
|
||||||
|
|
||||||
|
|
||||||
pattern_markdown = re.compile(r'^<div class="markdown-body"><p>|<\/p><\/div>$')
|
|
||||||
pattern_markdown_p = re.compile(r'^<div class="markdown-body">|<\/div>$')
|
def pattern_html(html):
|
||||||
|
bs = BeautifulSoup(html, 'html.parser')
|
||||||
|
return bs.get_text(separator='')
|
||||||
|
|
||||||
def thread_write_chat(chatbot, history):
|
def thread_write_chat(chatbot, history):
|
||||||
"""
|
"""
|
||||||
对话记录写入数据库
|
对话记录写入数据库
|
||||||
"""
|
"""
|
||||||
private_key = toolbox.get_conf('private_key')[0]
|
private_key = toolbox.get_conf('private_key')[0]
|
||||||
chat_title = chatbot[0][1].split()
|
chat_title = chatbot[0][1].split()
|
||||||
i_say = pattern_markdown.sub('', chatbot[-1][0])
|
i_say = pattern_html(chatbot[-1][0])
|
||||||
if history:
|
if history:
|
||||||
gpt_result = history
|
gpt_result = history
|
||||||
else: # 如果历史对话不存在,那么读取对话框
|
else: # 如果历史对话不存在,那么读取对话框
|
||||||
gpt_result = [pattern_markdown.sub('', v) for i in chatbot for v in i]
|
gpt_result = [pattern_html(v) for i in chatbot for v in i]
|
||||||
if private_key in chat_title:
|
if private_key in chat_title:
|
||||||
SqliteHandle(f'ai_private_{chat_title[-2]}').inset_prompt({i_say: gpt_result})
|
SqliteHandle(f'ai_private_{chat_title[-2]}').inset_prompt({i_say: gpt_result})
|
||||||
else:
|
else:
|
||||||
@ -522,20 +526,19 @@ def thread_write_chat(chatbot, history):
|
|||||||
base_path = os.path.dirname(__file__)
|
base_path = os.path.dirname(__file__)
|
||||||
prompt_path = os.path.join(base_path, 'prompt_users')
|
prompt_path = os.path.join(base_path, 'prompt_users')
|
||||||
|
|
||||||
def reuse_chat(result, chatbot, history, pro_numb):
|
def reuse_chat(result, chatbot, history, pro_numb, say):
|
||||||
"""复用对话记录"""
|
"""复用对话记录"""
|
||||||
if result is None or result == []:
|
if result is None or result == []:
|
||||||
return chatbot, history, gr.update(), gr.update(), '', gr.Column.update()
|
return chatbot, history, gr.update(), gr.update(), '', gr.Column.update()
|
||||||
else:
|
else:
|
||||||
if pro_numb:
|
if pro_numb:
|
||||||
chatbot += result
|
chatbot += result
|
||||||
history += [pattern_markdown.sub('', _) for i in result for _ in i]
|
history += [pattern_html(_) for i in result for _ in i]
|
||||||
else:
|
else:
|
||||||
chatbot.append(result[-1])
|
chatbot.append(result[-1])
|
||||||
history += [pattern_markdown.sub('', _) for i in result[-2:] for _ in i]
|
history += [pattern_html(_) for i in result[-2:] for _ in i]
|
||||||
print(chatbot[-1][0])
|
print(chatbot[-1][0])
|
||||||
i_say = pattern_markdown.sub('', chatbot[-1][0])
|
return chatbot, history, say, gr.Tabs.update(selected='chatbot'), '', gr.Column.update(visible=False)
|
||||||
return chatbot, history, i_say, gr.Tabs.update(selected='chatbot'), '', gr.Column.update(visible=False)
|
|
||||||
|
|
||||||
|
|
||||||
def num_tokens_from_string(listing: list, encoding_name: str = 'cl100k_base') -> int:
|
def num_tokens_from_string(listing: list, encoding_name: str = 'cl100k_base') -> int:
|
||||||
@ -551,10 +554,8 @@ def spinner_chatbot_loading(chatbot):
|
|||||||
# 将元组转换为列表并修改元素
|
# 将元组转换为列表并修改元素
|
||||||
loading_msg = copy.deepcopy(chatbot)
|
loading_msg = copy.deepcopy(chatbot)
|
||||||
temp_list = list(loading_msg[-1])
|
temp_list = list(loading_msg[-1])
|
||||||
if pattern_markdown.match(temp_list[1]):
|
|
||||||
temp_list[1] = pattern_markdown.sub('', temp_list[1]) + f'{random.choice(loading)}'
|
temp_list[1] = pattern_html(temp_list[1]) + f'{random.choice(loading)}'
|
||||||
else:
|
|
||||||
temp_list[1] = pattern_markdown_p.sub('', temp_list[1]) + f'{random.choice(loading)}'
|
|
||||||
# 将列表转换回元组并替换原始元组
|
# 将列表转换回元组并替换原始元组
|
||||||
loading_msg[-1] = tuple(temp_list)
|
loading_msg[-1] = tuple(temp_list)
|
||||||
return loading_msg
|
return loading_msg
|
||||||
|
|||||||
11
toolbox.py
11
toolbox.py
@ -1,3 +1,5 @@
|
|||||||
|
import html
|
||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
@ -291,8 +293,9 @@ def markdown_convertion(txt):
|
|||||||
"""
|
"""
|
||||||
将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。
|
将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。
|
||||||
"""
|
"""
|
||||||
pre = '<div class="markdown-body">'
|
pre = '<div class="md-message">'
|
||||||
suf = '</div>'
|
suf = '</div>'
|
||||||
|
raw_hide = f'<div class="raw-message hideM">%s</div>'
|
||||||
if txt.startswith(pre) and txt.endswith(suf):
|
if txt.startswith(pre) and txt.endswith(suf):
|
||||||
# print('警告,输入了已经经过转化的字符串,二次转化可能出问题')
|
# print('警告,输入了已经经过转化的字符串,二次转化可能出问题')
|
||||||
return txt # 已经被转化过,不需要再次转化
|
return txt # 已经被转化过,不需要再次转化
|
||||||
@ -363,9 +366,11 @@ def markdown_convertion(txt):
|
|||||||
# 2. convert to rendered equation
|
# 2. convert to rendered equation
|
||||||
convert_stage_2_2, n = re.subn(find_equation_pattern, replace_math_render, convert_stage_1, flags=re.DOTALL)
|
convert_stage_2_2, n = re.subn(find_equation_pattern, replace_math_render, convert_stage_1, flags=re.DOTALL)
|
||||||
# cat them together
|
# cat them together
|
||||||
return pre + convert_stage_2_1 + f'{split}' + convert_stage_2_2 + suf
|
context = convert_stage_2_1 + f'{split}' + convert_stage_2_2
|
||||||
|
return raw_hide.replace('%s', func_box.pattern_html(context)) + pre + context + suf
|
||||||
else:
|
else:
|
||||||
return pre + markdown.markdown(txt, extensions=['fenced_code', 'codehilite', 'tables', 'sane_lists']) + suf
|
context = markdown.markdown(txt, extensions=['fenced_code', 'codehilite', 'tables', 'sane_lists'])
|
||||||
|
return raw_hide.replace('%s', func_box.pattern_html(context)) + pre + context + suf
|
||||||
|
|
||||||
|
|
||||||
def close_up_code_segment_during_stream(gpt_reply):
|
def close_up_code_segment_during_stream(gpt_reply):
|
||||||
|
|||||||
Reference in New Issue
Block a user