diff --git a/__main__.py b/__main__.py index 0bd14c4..3f08c3f 100644 --- a/__main__.py +++ b/__main__.py @@ -2,7 +2,7 @@ import os import gradio as gr from request_llm.bridge_all import predict from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_user_upload, \ - get_user_download, get_conf, ArgsGeneralWrapper, DummyWith + get_conf, ArgsGeneralWrapper, DummyWith # 问询记录, python 版本建议3.9+(越新越好) import logging @@ -70,29 +70,34 @@ class ChatBot(ChatBotFrame): def draw_title(self): # self.title = gr.HTML(self.title_html) self.cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL, 'local': self.__url}) - def draw_chatbot(self): self.chatbot = gr.Chatbot(elem_id='main_chatbot', label=f"当前模型:{LLM_MODEL}") self.chatbot.style() self.history = gr.State([]) temp_draw = [gr.HTML() for i in range(7)] with gr.Box(elem_id='chat_box'): + self.state_users = gr.HTML(value='', visible=False, elem_id='state_users') with gr.Row(): - self.sm_upload = gr.UploadButton(label='🔗Upload', file_count='multiple', elem_classes='sm_btn').style(size='sm', full_width=False) - self.sm_code_block = gr.Button(value='⚛︎Code', elem_classes='sm_btn').style(size='sm', full_width=False) + self.sm_upload = gr.UploadButton(label='UPLOAD', file_count='multiple', elem_classes='sm_btn').style(size='sm', full_width=False) + self.sm_code_block = gr.Button(value='CODE', elem_classes='sm_btn').style(size='sm', full_width=False) gr.HTML(func_box.get_html("appearance_switcher.html").format(label=""), elem_id='user_input_tb', elem_classes="insert_block") + with gr.Column(scale=100): + self.md_dropdown = gr.Dropdown(choices=AVAIL_LLM_MODELS, + value=LLM_MODEL, show_label=False, interactive=True, + elem_classes='sm_select', elem_id='change-font-size').style(container=False) 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('') self.submitBtn = gr.Button("", variant="primary", elem_classes='submit_btn').style(full_width=False) with gr.Row(): - self.status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}", elem_id='debug_mes') + self.status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行\n {proxy_info}", elem_id='debug_mes') def signals_sm_btn(self): self.sm_upload.upload(on_file_uploaded, [self.sm_upload, self.chatbot, self.txt], [self.chatbot, self.txt]).then( fn=lambda: [gr.Tabs.update(selected='plug_tab'), gr.Column.update(visible=False)], inputs=None, outputs=[self.tabs_inputs, self.examples_column] ) - self.sm_code_block.click(fn=lambda x: x+'\n```\n\n```', inputs=[self.txt], outputs=[self.txt]) + self.sm_code_block.click(fn=lambda x: x+'```\n\n```', inputs=[self.txt], outputs=[self.txt]) + # self.sm_select_font.select(fn=lambda x: gr.HTML.update(value=f"{x}px"), inputs=[self.sm_select_font], outputs=[self.state_users]) def draw_examples(self): with gr.Column(elem_id='examples_col') as self.examples_column: @@ -185,12 +190,12 @@ class ChatBot(ChatBotFrame): self.pro_private_check], outputs=[self.pro_prompt_list, self.pro_prompt_state]) self.pro_prompt_list.click(fn=func_box.show_prompt_result, - inputs=[self.pro_prompt_list, self.pro_prompt_state, self.pro_results], - outputs=[self.pro_results]) + inputs=[self.pro_prompt_list, self.pro_prompt_state, self.pro_results, self.pro_edit_txt, self.pro_name_txt], + outputs=[self.pro_results, self.pro_edit_txt, self.pro_name_txt]) self.pro_new_btn.click(fn=func_box.prompt_save, inputs=[self.pro_edit_txt, self.pro_name_txt, self.pro_fp_state], outputs=[self.pro_edit_txt, self.pro_name_txt, self.pro_private_check, - self.pro_func_prompt, self.pro_fp_state]) + self.pro_func_prompt, self.pro_fp_state, self.tabs_chatbot]) self.pro_reuse_btn.click( fn=func_box.reuse_chat, inputs=[self.pro_results, self.chatbot, self.history, self.pro_name_txt, self.txt], @@ -260,7 +265,6 @@ class ChatBot(ChatBotFrame): placeholder="这里是特殊函数插件的高级参数输入区").style(container=False) self.switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary") - def draw_setting_chat(self): switch_model = get_conf('switch_model')[0] with gr.TabItem('Settings', id='sett_tab'): @@ -272,12 +276,9 @@ class ChatBot(ChatBotFrame): label="MaxLength", ).style(container=False) self.pro_tf_slider = gr.Slider(minimum=0.01, maximum=1.0, value=0.70, step=0.01, interactive=True, label="Term Frequency系数").style(container=False) - self.models_box = gr.CheckboxGroup(choices=switch_model['key'], value=switch_model['value'], - label="对话模式") + self.models_box = gr.CheckboxGroup(choices=switch_model['key'], value=switch_model['value'], label="对话模式") self.system_prompt = gr.Textbox(show_label=True, lines=2, placeholder=f"System Prompt", label="System prompt", value=self.initial_prompt) - self.md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style( - container=False) # temp = gr.Markdown(self.description) def draw_goals_auto(self): @@ -319,7 +320,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], _js='()=>{clearHistoryHtml();}') + self.resetBtn.click(lambda: ([], [], "已重置"), None, [self.chatbot, self.history, self.status]) def signals_function(self): # 基础功能区的回调函数注册 @@ -339,7 +340,9 @@ class ChatBot(ChatBotFrame): for k in crazy_fns: if not crazy_fns[k].get("AsButton", True): continue self.click_handle = crazy_fns[k]["Button"].click(**self.clear_agrs).then( - ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*self.input_combo, gr.State(PORT)], self.output_combo) + ArgsGeneralWrapper(crazy_fns[k]["Function"]), + [*self.input_combo, gr.State(PORT), gr.State(crazy_fns[k].get('Parameters', False))], + self.output_combo) self.click_handle.then(on_report_generated, [self.cookies, self.file_upload, self.chatbot], [self.cookies, self.file_upload, self.chatbot]) # self.click_handle.then(fn=lambda x: '', inputs=[], outputs=self.txt) @@ -373,7 +376,8 @@ class ChatBot(ChatBotFrame): yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs) self.click_handle = self.switchy_bt.click(**self.clear_agrs).then(route, [self.switchy_bt, *self.input_combo, gr.State(PORT)], self.output_combo) - self.click_handle.then(on_report_generated, [self.cookies, self.file_upload, self.chatbot], [self.file_upload, self.chatbot]) + self.click_handle.then(on_report_generated, [self.cookies, self.file_upload, self.chatbot], + [self.cookies, self.file_upload, self.chatbot]) self.cancel_handles.append(self.click_handle) # 终止按钮的回调函数注册 self.stopBtn.click(fn=None, inputs=None, outputs=None, cancels=self.cancel_handles) diff --git a/toolbox.py b/toolbox.py index 262c67a..7cecaa0 100644 --- a/toolbox.py +++ b/toolbox.py @@ -1,5 +1,4 @@ import html - import markdown import importlib import inspect @@ -12,7 +11,7 @@ import os import time import glob import sys -from concurrent.futures import ThreadPoolExecutor +import threading ############################### 插件输入输出接驳区 ####################################### pj = os.path.join @@ -70,8 +69,11 @@ def ArgsGeneralWrapper(f): 'start_time': start_time } plugin_kwargs = { - "advanced_arg": plugin_advanced_arg + "advanced_arg": plugin_advanced_arg, + "parameters_def": '' } + if len(args) > 1: + plugin_kwargs.update({'parameters_def': args[1]}) transparent_address_private = f'

\n{private_key}\n{ipaddr.client.host}\n

' transparent_address = f'

\n{ipaddr.client.host}\n

' if private in models: @@ -88,23 +90,19 @@ def ArgsGeneralWrapper(f): chatbot_with_cookie.write_list(chatbot) txt_passon = txt if encrypt in models: txt_passon = func_box.encryption_str(txt) - if txt_passon == '' and len(args) > 1: - msgs = f'### Warning 输入框为空\n' \ - f'tips: 使用基础功能或{func_box.html_tag_color("高亮插件", "#b522c5", "ffffff")}功能时,请在输入区输入需要处理的内容' - yield from update_ui(chatbot=chatbot_with_cookie, history=history, msg=msgs) # 刷新界面 - return yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args) return decorated -pool = ThreadPoolExecutor(200) + def update_ui(chatbot, history, msg='正常', *args): # 刷新界面 """ 刷新用户界面 """ assert isinstance(chatbot, ChatBotWithCookies), "在传递chatbot的过程中不要将其丢弃。必要时,可用clear将其清空,然后用for+append循环重新赋值。" yield chatbot.get_cookies(), chatbot, history, msg - pool.submit(func_box.thread_write_chat, chatbot, history) + threading.Thread(target=func_box.thread_write_chat, args=(chatbot, history)).start() + # func_box.thread_write_chat(chatbot, history) def update_ui_lastest_msg(lastmsg, chatbot, history, delay=1): # 刷新界面 """ @@ -307,13 +305,6 @@ def markdown_convertion(txt): } find_equation_pattern = r'', '') return content - def no_code(txt): if '```' not in txt: return True @@ -353,23 +343,24 @@ def markdown_convertion(txt): else: return False - if ('$' in txt) and no_code(txt): # 有$标识的公式符号,且没有代码段```的标识 + if ('$$' in txt) and no_code(txt): # 有$标识的公式符号,且没有代码段```的标识 # convert everything to html format split = markdown.markdown(text='---') - convert_stage_1 = markdown.markdown(text=txt, extensions=['mdx_math', 'fenced_code', 'tables', 'sane_lists'], - extension_configs=markdown_extension_configs) + txt = re.sub(r'\$\$((?:.|\n)*?)\$\$', lambda match: '$$' + re.sub(r'\n+', '
', match.group(1)) + '$$', txt) + convert_stage_1 = markdown.markdown(text=txt, extensions=['mdx_math', 'fenced_code', 'tables', 'sane_lists'], extension_configs=markdown_extension_configs) convert_stage_1 = markdown_bug_hunt(convert_stage_1) # re.DOTALL: Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline. Corresponds to the inline flag (?s). # 1. convert to easy-to-copy tex (do not render math) convert_stage_2_1, n = re.subn(find_equation_pattern, replace_math_no_render, convert_stage_1, flags=re.DOTALL) # 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_1_resp = convert_stage_1.replace('
', '') + convert_stage_2_2, n = re.subn(find_equation_pattern, replace_math_render, convert_stage_1_resp, flags=re.DOTALL) # cat them together - context = convert_stage_2_1 + f'{split}' + convert_stage_2_2 - return raw_hide.replace('%s', func_box.pattern_html(context)) + pre + context + suf + context = pre + convert_stage_2_1 + f'{split}' + convert_stage_2_2 + suf + return raw_hide.replace('%s', func_box.pattern_html(context)) + context else: - context = markdown.markdown(txt, extensions=['fenced_code', 'codehilite', 'tables', 'sane_lists']) - return raw_hide.replace('%s', func_box.pattern_html(context)) + pre + context + suf + context = pre + markdown.markdown(txt, extensions=['fenced_code', 'codehilite', 'tables', 'sane_lists']) + suf + return raw_hide.replace('%s', func_box.pattern_html(context)) + context def close_up_code_segment_during_stream(gpt_reply): @@ -517,7 +508,9 @@ def get_user_upload(chatbot, ipaddr: gr.Request): """ private_upload = './private_upload' user_history = os.path.join(private_upload, ipaddr.client.host) - history = '' + history = """ + | 编号 | 字段2 | 字段2 | + """ for root, d, file in os.walk(user_history): history += f'目录:\t {root} \t\t 目录内文件: {file}\n\n' chatbot.append(['Loading....',