Add parsing arbitrary code items

This commit is contained in:
mrhblfx 2023-04-16 23:00:45 +08:00
parent f0fbc65a36
commit 2f9a4e1618
4 changed files with 84 additions and 25 deletions

View File

@ -181,6 +181,14 @@ def get_crazy_functions():
print(f'[下载arxiv论文并翻译摘要] 插件导入失败 {str(err)}')
from crazy_functions.解析项目源代码 import 解析任意code项目
function_plugins.update({
"解析任意code项目": {
"Color": "stop",
"AsButton": False,
"Function": HotReload(解析任意code项目)
},
})
###################### 第n组插件 ###########################
return function_plugins

View File

@ -264,3 +264,34 @@ def 解析一个CSharp项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, s
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
return
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
@CatchException
def 解析任意code项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
txt_include = plugin_kwargs.get("txt_include")
txt_except = plugin_kwargs.get("txt_except")
# 将要匹配的后缀
pattern_include = [_.lstrip(" .,").rstrip(" ,") for _ in txt_include.split(" ") if _ != ""]
pattern_include = [_.lstrip(" .,").rstrip(" ,") for __ in pattern_include for _ in __.split(",") if _ != ""]
pattern_include = [_.lstrip(" .,").rstrip(" ,") for __ in pattern_include for _ in __.split("") if _ != ""]
# 将要忽略匹配的后缀
pattern_except = [_.lstrip(" .,").rstrip(" ,") for _ in txt_except.split(" ") if _ != ""]
pattern_except = [_.lstrip(" .,").rstrip(" ,") for __ in pattern_except for _ in __.split(",") if _ != ""]
pattern_except = [_.lstrip(" .,").rstrip(" ,") for __ in pattern_except for _ in __.split("") if _ != ""]
pattern_except += ['zip', 'rar', '7z', 'tar', 'gz'] # 避免解析上传的压缩文件
history = [] # 清空历史,以免输入溢出
import glob, os
if os.path.exists(txt):
project_folder = txt
else:
if txt == "": txt = '空空如也的输入栏'
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
return
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.*', recursive=True) if os.path.isfile(f) and \
([] == pattern_include or f.split(".")[-1] in pattern_include) and f.split(".")[-1] not in pattern_except]
if len(file_manifest) == 0:
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何文件: {txt}")
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
return
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)

21
main.py
View File

@ -83,9 +83,23 @@ with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=
variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
crazy_fns[k]["Button"].style(size="sm")
with gr.Row():
with gr.Accordion("解析任意code项目", open=True):
gr.Markdown("输入的文件后缀用空格或逗号隔开, 可混合使用空格逗号")
with gr.Row():
gr.Markdown("将要匹配文件的后缀, 不输入则代表解析所有文件")
txt_pattern_include = gr.Textbox(show_label=False, placeholder="例如: .c .cpp .py").style(container=False)
with gr.Row():
gr.Markdown("将要忽略匹配文件的后缀")
txt_pattern_except = gr.Textbox(show_label=False, placeholder="例如: .png, .jpg wav flac").style(container=False)
code_plugin_name = "解析任意code项目"
variant = crazy_fns[code_plugin_name]["Color"] if "Color" in crazy_fns[code_plugin_name] else "secondary"
crazy_fns[code_plugin_name]["Button"] = gr.Button(code_plugin_name, variant=variant)
crazy_fns[code_plugin_name]["Button"].style(size="sm")
with gr.Row():
with gr.Accordion("更多函数插件", open=True):
dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
dropdown_fn_list.remove(code_plugin_name)
with gr.Column(scale=1):
dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False)
with gr.Column(scale=1):
@ -118,7 +132,8 @@ with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=
return ret
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] )
# 整理反复出现的控件句柄组合
input_combo = [cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt]
add_input_combo = (txt_pattern_include, txt_pattern_except)
input_combo = [cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *add_input_combo]
output_combo = [cookies, chatbot, history, status]
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
# 提交按钮、重置按钮
@ -140,6 +155,10 @@ with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=
click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
cancel_handles.append(click_handle)
# 函数插件-解析任意code项目
click_handle = crazy_fns[code_plugin_name]["Button"].click(ArgsGeneralWrapper(crazy_fns[code_plugin_name]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
cancel_handles.append(click_handle)
# 函数插件-下拉菜单与随变按钮的互动
def on_dropdown_changed(k):
variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"

View File

@ -27,7 +27,7 @@ def ArgsGeneralWrapper(f):
"""
装饰器函数用于重组输入参数改变输入参数的顺序与结构
"""
def decorated(cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *args):
def decorated(cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt, txt_include, txt_except, *args):
txt_passon = txt
if txt == "" and txt2 != "": txt_passon = txt2
# 引入一个有cookie的chatbot
@ -41,9 +41,10 @@ def ArgsGeneralWrapper(f):
'top_p':top_p,
'temperature':temperature,
}
plugin_kwargs = {
# 目前还没有
}
# plugin_kwargs = {
# # 目前还没有
# }
plugin_kwargs = dict(txt_include = txt_include, txt_except = txt_except)
chatbot_with_cookie = ChatBotWithCookies(cookies)
chatbot_with_cookie.write_list(chatbot)
yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)