diff --git a/crazy_functional.py b/crazy_functional.py index 609c0a15..4bbdd887 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -47,8 +47,16 @@ def get_crazy_functions(): from crazy_functions.Internet_GPT_Wrap import NetworkGPT_Wrap from crazy_functions.Image_Generate import 图片生成_DALLE2, 图片生成_DALLE3, 图片修改_DALLE2 from crazy_functions.Image_Generate_Wrap import ImageGen_Wrap + from crazy_functions.SourceCode_Comment import 注释Python项目 function_plugins = { + "注释Python项目": { + "Group": "对话|编程|学术|智能体", + "Color": "stop", + "AsButton": True, + "Info": "注释Python项目", + "Function": HotReload(注释Python项目), + }, "虚空终端": { "Group": "对话|编程|学术|智能体", "Color": "stop", diff --git a/crazy_functions/SourceCode_Comment.py b/crazy_functions/SourceCode_Comment.py index 20d8ff85..b0d9378b 100644 --- a/crazy_functions/SourceCode_Comment.py +++ b/crazy_functions/SourceCode_Comment.py @@ -1,5 +1,5 @@ import os, copy, time -from toolbox import CatchException, report_exception, update_ui, zip_result, promote_file_to_downloadzone, update_ui_lastest_msg, get_conf +from toolbox import CatchException, report_exception, update_ui, zip_result, promote_file_to_downloadzone, update_ui_lastest_msg, get_conf, generate_file_link from shared_utils.fastapi_server import validate_path_safety from crazy_functions.crazy_utils import input_clipping from crazy_functions.crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency @@ -60,6 +60,16 @@ def 注释源代码(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch # <将结果写回源文件> with open(fp, 'w', encoding='utf-8') as f: f.write(file_tree_struct.manifest[fp].revised_content) + # <生成对比html> + with open("crazy_functions/agent_fns/python_comment_compare.html", 'r', encoding='utf-8') as f: + html_template = f.read() + html_template = html_template.replace("REPLACE_CODE_FILE_LEFT", pcc.original_content) + html_template = html_template.replace("REPLACE_CODE_FILE_RIGHT", revised_content) + compare_html_path = fp + '.compare.html' + file_tree_struct.manifest[fp].compare_html = compare_html_path + with open(compare_html_path, 'w', encoding='utf-8') as f: + f.write(html_template) + chatbot.append([None, f"正在处理:"]) futures = [] @@ -69,14 +79,25 @@ def 注释源代码(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch cnt = 0 while True: - # yield一次以刷新前端页面 cnt += 1 time.sleep(3) worker_done = [h.done() for h in futures] remain = len(worker_done) - sum(worker_done) - yield from update_ui_lastest_msg(f"剩余源文件数量: {remain}." + ''.join(['.']*(cnt % 10+1)), chatbot=chatbot, history=history, delay=0) - # 更好的UI视觉效果 + # <展示已经完成的部分> + preview_html_list = [] + for done, fp in zip(worker_done, file_manifest): + if not done: continue + preview_html_list.append(file_tree_struct.manifest[fp].compare_html) + file_links = generate_file_link(preview_html_list) + + yield from update_ui_lastest_msg( + f"剩余源文件数量: {remain}.\n\n" + + f"已完成的文件: {sum(worker_done)}.\n\n" + + file_links + + "\n\n" + + ''.join(['.']*(cnt % 10 + 1) + ), chatbot=chatbot, history=history, delay=0) yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面 if all(worker_done): executor.shutdown() diff --git a/crazy_functions/agent_fns/python_comment_agent.py b/crazy_functions/agent_fns/python_comment_agent.py index e8fdea9a..9a394a57 100644 --- a/crazy_functions/agent_fns/python_comment_agent.py +++ b/crazy_functions/agent_fns/python_comment_agent.py @@ -118,6 +118,7 @@ def zip_result(folder): class PythonCodeComment(): def __init__(self, llm_kwargs, language) -> None: + self.original_content = "" self.full_context = [] self.full_context_with_line_no = [] self.current_page_start = 0 @@ -140,6 +141,7 @@ class PythonCodeComment(): def read_file(self, path, brief): with open(path, 'r', encoding='utf8') as f: self.full_context = f.readlines() + self.original_content = ''.join(self.full_context) self.file_basename = os.path.basename(path) self.file_brief = brief self.full_context_with_line_no = self.generate_tagged_code_from_full_context() diff --git a/crazy_functions/agent_fns/python_comment_compare.html b/crazy_functions/agent_fns/python_comment_compare.html new file mode 100644 index 00000000..d5ca926e --- /dev/null +++ b/crazy_functions/agent_fns/python_comment_compare.html @@ -0,0 +1,47 @@ + + + + + 源文件对比 + + + +
+
+
+REPLACE_CODE_FILE_LEFT
+
+
+
+
+REPLACE_CODE_FILE_RIGHT
+
+
+
+ + \ No newline at end of file diff --git a/toolbox.py b/toolbox.py index a40f71d0..4a1c162e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -567,8 +567,6 @@ def generate_file_link(report_files:List[str]): return file_links - - def on_report_generated(cookies:dict, files:List[str], chatbot:ChatBotWithCookies): if "files_to_promote" in cookies: report_files = cookies["files_to_promote"]