Spaces:
Sleeping
Sleeping
Merge pull request #502 from mrhblfx/new_code_fun
Browse files- crazy_functional.py +13 -2
- crazy_functions/解析项目源代码.py +50 -9
- main.py +19 -10
- toolbox.py +8 -8
crazy_functional.py
CHANGED
|
@@ -174,7 +174,7 @@ def get_crazy_functions():
|
|
| 174 |
"AsButton": False, # 加入下拉菜单中
|
| 175 |
"Function": HotReload(Markdown英译中)
|
| 176 |
},
|
| 177 |
-
|
| 178 |
})
|
| 179 |
|
| 180 |
###################### 第三组插件 ###########################
|
|
@@ -187,7 +187,7 @@ def get_crazy_functions():
|
|
| 187 |
"Function": HotReload(下载arxiv论文并翻译摘要)
|
| 188 |
}
|
| 189 |
})
|
| 190 |
-
|
| 191 |
from crazy_functions.联网的ChatGPT import 连接网络回答问题
|
| 192 |
function_plugins.update({
|
| 193 |
"连接网络回答问题(先输入问题,再点击按钮,需要访问谷歌)": {
|
|
@@ -197,5 +197,16 @@ def get_crazy_functions():
|
|
| 197 |
}
|
| 198 |
})
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
###################### 第n组插件 ###########################
|
| 201 |
return function_plugins
|
|
|
|
| 174 |
"AsButton": False, # 加入下拉菜单中
|
| 175 |
"Function": HotReload(Markdown英译中)
|
| 176 |
},
|
| 177 |
+
|
| 178 |
})
|
| 179 |
|
| 180 |
###################### 第三组插件 ###########################
|
|
|
|
| 187 |
"Function": HotReload(下载arxiv论文并翻译摘要)
|
| 188 |
}
|
| 189 |
})
|
| 190 |
+
|
| 191 |
from crazy_functions.联网的ChatGPT import 连接网络回答问题
|
| 192 |
function_plugins.update({
|
| 193 |
"连接网络回答问题(先输入问题,再点击按钮,需要访问谷歌)": {
|
|
|
|
| 197 |
}
|
| 198 |
})
|
| 199 |
|
| 200 |
+
from crazy_functions.解析项目源代码 import 解析任意code项目
|
| 201 |
+
function_plugins.update({
|
| 202 |
+
"解析项目源代码(手动指定和筛选源代码文件类型)": {
|
| 203 |
+
"Color": "stop",
|
| 204 |
+
"AsButton": False,
|
| 205 |
+
"AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False)
|
| 206 |
+
"ArgsReminder": "输入时用逗号隔开, *代表通配符, 加了^代表不匹配; 不输入代表全部匹配。例如: \"*.c, ^*.cpp, config.toml, ^*.toml\"", # 高级参数输入区的显示提示
|
| 207 |
+
"Function": HotReload(解析任意code项目)
|
| 208 |
+
},
|
| 209 |
+
})
|
| 210 |
+
|
| 211 |
###################### 第n组插件 ###########################
|
| 212 |
return function_plugins
|
crazy_functions/解析项目源代码.py
CHANGED
|
@@ -11,7 +11,7 @@ def 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs,
|
|
| 11 |
history_array = []
|
| 12 |
sys_prompt_array = []
|
| 13 |
report_part_1 = []
|
| 14 |
-
|
| 15 |
assert len(file_manifest) <= 512, "源文件太多(超过512个), 请缩减输入文件的数量。或者,您也可以选择删除此行警告,并修改代码拆分file_manifest列表,从而实现分批次处理。"
|
| 16 |
############################## <第一步,逐个文件分析,多线程> ##################################
|
| 17 |
for index, fp in enumerate(file_manifest):
|
|
@@ -63,10 +63,10 @@ def 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs,
|
|
| 63 |
current_iteration_focus = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(this_iteration_file_manifest)])
|
| 64 |
i_say = f'根据以上分析,对程序的整体功能和构架重新做出概括。然后用一张markdown表格整理每个文件的功能(包括{previous_iteration_files_string})。'
|
| 65 |
inputs_show_user = f'根据以上分析,对程序的整体功能和构架重新做出概括,由于输入长度限制,可能需要分组处理,本组文件为 {current_iteration_focus} + 已经汇总的文件组。'
|
| 66 |
-
this_iteration_history = copy.deepcopy(this_iteration_gpt_response_collection)
|
| 67 |
this_iteration_history.append(last_iteration_result)
|
| 68 |
result = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 69 |
-
inputs=i_say, inputs_show_user=inputs_show_user, llm_kwargs=llm_kwargs, chatbot=chatbot,
|
| 70 |
history=this_iteration_history, # 迭代之前的分析
|
| 71 |
sys_prompt="你是一个程序架构分析师,正在分析一个项目的源代码。")
|
| 72 |
report_part_2.extend([i_say, result])
|
|
@@ -222,8 +222,8 @@ def 解析一个Golang项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, s
|
|
| 222 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 223 |
return
|
| 224 |
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 225 |
-
|
| 226 |
-
|
| 227 |
@CatchException
|
| 228 |
def 解析一个Lua项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 229 |
history = [] # 清空历史,以免输入溢出
|
|
@@ -243,9 +243,9 @@ def 解析一个Lua项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst
|
|
| 243 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何lua文件: {txt}")
|
| 244 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 245 |
return
|
| 246 |
-
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 247 |
-
|
| 248 |
-
|
| 249 |
@CatchException
|
| 250 |
def 解析一个CSharp项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 251 |
history = [] # 清空历史,以免输入溢出
|
|
@@ -263,4 +263,45 @@ def 解析一个CSharp项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, s
|
|
| 263 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何CSharp文件: {txt}")
|
| 264 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 265 |
return
|
| 266 |
-
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
history_array = []
|
| 12 |
sys_prompt_array = []
|
| 13 |
report_part_1 = []
|
| 14 |
+
|
| 15 |
assert len(file_manifest) <= 512, "源文件太多(超过512个), 请缩减输入文件的数量。或者,您也可以选择删除此行警告,并修改代码拆分file_manifest列表,从而实现分批次处理。"
|
| 16 |
############################## <第一步,逐个文件分析,多线程> ##################################
|
| 17 |
for index, fp in enumerate(file_manifest):
|
|
|
|
| 63 |
current_iteration_focus = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(this_iteration_file_manifest)])
|
| 64 |
i_say = f'根据以上分析,对程序的整体功能和构架重新做出概括。然后用一张markdown表格整理每个文件的功能(包括{previous_iteration_files_string})。'
|
| 65 |
inputs_show_user = f'根据以上分析,对程序的整体功能和构架重新做出概括,由于输入长度限制,可能需要分组处理,本组文件为 {current_iteration_focus} + 已经汇总的文件组。'
|
| 66 |
+
this_iteration_history = copy.deepcopy(this_iteration_gpt_response_collection)
|
| 67 |
this_iteration_history.append(last_iteration_result)
|
| 68 |
result = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 69 |
+
inputs=i_say, inputs_show_user=inputs_show_user, llm_kwargs=llm_kwargs, chatbot=chatbot,
|
| 70 |
history=this_iteration_history, # 迭代之前的分析
|
| 71 |
sys_prompt="你是一个程序架构分析师,正在分析一个项目的源代码。")
|
| 72 |
report_part_2.extend([i_say, result])
|
|
|
|
| 222 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 223 |
return
|
| 224 |
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 225 |
+
|
| 226 |
+
|
| 227 |
@CatchException
|
| 228 |
def 解析一个Lua项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 229 |
history = [] # 清空历史,以免输入溢出
|
|
|
|
| 243 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何lua文件: {txt}")
|
| 244 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 245 |
return
|
| 246 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 247 |
+
|
| 248 |
+
|
| 249 |
@CatchException
|
| 250 |
def 解析一个CSharp项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 251 |
history = [] # 清空历史,以免输入溢出
|
|
|
|
| 263 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何CSharp文件: {txt}")
|
| 264 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 265 |
return
|
| 266 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
@CatchException
|
| 270 |
+
def 解析任意code项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 271 |
+
txt_pattern = plugin_kwargs.get("advanced_arg")
|
| 272 |
+
txt_pattern = txt_pattern.replace(",", ",")
|
| 273 |
+
# 将要匹配的模式(例如: *.c, *.cpp, *.py, config.toml)
|
| 274 |
+
pattern_include = [_.lstrip(" ,").rstrip(" ,") for _ in txt_pattern.split(",") if _ != "" and not _.strip().startswith("^")]
|
| 275 |
+
if not pattern_include: pattern_include = ["*"] # 不输入即全部匹配
|
| 276 |
+
# 将要忽略匹配的文件后缀(例如: ^*.c, ^*.cpp, ^*.py)
|
| 277 |
+
pattern_except_suffix = [_.lstrip(" ^*.,").rstrip(" ,") for _ in txt_pattern.split(" ") if _ != "" and _.strip().startswith("^*.")]
|
| 278 |
+
pattern_except_suffix += ['zip', 'rar', '7z', 'tar', 'gz'] # 避免解析压缩文件
|
| 279 |
+
# 将要忽略匹配的文件名(例如: ^README.md)
|
| 280 |
+
pattern_except_name = [_.lstrip(" ^*,").rstrip(" ,").replace(".", "\.") for _ in txt_pattern.split(" ") if _ != "" and _.strip().startswith("^") and not _.strip().startswith("^*.")]
|
| 281 |
+
# 生成正则表达式
|
| 282 |
+
pattern_except = '/[^/]+\.(' + "|".join(pattern_except_suffix) + ')$'
|
| 283 |
+
pattern_except += '|/(' + "|".join(pattern_except_name) + ')$' if pattern_except_name != [] else ''
|
| 284 |
+
|
| 285 |
+
history.clear()
|
| 286 |
+
import glob, os, re
|
| 287 |
+
if os.path.exists(txt):
|
| 288 |
+
project_folder = txt
|
| 289 |
+
else:
|
| 290 |
+
if txt == "": txt = '空空如也的输入栏'
|
| 291 |
+
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 292 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 293 |
+
return
|
| 294 |
+
# 若上传压缩文件, 先寻找到解压的文件夹路径, 从而避免解析压缩文件
|
| 295 |
+
maybe_dir = [f for f in glob.glob(f'{project_folder}/*') if os.path.isdir(f)]
|
| 296 |
+
if len(maybe_dir)>0 and maybe_dir[0].endswith('.extract'):
|
| 297 |
+
extract_folder_path = maybe_dir[0]
|
| 298 |
+
else:
|
| 299 |
+
extract_folder_path = project_folder
|
| 300 |
+
# 按输入的匹配模式寻找上传的非压缩文件和已解压的文件
|
| 301 |
+
file_manifest = [f for pattern in pattern_include for f in glob.glob(f'{extract_folder_path}/**/{pattern}', recursive=True) if "" != extract_folder_path and \
|
| 302 |
+
os.path.isfile(f) and (not re.search(pattern_except, f) or pattern.endswith('.' + re.search(pattern_except, f).group().split('.')[-1]))]
|
| 303 |
+
if len(file_manifest) == 0:
|
| 304 |
+
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何文件: {txt}")
|
| 305 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 306 |
+
return
|
| 307 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
main.py
CHANGED
|
@@ -45,7 +45,7 @@ def main():
|
|
| 45 |
|
| 46 |
gr_L1 = lambda: gr.Row().style()
|
| 47 |
gr_L2 = lambda scale: gr.Column(scale=scale)
|
| 48 |
-
if LAYOUT == "TOP-DOWN":
|
| 49 |
gr_L1 = lambda: DummyWith()
|
| 50 |
gr_L2 = lambda scale: gr.Row()
|
| 51 |
CHATBOT_HEIGHT /= 2
|
|
@@ -88,9 +88,12 @@ def main():
|
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Accordion("更多函数插件", open=True):
|
| 90 |
dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
|
| 91 |
-
with gr.
|
| 92 |
dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False)
|
| 93 |
-
with gr.
|
|
|
|
|
|
|
|
|
|
| 94 |
switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary")
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
|
|
@@ -100,7 +103,7 @@ def main():
|
|
| 100 |
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
| 101 |
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
| 102 |
max_length_sl = gr.Slider(minimum=256, maximum=4096, value=512, step=1, interactive=True, label="Local LLM MaxLength",)
|
| 103 |
-
checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
|
| 104 |
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
|
| 105 |
|
| 106 |
gr.Markdown(description)
|
|
@@ -122,11 +125,12 @@ def main():
|
|
| 122 |
ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
|
| 123 |
ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
|
| 124 |
ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
|
|
|
|
| 125 |
if "底部输入区" in a: ret.update({txt: gr.update(value="")})
|
| 126 |
return ret
|
| 127 |
-
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2] )
|
| 128 |
# 整理反复出现的控件句柄组合
|
| 129 |
-
input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt]
|
| 130 |
output_combo = [cookies, chatbot, history, status]
|
| 131 |
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
|
| 132 |
# 提交按钮、重置按钮
|
|
@@ -153,14 +157,19 @@ def main():
|
|
| 153 |
# 函数插件-下拉菜单与随变按钮的互动
|
| 154 |
def on_dropdown_changed(k):
|
| 155 |
variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
def on_md_dropdown_changed(k):
|
| 159 |
return {chatbot: gr.update(label="当前模型:"+k)}
|
| 160 |
md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
|
| 161 |
# 随变按钮的回调函数注册
|
| 162 |
def route(k, *args, **kwargs):
|
| 163 |
-
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
|
| 164 |
yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs)
|
| 165 |
click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
|
| 166 |
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
|
|
@@ -178,7 +187,7 @@ def main():
|
|
| 178 |
print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
|
| 179 |
print(f"\t(亮色主题): http://localhost:{PORT}")
|
| 180 |
print(f"\t(暗色主题): http://localhost:{PORT}/?__dark-theme=true")
|
| 181 |
-
def open():
|
| 182 |
time.sleep(2) # 打开浏览器
|
| 183 |
webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true")
|
| 184 |
threading.Thread(target=open, name="open-browser", daemon=True).start()
|
|
|
|
| 45 |
|
| 46 |
gr_L1 = lambda: gr.Row().style()
|
| 47 |
gr_L2 = lambda scale: gr.Column(scale=scale)
|
| 48 |
+
if LAYOUT == "TOP-DOWN":
|
| 49 |
gr_L1 = lambda: DummyWith()
|
| 50 |
gr_L2 = lambda scale: gr.Row()
|
| 51 |
CHATBOT_HEIGHT /= 2
|
|
|
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Accordion("更多函数插件", open=True):
|
| 90 |
dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
|
| 91 |
+
with gr.Row():
|
| 92 |
dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False)
|
| 93 |
+
with gr.Row():
|
| 94 |
+
plugin_advanced_arg = gr.Textbox(show_label=True, label="高级参数输入区", visible=False,
|
| 95 |
+
placeholder="这里是特殊函数插件的高级参数输入区").style(container=False)
|
| 96 |
+
with gr.Row():
|
| 97 |
switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary")
|
| 98 |
with gr.Row():
|
| 99 |
with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
|
|
|
|
| 103 |
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
| 104 |
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
| 105 |
max_length_sl = gr.Slider(minimum=256, maximum=4096, value=512, step=1, interactive=True, label="Local LLM MaxLength",)
|
| 106 |
+
checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键", "插件参数区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
|
| 107 |
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
|
| 108 |
|
| 109 |
gr.Markdown(description)
|
|
|
|
| 125 |
ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
|
| 126 |
ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
|
| 127 |
ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
|
| 128 |
+
ret.update({plugin_advanced_arg: gr.update(visible=("插件参数区" in a))})
|
| 129 |
if "底部输入区" in a: ret.update({txt: gr.update(value="")})
|
| 130 |
return ret
|
| 131 |
+
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2, plugin_advanced_arg] )
|
| 132 |
# 整理反复出现的控件句柄组合
|
| 133 |
+
input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg]
|
| 134 |
output_combo = [cookies, chatbot, history, status]
|
| 135 |
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
|
| 136 |
# 提交按钮、重置按钮
|
|
|
|
| 157 |
# 函数插件-下拉菜单与随变按钮的互动
|
| 158 |
def on_dropdown_changed(k):
|
| 159 |
variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
|
| 160 |
+
ret = {switchy_bt: gr.update(value=k, variant=variant)}
|
| 161 |
+
if crazy_fns[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
|
| 162 |
+
ret.update({plugin_advanced_arg: gr.update(visible=True, label=f"插件[{k}]的高级参数说明:" + crazy_fns[k].get("ArgsReminder", [f"没有提供高级参数功能说明"]))})
|
| 163 |
+
else:
|
| 164 |
+
ret.update({plugin_advanced_arg: gr.update(visible=False, label=f"插件[{k}]不需要高级参数。")})
|
| 165 |
+
return ret
|
| 166 |
+
dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, plugin_advanced_arg] )
|
| 167 |
def on_md_dropdown_changed(k):
|
| 168 |
return {chatbot: gr.update(label="当前模型:"+k)}
|
| 169 |
md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
|
| 170 |
# 随变按钮的回调函数注册
|
| 171 |
def route(k, *args, **kwargs):
|
| 172 |
+
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
|
| 173 |
yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs)
|
| 174 |
click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
|
| 175 |
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
|
|
|
|
| 187 |
print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
|
| 188 |
print(f"\t(亮色主题): http://localhost:{PORT}")
|
| 189 |
print(f"\t(暗色主题): http://localhost:{PORT}/?__dark-theme=true")
|
| 190 |
+
def open():
|
| 191 |
time.sleep(2) # 打开浏览器
|
| 192 |
webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true")
|
| 193 |
threading.Thread(target=open, name="open-browser", daemon=True).start()
|
toolbox.py
CHANGED
|
@@ -24,23 +24,23 @@ def ArgsGeneralWrapper(f):
|
|
| 24 |
"""
|
| 25 |
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
| 26 |
"""
|
| 27 |
-
def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *args):
|
| 28 |
txt_passon = txt
|
| 29 |
if txt == "" and txt2 != "": txt_passon = txt2
|
| 30 |
# 引入一个有cookie的chatbot
|
| 31 |
cookies.update({
|
| 32 |
-
'top_p':top_p,
|
| 33 |
'temperature':temperature,
|
| 34 |
})
|
| 35 |
llm_kwargs = {
|
| 36 |
'api_key': cookies['api_key'],
|
| 37 |
'llm_model': llm_model,
|
| 38 |
-
'top_p':top_p,
|
| 39 |
'max_length': max_length,
|
| 40 |
'temperature':temperature,
|
| 41 |
}
|
| 42 |
plugin_kwargs = {
|
| 43 |
-
|
| 44 |
}
|
| 45 |
chatbot_with_cookie = ChatBotWithCookies(cookies)
|
| 46 |
chatbot_with_cookie.write_list(chatbot)
|
|
@@ -219,7 +219,7 @@ def markdown_convertion(txt):
|
|
| 219 |
return content
|
| 220 |
else:
|
| 221 |
return tex2mathml_catch_exception(content)
|
| 222 |
-
|
| 223 |
def markdown_bug_hunt(content):
|
| 224 |
"""
|
| 225 |
解决一个mdx_math的bug(单$包裹begin命令时多余<script>)
|
|
@@ -227,7 +227,7 @@ def markdown_convertion(txt):
|
|
| 227 |
content = content.replace('<script type="math/tex">\n<script type="math/tex; mode=display">', '<script type="math/tex; mode=display">')
|
| 228 |
content = content.replace('</script>\n</script>', '</script>')
|
| 229 |
return content
|
| 230 |
-
|
| 231 |
|
| 232 |
if ('$' in txt) and ('```' not in txt): # 有$标识的公式符号,且没有代码段```的标识
|
| 233 |
# convert everything to html format
|
|
@@ -248,7 +248,7 @@ def markdown_convertion(txt):
|
|
| 248 |
def close_up_code_segment_during_stream(gpt_reply):
|
| 249 |
"""
|
| 250 |
在gpt输出代码的中途(输出了前面的```,但还没输出完后面的```),补上后面的```
|
| 251 |
-
|
| 252 |
Args:
|
| 253 |
gpt_reply (str): GPT模型返回的回复字符串。
|
| 254 |
|
|
@@ -511,7 +511,7 @@ class DummyWith():
|
|
| 511 |
它的作用是……额……没用,即在代码结构不变得情况下取代其他的上下文管理器。
|
| 512 |
上下文管理器是一种Python对象,用于与with语句一起使用,
|
| 513 |
以确保一些资源在代码块执行期间得到正确的初始化和清理。
|
| 514 |
-
上下文管理器必须实现两个方法,分别为 __enter__()和 __exit__()。
|
| 515 |
在上下文执行开始的情况下,__enter__()方法会在代码块被执行前被调用,
|
| 516 |
而在上下文执行结束时,__exit__()方法则会被调用。
|
| 517 |
"""
|
|
|
|
| 24 |
"""
|
| 25 |
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
| 26 |
"""
|
| 27 |
+
def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg, *args):
|
| 28 |
txt_passon = txt
|
| 29 |
if txt == "" and txt2 != "": txt_passon = txt2
|
| 30 |
# 引入一个有cookie的chatbot
|
| 31 |
cookies.update({
|
| 32 |
+
'top_p':top_p,
|
| 33 |
'temperature':temperature,
|
| 34 |
})
|
| 35 |
llm_kwargs = {
|
| 36 |
'api_key': cookies['api_key'],
|
| 37 |
'llm_model': llm_model,
|
| 38 |
+
'top_p':top_p,
|
| 39 |
'max_length': max_length,
|
| 40 |
'temperature':temperature,
|
| 41 |
}
|
| 42 |
plugin_kwargs = {
|
| 43 |
+
"advanced_arg": plugin_advanced_arg,
|
| 44 |
}
|
| 45 |
chatbot_with_cookie = ChatBotWithCookies(cookies)
|
| 46 |
chatbot_with_cookie.write_list(chatbot)
|
|
|
|
| 219 |
return content
|
| 220 |
else:
|
| 221 |
return tex2mathml_catch_exception(content)
|
| 222 |
+
|
| 223 |
def markdown_bug_hunt(content):
|
| 224 |
"""
|
| 225 |
解决一个mdx_math的bug(单$包裹begin命令时多余<script>)
|
|
|
|
| 227 |
content = content.replace('<script type="math/tex">\n<script type="math/tex; mode=display">', '<script type="math/tex; mode=display">')
|
| 228 |
content = content.replace('</script>\n</script>', '</script>')
|
| 229 |
return content
|
| 230 |
+
|
| 231 |
|
| 232 |
if ('$' in txt) and ('```' not in txt): # 有$标识的公式符号,且没有代码段```的标识
|
| 233 |
# convert everything to html format
|
|
|
|
| 248 |
def close_up_code_segment_during_stream(gpt_reply):
|
| 249 |
"""
|
| 250 |
在gpt输出代码的中途(输出了前面的```,但还没输出完后面的```),补上后面的```
|
| 251 |
+
|
| 252 |
Args:
|
| 253 |
gpt_reply (str): GPT模型返回的回复字符串。
|
| 254 |
|
|
|
|
| 511 |
它的作用是……额……没用,即在代码结构不变得情况下取代其他的上下文管理器。
|
| 512 |
上下文管理器是一种Python对象,用于与with语句一起使用,
|
| 513 |
以确保一些资源在代码块执行期间得到正确的初始化和清理。
|
| 514 |
+
上下文管理器必须实现两个方法,分别为 __enter__()和 __exit__()。
|
| 515 |
在上下文执行开始的情况下,__enter__()方法会在代码块被执行前被调用,
|
| 516 |
而在上下文执行结束时,__exit__()方法则会被调用。
|
| 517 |
"""
|