From 7947c968adddc13ea46039ce670e94c762bc3e99 Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Thu, 25 May 2023 13:46:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E6=8C=87=E5=AE=9Amarkdown?= =?UTF-8?q?=E7=9A=84=E7=BF=BB=E8=AF=91=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functional.py | 15 ++++++ crazy_functions/Latex全文润色.py | 7 ++- crazy_functions/crazy_functions_test.py | 16 +++++- crazy_functions/批量Markdown翻译.py | 69 +++++++++++++++++++++++-- crazy_functions/数学动画生成manim.py | 6 +-- toolbox.py | 4 ++ 6 files changed, 103 insertions(+), 14 deletions(-) diff --git a/crazy_functional.py b/crazy_functional.py index f9b9548..91c85cf 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -280,5 +280,20 @@ def get_crazy_functions(): }) except: print('Load function plugin failed') + + try: + from crazy_functions.批量Markdown翻译 import Markdown翻译指定语言 + function_plugins.update({ + "Markdown翻译(手动指定语言)": { + "Color": "stop", + "AsButton": False, + "AdvancedArgs": True, + "ArgsReminder": "请输入要翻译成哪种语言,默认为Chinese。", + "Function": HotReload(Markdown翻译指定语言) + } + }) + except: + print('Load function plugin failed') + ###################### 第n组插件 ########################### return function_plugins diff --git a/crazy_functions/Latex全文润色.py b/crazy_functions/Latex全文润色.py index f4e3a4e..1175e86 100644 --- a/crazy_functions/Latex全文润色.py +++ b/crazy_functions/Latex全文润色.py @@ -113,12 +113,11 @@ def 多文件润色(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch scroller_max_len = 80 ) - pfg.sp_file_result = [] - for i_say, gpt_say in zip(gpt_response_collection[0::2], gpt_response_collection[1::2]): - pfg.sp_file_result.append(gpt_say) - # <-------- 文本碎片重组为完整的tex文件,整理结果为压缩包 ----------> try: + pfg.sp_file_result = [] + for i_say, gpt_say in zip(gpt_response_collection[0::2], gpt_response_collection[1::2]): + pfg.sp_file_result.append(gpt_say) pfg.merge_result() pfg.write_result() pfg.zip_result() diff --git a/crazy_functions/crazy_functions_test.py b/crazy_functions/crazy_functions_test.py index 69b54d3..3d5c355 100644 --- a/crazy_functions/crazy_functions_test.py +++ b/crazy_functions/crazy_functions_test.py @@ -105,6 +105,19 @@ def test_数学动画生成manim(): for cookies, cb, hist, msg in 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): print(cb) + + +def test_Markdown多语言(): + from crazy_functions.批量Markdown翻译 import Markdown翻译指定语言 + txt = "README.md" + history = [] + for lang in ["English", "Spanish", "French", "German", "Italian", "Chinese", "Japanese", "Korean", "Portuguese", "Russian", "Arabic"]: + plugin_kwargs = {"advanced_arg": lang} + for cookies, cb, hist, msg in Markdown翻译指定语言(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) + + + # test_解析一个Python项目() # test_Latex英文润色() # test_Markdown中译英() @@ -115,7 +128,8 @@ def test_数学动画生成manim(): # test_解析一个Cpp项目() # test_联网回答问题() # test_解析ipynb文件() -test_数学动画生成manim() +# test_数学动画生成manim() +test_Markdown多语言() input("程序完成,回车退出。") print("退出。") \ No newline at end of file diff --git a/crazy_functions/批量Markdown翻译.py b/crazy_functions/批量Markdown翻译.py index 26f42ca..c1baade 100644 --- a/crazy_functions/批量Markdown翻译.py +++ b/crazy_functions/批量Markdown翻译.py @@ -1,4 +1,4 @@ -from toolbox import update_ui +from toolbox import update_ui, trimmed_format_exc, gen_time_str from toolbox import CatchException, report_execption, write_results_to_file fast_debug = False @@ -32,9 +32,21 @@ class PaperFileGroup(): self.sp_file_contents.append(segment) self.sp_file_index.append(index) self.sp_file_tag.append(self.file_paths[index] + f".part-{j}.md") - print('Segmentation: done') + def merge_result(self): + self.file_result = ["" for _ in range(len(self.file_paths))] + for r, k in zip(self.sp_file_result, self.sp_file_index): + self.file_result[k] += r + + def write_result(self): + manifest = [] + for path, res in zip(self.file_paths, self.file_result): + with open(path + f'{gen_time_str()}.trans.md', 'w', encoding='utf8') as f: + manifest.append(path + '.trans.md') + f.write(res) + return manifest + def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='en'): import time, os, re from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency @@ -53,7 +65,7 @@ def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch pfg.run_file_split(max_token_limit=1500) n_split = len(pfg.sp_file_contents) - # <-------- 多线程润色开始 ----------> + # <-------- 多线程翻译开始 ----------> if language == 'en->zh': inputs_array = ["This is a Markdown file, translate it into Chinese, do not modify any existing Markdown commands:" + f"\n\n{frag}" for frag in pfg.sp_file_contents] @@ -64,6 +76,11 @@ def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch f"\n\n{frag}" for frag in pfg.sp_file_contents] inputs_show_user_array = [f"翻译 {f}" for f in pfg.sp_file_tag] sys_prompt_array = ["You are a professional academic paper translator." for _ in range(n_split)] + else: + inputs_array = [f"This is a Markdown file, translate it into {language}, do not modify any existing Markdown commands, only answer me with translated results:" + + f"\n\n{frag}" for frag in pfg.sp_file_contents] + inputs_show_user_array = [f"翻译 {f}" for f in pfg.sp_file_tag] + sys_prompt_array = ["You are a professional academic paper translator." for _ in range(n_split)] gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( inputs_array=inputs_array, @@ -75,6 +92,14 @@ def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch # max_workers=5, # OpenAI所允许的最大并行过载 scroller_max_len = 80 ) + try: + pfg.sp_file_result = [] + for i_say, gpt_say in zip(gpt_response_collection[0::2], gpt_response_collection[1::2]): + pfg.sp_file_result.append(gpt_say) + pfg.merge_result() + pfg.write_result() + except: + print(trimmed_format_exc()) # <-------- 整理结果,退出 ----------> create_report_file_name = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + f"-chatgpt.polish.md" @@ -183,4 +208,40 @@ def Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_p report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.md文件: {txt}") yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return - yield from 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='zh->en') \ No newline at end of file + yield from 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='zh->en') + + +@CatchException +def Markdown翻译指定语言(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + # 基本信息:功能、贡献者 + chatbot.append([ + "函数插件功能?", + "对整个Markdown项目进行翻译。函数插件贡献者: Binary-Husky"]) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + + # 尝试导入依赖,如果缺少依赖,则给出安装建议 + try: + import tiktoken + import glob, os + except: + report_execption(chatbot, history, + a=f"解析项目: {txt}", + b=f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade tiktoken```。") + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + return + history = [] # 清空历史,以免输入溢出 + success, file_manifest, project_folder = get_files_from_everything(txt) + if not success: + # 什么都没有 + if txt == "": txt = '空空如也的输入栏' + report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}") + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + return + if len(file_manifest) == 0: + report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.md文件: {txt}") + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + return + + if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg") + language = plugin_kwargs.get("advanced_arg", 'Chinese') + yield from 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language=language) \ No newline at end of file diff --git a/crazy_functions/数学动画生成manim.py b/crazy_functions/数学动画生成manim.py index 9bb4d01..5851b9c 100644 --- a/crazy_functions/数学动画生成manim.py +++ b/crazy_functions/数学动画生成manim.py @@ -1,4 +1,4 @@ -from toolbox import CatchException, update_ui +from toolbox import CatchException, update_ui, gen_time_str from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive from .crazy_utils import input_clipping @@ -12,10 +12,6 @@ def inspect_dependency(chatbot, history): yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return False -def gen_time_str(): - import time - return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) - def eval_manim(code): import subprocess, sys, os, shutil diff --git a/toolbox.py b/toolbox.py index 08ba8ed..23bbbfb 100644 --- a/toolbox.py +++ b/toolbox.py @@ -727,6 +727,7 @@ def clip_history(inputs, history, tokenizer, max_token_limit): 第三部分 其他小工具: - zip_folder: 把某个路径下所有文件压缩,然后转移到指定的另一个路径中(gpt写的) + - gen_time_str: 生成时间戳 ======================================================================== """ @@ -761,3 +762,6 @@ def zip_folder(source_folder, dest_folder, zip_name): print(f"Zip file created at {zip_file}") +def gen_time_str(): + import time + return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) \ No newline at end of file