Spaces:
Running
Running
现在指定markdown的翻译语言
Browse files- crazy_functional.py +15 -0
- crazy_functions/Latex全文润色.py +3 -4
- crazy_functions/crazy_functions_test.py +15 -1
- crazy_functions/批量Markdown翻译.py +65 -4
- crazy_functions/数学动画生成manim.py +1 -5
- toolbox.py +4 -0
crazy_functional.py
CHANGED
@@ -280,5 +280,20 @@ def get_crazy_functions():
|
|
280 |
})
|
281 |
except:
|
282 |
print('Load function plugin failed')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
###################### 第n组插件 ###########################
|
284 |
return function_plugins
|
|
|
280 |
})
|
281 |
except:
|
282 |
print('Load function plugin failed')
|
283 |
+
|
284 |
+
try:
|
285 |
+
from crazy_functions.批量Markdown翻译 import Markdown翻译指定语言
|
286 |
+
function_plugins.update({
|
287 |
+
"Markdown翻译(手动指定语言)": {
|
288 |
+
"Color": "stop",
|
289 |
+
"AsButton": False,
|
290 |
+
"AdvancedArgs": True,
|
291 |
+
"ArgsReminder": "请输入要翻译成哪种语言,默认为Chinese。",
|
292 |
+
"Function": HotReload(Markdown翻译指定语言)
|
293 |
+
}
|
294 |
+
})
|
295 |
+
except:
|
296 |
+
print('Load function plugin failed')
|
297 |
+
|
298 |
###################### 第n组插件 ###########################
|
299 |
return function_plugins
|
crazy_functions/Latex全文润色.py
CHANGED
@@ -113,12 +113,11 @@ def 多文件润色(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch
|
|
113 |
scroller_max_len = 80
|
114 |
)
|
115 |
|
116 |
-
pfg.sp_file_result = []
|
117 |
-
for i_say, gpt_say in zip(gpt_response_collection[0::2], gpt_response_collection[1::2]):
|
118 |
-
pfg.sp_file_result.append(gpt_say)
|
119 |
-
|
120 |
# <-------- 文本碎片重组为完整的tex文件,整理结果为压缩包 ---------->
|
121 |
try:
|
|
|
|
|
|
|
122 |
pfg.merge_result()
|
123 |
pfg.write_result()
|
124 |
pfg.zip_result()
|
|
|
113 |
scroller_max_len = 80
|
114 |
)
|
115 |
|
|
|
|
|
|
|
|
|
116 |
# <-------- 文本碎片重组为完整的tex文件,整理结果为压缩包 ---------->
|
117 |
try:
|
118 |
+
pfg.sp_file_result = []
|
119 |
+
for i_say, gpt_say in zip(gpt_response_collection[0::2], gpt_response_collection[1::2]):
|
120 |
+
pfg.sp_file_result.append(gpt_say)
|
121 |
pfg.merge_result()
|
122 |
pfg.write_result()
|
123 |
pfg.zip_result()
|
crazy_functions/crazy_functions_test.py
CHANGED
@@ -105,6 +105,19 @@ def test_数学动画生成manim():
|
|
105 |
for cookies, cb, hist, msg in 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
106 |
print(cb)
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# test_解析一个Python项目()
|
109 |
# test_Latex英文润色()
|
110 |
# test_Markdown中译英()
|
@@ -115,7 +128,8 @@ def test_数学动画生成manim():
|
|
115 |
# test_解析一个Cpp项目()
|
116 |
# test_联网回答问题()
|
117 |
# test_解析ipynb文件()
|
118 |
-
test_数学动画生成manim()
|
|
|
119 |
|
120 |
input("程序完成,回车退出。")
|
121 |
print("退出。")
|
|
|
105 |
for cookies, cb, hist, msg in 动画生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
106 |
print(cb)
|
107 |
|
108 |
+
|
109 |
+
|
110 |
+
def test_Markdown多语言():
|
111 |
+
from crazy_functions.批量Markdown翻译 import Markdown翻译指定语言
|
112 |
+
txt = "README.md"
|
113 |
+
history = []
|
114 |
+
for lang in ["English", "Spanish", "French", "German", "Italian", "Chinese", "Japanese", "Korean", "Portuguese", "Russian", "Arabic"]:
|
115 |
+
plugin_kwargs = {"advanced_arg": lang}
|
116 |
+
for cookies, cb, hist, msg in Markdown翻译指定语言(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
117 |
+
print(cb)
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
# test_解析一个Python项目()
|
122 |
# test_Latex英文润色()
|
123 |
# test_Markdown中译英()
|
|
|
128 |
# test_解析一个Cpp项目()
|
129 |
# test_联网回答问题()
|
130 |
# test_解析ipynb文件()
|
131 |
+
# test_数学动画生成manim()
|
132 |
+
test_Markdown多语言()
|
133 |
|
134 |
input("程序完成,回车退出。")
|
135 |
print("退出。")
|
crazy_functions/批量Markdown翻译.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from toolbox import update_ui
|
2 |
from toolbox import CatchException, report_execption, write_results_to_file
|
3 |
fast_debug = False
|
4 |
|
@@ -32,9 +32,21 @@ class PaperFileGroup():
|
|
32 |
self.sp_file_contents.append(segment)
|
33 |
self.sp_file_index.append(index)
|
34 |
self.sp_file_tag.append(self.file_paths[index] + f".part-{j}.md")
|
35 |
-
|
36 |
print('Segmentation: done')
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='en'):
|
39 |
import time, os, re
|
40 |
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
|
|
53 |
pfg.run_file_split(max_token_limit=1500)
|
54 |
n_split = len(pfg.sp_file_contents)
|
55 |
|
56 |
-
# <--------
|
57 |
if language == 'en->zh':
|
58 |
inputs_array = ["This is a Markdown file, translate it into Chinese, do not modify any existing Markdown commands:" +
|
59 |
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
|
|
64 |
f"\n\n{frag}" for frag in pfg.sp_file_contents]
|
65 |
inputs_show_user_array = [f"翻译 {f}" for f in pfg.sp_file_tag]
|
66 |
sys_prompt_array = ["You are a professional academic paper translator." for _ in range(n_split)]
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
69 |
inputs_array=inputs_array,
|
@@ -75,6 +92,14 @@ def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch
|
|
75 |
# max_workers=5, # OpenAI所允许的最大并行过载
|
76 |
scroller_max_len = 80
|
77 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# <-------- 整理结果,退出 ---------->
|
80 |
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
|
|
183 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.md文件: {txt}")
|
184 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
185 |
return
|
186 |
-
yield from 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='zh->en')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from toolbox import update_ui, trimmed_format_exc, gen_time_str
|
2 |
from toolbox import CatchException, report_execption, write_results_to_file
|
3 |
fast_debug = False
|
4 |
|
|
|
32 |
self.sp_file_contents.append(segment)
|
33 |
self.sp_file_index.append(index)
|
34 |
self.sp_file_tag.append(self.file_paths[index] + f".part-{j}.md")
|
|
|
35 |
print('Segmentation: done')
|
36 |
|
37 |
+
def merge_result(self):
|
38 |
+
self.file_result = ["" for _ in range(len(self.file_paths))]
|
39 |
+
for r, k in zip(self.sp_file_result, self.sp_file_index):
|
40 |
+
self.file_result[k] += r
|
41 |
+
|
42 |
+
def write_result(self):
|
43 |
+
manifest = []
|
44 |
+
for path, res in zip(self.file_paths, self.file_result):
|
45 |
+
with open(path + f'{gen_time_str()}.trans.md', 'w', encoding='utf8') as f:
|
46 |
+
manifest.append(path + '.trans.md')
|
47 |
+
f.write(res)
|
48 |
+
return manifest
|
49 |
+
|
50 |
def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='en'):
|
51 |
import time, os, re
|
52 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
|
|
65 |
pfg.run_file_split(max_token_limit=1500)
|
66 |
n_split = len(pfg.sp_file_contents)
|
67 |
|
68 |
+
# <-------- 多线程翻译开始 ---------->
|
69 |
if language == 'en->zh':
|
70 |
inputs_array = ["This is a Markdown file, translate it into Chinese, do not modify any existing Markdown commands:" +
|
71 |
f"\n\n{frag}" for frag in pfg.sp_file_contents]
|
|
|
76 |
f"\n\n{frag}" for frag in pfg.sp_file_contents]
|
77 |
inputs_show_user_array = [f"翻译 {f}" for f in pfg.sp_file_tag]
|
78 |
sys_prompt_array = ["You are a professional academic paper translator." for _ in range(n_split)]
|
79 |
+
else:
|
80 |
+
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:" +
|
81 |
+
f"\n\n{frag}" for frag in pfg.sp_file_contents]
|
82 |
+
inputs_show_user_array = [f"翻译 {f}" for f in pfg.sp_file_tag]
|
83 |
+
sys_prompt_array = ["You are a professional academic paper translator." for _ in range(n_split)]
|
84 |
|
85 |
gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
86 |
inputs_array=inputs_array,
|
|
|
92 |
# max_workers=5, # OpenAI所允许的最大并行过载
|
93 |
scroller_max_len = 80
|
94 |
)
|
95 |
+
try:
|
96 |
+
pfg.sp_file_result = []
|
97 |
+
for i_say, gpt_say in zip(gpt_response_collection[0::2], gpt_response_collection[1::2]):
|
98 |
+
pfg.sp_file_result.append(gpt_say)
|
99 |
+
pfg.merge_result()
|
100 |
+
pfg.write_result()
|
101 |
+
except:
|
102 |
+
print(trimmed_format_exc())
|
103 |
|
104 |
# <-------- 整理结果,退出 ---------->
|
105 |
create_report_file_name = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + f"-chatgpt.polish.md"
|
|
|
208 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.md文件: {txt}")
|
209 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
210 |
return
|
211 |
+
yield from 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='zh->en')
|
212 |
+
|
213 |
+
|
214 |
+
@CatchException
|
215 |
+
def Markdown翻译指定语言(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
216 |
+
# 基本信息:功能、贡献者
|
217 |
+
chatbot.append([
|
218 |
+
"函数插件功能?",
|
219 |
+
"对整个Markdown项目进行翻译。函数插件贡献者: Binary-Husky"])
|
220 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
221 |
+
|
222 |
+
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
223 |
+
try:
|
224 |
+
import tiktoken
|
225 |
+
import glob, os
|
226 |
+
except:
|
227 |
+
report_execption(chatbot, history,
|
228 |
+
a=f"解析项目: {txt}",
|
229 |
+
b=f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade tiktoken```。")
|
230 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
231 |
+
return
|
232 |
+
history = [] # 清空历史,以免输入溢出
|
233 |
+
success, file_manifest, project_folder = get_files_from_everything(txt)
|
234 |
+
if not success:
|
235 |
+
# 什么都没有
|
236 |
+
if txt == "": txt = '空空如���的输入栏'
|
237 |
+
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
238 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
239 |
+
return
|
240 |
+
if len(file_manifest) == 0:
|
241 |
+
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.md文件: {txt}")
|
242 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
243 |
+
return
|
244 |
+
|
245 |
+
if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg")
|
246 |
+
language = plugin_kwargs.get("advanced_arg", 'Chinese')
|
247 |
+
yield from 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language=language)
|
crazy_functions/数学动画生成manim.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from toolbox import CatchException, update_ui
|
2 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
3 |
from .crazy_utils import input_clipping
|
4 |
|
@@ -12,10 +12,6 @@ def inspect_dependency(chatbot, history):
|
|
12 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
13 |
return False
|
14 |
|
15 |
-
def gen_time_str():
|
16 |
-
import time
|
17 |
-
return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
|
18 |
-
|
19 |
def eval_manim(code):
|
20 |
import subprocess, sys, os, shutil
|
21 |
|
|
|
1 |
+
from toolbox import CatchException, update_ui, gen_time_str
|
2 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
3 |
from .crazy_utils import input_clipping
|
4 |
|
|
|
12 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
13 |
return False
|
14 |
|
|
|
|
|
|
|
|
|
15 |
def eval_manim(code):
|
16 |
import subprocess, sys, os, shutil
|
17 |
|
toolbox.py
CHANGED
@@ -727,6 +727,7 @@ def clip_history(inputs, history, tokenizer, max_token_limit):
|
|
727 |
第三部分
|
728 |
其他小工具:
|
729 |
- zip_folder: 把某个路径下所有文件压缩,然后转移到指定的另一个路径中(gpt写的)
|
|
|
730 |
========================================================================
|
731 |
"""
|
732 |
|
@@ -761,3 +762,6 @@ def zip_folder(source_folder, dest_folder, zip_name):
|
|
761 |
|
762 |
print(f"Zip file created at {zip_file}")
|
763 |
|
|
|
|
|
|
|
|
727 |
第三部分
|
728 |
其他小工具:
|
729 |
- zip_folder: 把某个路径下所有文件压缩,然后转移到指定的另一个路径中(gpt写的)
|
730 |
+
- gen_time_str: 生成时间戳
|
731 |
========================================================================
|
732 |
"""
|
733 |
|
|
|
762 |
|
763 |
print(f"Zip file created at {zip_file}")
|
764 |
|
765 |
+
def gen_time_str():
|
766 |
+
import time
|
767 |
+
return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
|