From ce0d8b9dab677dfb8e46429a58eeec2ef965b0ab Mon Sep 17 00:00:00 2001
From: 505030475 <505030475@qq.com>
Date: Sun, 11 Jun 2023 01:36:23 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E8=99=9A=E7=A9=BA=E7=BB=88=E7=AB=AF?=
=?UTF-8?q?=E6=8F=92=E4=BB=B6=E9=9B=8F=E5=BD=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
crazy_functional.py | 16 ++++-
crazy_functions/虚空终端.py | 131 ++++++++++++++++++++++++++++++++++++
toolbox.py | 16 +++--
3 files changed, 158 insertions(+), 5 deletions(-)
create mode 100644 crazy_functions/虚空终端.py
diff --git a/crazy_functional.py b/crazy_functional.py
index d8ca9ae..2f0fbaa 100644
--- a/crazy_functional.py
+++ b/crazy_functional.py
@@ -368,5 +368,19 @@ def get_crazy_functions():
})
except:
print('Load function plugin failed')
- ###################### 第n组插件 ###########################
+
+ try:
+ from crazy_functions.虚空终端 import 终端
+ function_plugins.update({
+ "超级终端": {
+ "Color": "stop",
+ "AsButton": False,
+ # "AdvancedArgs": True,
+ # "ArgsReminder": "",
+ "Function": HotReload(终端)
+ }
+ })
+ except:
+ print('Load function plugin failed')
+
return function_plugins
diff --git a/crazy_functions/虚空终端.py b/crazy_functions/虚空终端.py
new file mode 100644
index 0000000..fe71a46
--- /dev/null
+++ b/crazy_functions/虚空终端.py
@@ -0,0 +1,131 @@
+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
+
+
+prompt = """
+I have to achieve some functionalities by calling one of the functions below.
+Your job is to find the correct funtion to use to satisfy my requirement,
+and then write python code to call this function with correct parameters.
+
+These are functions you are allowed to choose from:
+1.
+ 功能描述: 总结音视频内容
+ 调用函数: ConcludeAudioContent(txt, llm_kwargs)
+ 参数说明:
+ txt: 音频文件的路径
+ llm_kwargs: 模型参数, 永远给定None
+2.
+ 功能描述: 将每次对话记录写入Markdown格式的文件中
+ 调用函数: WriteMarkdown()
+3.
+ 功能描述: 将指定目录下的PDF文件从英文翻译成中文
+ 调用函数: BatchTranslatePDFDocuments_MultiThreaded(txt, llm_kwargs)
+ 参数说明:
+ txt: PDF文件所在的路径
+ llm_kwargs: 模型参数, 永远给定None
+4.
+ 功能描述: 根据文本使用GPT模型生成相应的图像
+ 调用函数: ImageGeneration(txt, llm_kwargs)
+ 参数说明:
+ txt: 图像生成所用到的提示文本
+ llm_kwargs: 模型参数, 永远给定None
+5.
+ 功能描述: 对输入的word文档进行摘要生成
+ 调用函数: SummarizingWordDocuments(input_path, output_path)
+ 参数说明:
+ input_path: 待处理的word文档路径
+ output_path: 摘要生成后的文档路径
+
+
+You should always anwser with following format:
+----------------
+Code:
+```
+class AutoAcademic(object):
+ def __init__(self):
+ self.selected_function = "FILL_CORRECT_FUNCTION_HERE" # e.g., "GenerateImage"
+ self.txt = "FILL_MAIN_PARAMETER_HERE" # e.g., "荷叶上的蜻蜓"
+ self.llm_kwargs = None
+```
+Explanation:
+只有GenerateImage和生成图像相关, 因此选择GenerateImage函数。
+----------------
+
+Now, this is my requirement:
+
+"""
+def get_fn_lib():
+ return {
+ "BatchTranslatePDFDocuments_MultiThreaded": ("crazy_functions.批量翻译PDF文档_多线程", "批量翻译PDF文档"),
+ "SummarizingWordDocuments": ("crazy_functions.总结word文档", "总结word文档"),
+ "ImageGeneration": ("crazy_functions.图片生成", "图片生成"),
+ "TranslateMarkdownFromEnglishToChinese": ("crazy_functions.批量Markdown翻译", "Markdown中译英"),
+ "SummaryAudioVideo": ("crazy_functions.总结音视频", "总结音视频"),
+ }
+
+def inspect_dependency(chatbot, history):
+ return True
+
+def eval_code(code, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
+ import subprocess, sys, os, shutil, importlib
+
+ with open('gpt_log/void_terminal_runtime.py', 'w', encoding='utf8') as f:
+ f.write(code)
+
+ try:
+ AutoAcademic = getattr(importlib.import_module('gpt_log.void_terminal_runtime', 'AutoAcademic'), 'AutoAcademic')
+ # importlib.reload(AutoAcademic)
+ auto_dict = AutoAcademic()
+ selected_function = auto_dict.selected_function
+ txt = auto_dict.txt
+ fp, fn = get_fn_lib()[selected_function]
+ fn_plugin = getattr(importlib.import_module(fp, fn), fn)
+ yield from fn_plugin(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port)
+ except:
+ from toolbox import trimmed_format_exc
+ chatbot.append(["执行错误", f"\n```\n{trimmed_format_exc()}\n```\n"])
+ yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
+
+def get_code_block(reply):
+ import re
+ pattern = r"```([\s\S]*?)```" # regex pattern to match code blocks
+ matches = re.findall(pattern, reply) # find all code blocks in text
+ if len(matches) != 1:
+ raise RuntimeError("GPT is not generating proper code.")
+ return matches[0].strip('python') # code block
+
+@CatchException
+def 终端(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
+ """
+ txt 输入栏用户输入的文本, 例如需要翻译的一段话, 再例如一个包含了待处理文件的路径
+ llm_kwargs gpt模型参数, 如温度和top_p等, 一般原样传递下去就行
+ plugin_kwargs 插件模型的参数, 暂时没有用武之地
+ chatbot 聊天显示框的句柄, 用于显示给用户
+ history 聊天历史, 前情提要
+ system_prompt 给gpt的静默提醒
+ web_port 当前软件运行的端口号
+ """
+ # 清空历史, 以免输入溢出
+ history = []
+
+ # 基本信息:功能、贡献者
+ chatbot.append(["函数插件功能?", "根据自然语言执行插件命令, 作者: binary-husky, 插件初始化中 ..."])
+ yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
+
+ # # 尝试导入依赖, 如果缺少依赖, 则给出安装建议
+ # dep_ok = yield from inspect_dependency(chatbot=chatbot, history=history) # 刷新界面
+ # if not dep_ok: return
+
+ # 输入
+ i_say = prompt + txt
+ # 开始
+ gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
+ inputs=i_say, inputs_show_user=txt,
+ llm_kwargs=llm_kwargs, chatbot=chatbot, history=[],
+ sys_prompt=""
+ )
+
+ # 将代码转为动画
+ code = get_code_block(gpt_say)
+ yield from eval_code(code, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port)
diff --git a/toolbox.py b/toolbox.py
index 4b0e1dd..4ab1116 100644
--- a/toolbox.py
+++ b/toolbox.py
@@ -221,16 +221,21 @@ def text_divide_paragraph(text):
"""
将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。
"""
+ pre = '
'
+ suf = '
'
+ if text.startswith(pre) and text.endswith(suf):
+ return text
+
if '```' in text:
# careful input
- return text
+ return pre + text + suf
else:
# wtf input
lines = text.split("\n")
for i, line in enumerate(lines):
lines[i] = lines[i].replace(" ", " ")
text = "".join(lines)
- return text
+ return pre + text + suf
@lru_cache(maxsize=128) # 使用 lru缓存 加快转换速度
def markdown_convertion(txt):
@@ -342,8 +347,11 @@ def format_io(self, y):
if y is None or y == []:
return []
i_ask, gpt_reply = y[-1]
- i_ask = text_divide_paragraph(i_ask) # 输入部分太自由,预处理一波
- gpt_reply = close_up_code_segment_during_stream(gpt_reply) # 当代码输出半截的时候,试着补上后个```
+ # 输入部分太自由,预处理一波
+ if i_ask is not None: i_ask = text_divide_paragraph(i_ask)
+ # 当代码输出半截的时候,试着补上后个```
+ if gpt_reply is not None: gpt_reply = close_up_code_segment_during_stream(gpt_reply)
+ # process
y[-1] = (
None if i_ask is None else markdown.markdown(i_ask, extensions=['fenced_code', 'tables']),
None if gpt_reply is None else markdown_convertion(gpt_reply)
From af7734dd35c62de6f85a18b00c3598527b85cfb4 Mon Sep 17 00:00:00 2001
From: 505030475 <505030475@qq.com>
Date: Mon, 19 Jun 2023 16:57:11 +1000
Subject: [PATCH 2/3] avoid file fusion
---
crazy_functions/latex_utils.py | 2 +-
main.py | 4 ++--
toolbox.py | 14 ++++++++++----
3 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/crazy_functions/latex_utils.py b/crazy_functions/latex_utils.py
index 78eec29..163d0e2 100644
--- a/crazy_functions/latex_utils.py
+++ b/crazy_functions/latex_utils.py
@@ -685,7 +685,7 @@ def 编译Latex(chatbot, history, main_file_original, main_file_modified, work_f
result_pdf = pj(work_folder_modified, f'{main_file_modified}.pdf')
if os.path.exists(pj(work_folder, '..', 'translation')):
shutil.copyfile(result_pdf, pj(work_folder, '..', 'translation', 'translate_zh.pdf'))
- promote_file_to_downloadzone(result_pdf)
+ promote_file_to_downloadzone(result_pdf, chatbot)
return True # 成功啦
else:
if n_fix>=max_try: break
diff --git a/main.py b/main.py
index 7dbf17f..65e1f4c 100644
--- a/main.py
+++ b/main.py
@@ -155,7 +155,7 @@ def main():
for k in crazy_fns:
if not crazy_fns[k].get("AsButton", True): continue
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])
+ click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
cancel_handles.append(click_handle)
# 函数插件-下拉菜单与随变按钮的互动
def on_dropdown_changed(k):
@@ -175,7 +175,7 @@ def main():
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs)
click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
- click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
+ click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
cancel_handles.append(click_handle)
# 终止按钮的回调函数注册
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
diff --git a/toolbox.py b/toolbox.py
index 4ab1116..ac49afc 100644
--- a/toolbox.py
+++ b/toolbox.py
@@ -439,13 +439,15 @@ def find_recent_files(directory):
return recent_files
-def promote_file_to_downloadzone(file, rename_file=None):
+def promote_file_to_downloadzone(file, rename_file=None, chatbot=None):
# 将文件复制一份到下载区
import shutil
if rename_file is None: rename_file = f'{gen_time_str()}-{os.path.basename(file)}'
new_path = os.path.join(f'./gpt_log/', rename_file)
if os.path.exists(new_path): os.remove(new_path)
shutil.copyfile(file, new_path)
+ if chatbot:
+ chatbot._cookies.update({'file_to_promote': [new_path]})
def on_file_uploaded(files, chatbot, txt, txt2, checkboxes):
"""
@@ -485,16 +487,20 @@ def on_file_uploaded(files, chatbot, txt, txt2, checkboxes):
return chatbot, txt, txt2
-def on_report_generated(files, chatbot):
+def on_report_generated(cookies, files, chatbot):
from toolbox import find_recent_files
- report_files = find_recent_files('gpt_log')
+ if 'file_to_promote' in cookies:
+ report_files = cookies['file_to_promote']
+ cookies.pop('file_to_promote')
+ else:
+ report_files = find_recent_files('gpt_log')
if len(report_files) == 0:
return None, chatbot
# files.extend(report_files)
file_links = ''
for f in report_files: file_links += f'
{f}'
chatbot.append(['报告如何远程获取?', f'报告已经添加到右侧“文件上传区”(可能处于折叠状态),请查收。{file_links}'])
- return report_files, chatbot
+ return cookies, report_files, chatbot
def is_openai_api_key(key):
API_MATCH_ORIGINAL = re.match(r"sk-[a-zA-Z0-9]{48}$", key)
From fcb0f466b982d67fcb6a30f77987cd7183e8b5a0 Mon Sep 17 00:00:00 2001
From: kaixindelele <1985790413@qq.com>
Date: Mon, 19 Jun 2023 15:11:32 +0800
Subject: [PATCH 3/3] Update crazy_functional.py
---
crazy_functional.py | 145 ++++++++++++++++++++++----------------------
1 file changed, 71 insertions(+), 74 deletions(-)
diff --git a/crazy_functional.py b/crazy_functional.py
index 2f0fbaa..462ba94 100644
--- a/crazy_functional.py
+++ b/crazy_functional.py
@@ -47,10 +47,10 @@ def get_crazy_functions():
"AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False)
"ArgsReminder": "若输入0,则不解析notebook中的Markdown块", # 高级参数输入区的显示提示
},
- "批量总结Word文档": {
- "Color": "stop",
- "Function": HotReload(总结word文档)
- },
+# "批量总结Word文档": {
+# "Color": "stop",
+# "Function": HotReload(总结word文档)
+# },
"解析整个C++项目头文件": {
"Color": "stop", # 按钮颜色
"AsButton": False, # 加入下拉菜单中
@@ -61,36 +61,36 @@ def get_crazy_functions():
"AsButton": False, # 加入下拉菜单中
"Function": HotReload(解析一个C项目)
},
- "解析整个Go项目": {
- "Color": "stop", # 按钮颜色
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(解析一个Golang项目)
- },
- "解析整个Rust项目": {
- "Color": "stop", # 按钮颜色
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(解析一个Rust项目)
- },
- "解析整个Java项目": {
- "Color": "stop", # 按钮颜色
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(解析一个Java项目)
- },
- "解析整个前端项目(js,ts,css等)": {
- "Color": "stop", # 按钮颜色
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(解析一个前端项目)
- },
- "解析整个Lua项目": {
- "Color": "stop", # 按钮颜色
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(解析一个Lua项目)
- },
- "解析整个CSharp项目": {
- "Color": "stop", # 按钮颜色
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(解析一个CSharp项目)
- },
+# "解析整个Go项目": {
+# "Color": "stop", # 按钮颜色
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(解析一个Golang项目)
+# },
+# "解析整个Rust项目": {
+# "Color": "stop", # 按钮颜色
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(解析一个Rust项目)
+# },
+# "解析整个Java项目": {
+# "Color": "stop", # 按钮颜色
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(解析一个Java项目)
+# },
+# "解析整个前端项目(js,ts,css等)": {
+# "Color": "stop", # 按钮颜色
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(解析一个前端项目)
+# },
+# "解析整个Lua项目": {
+# "Color": "stop", # 按钮颜色
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(解析一个Lua项目)
+# },
+# "解析整个CSharp项目": {
+# "Color": "stop", # 按钮颜色
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(解析一个CSharp项目)
+# },
"读Tex论文写摘要": {
"Color": "stop", # 按钮颜色
"Function": HotReload(读文章写摘要)
@@ -108,19 +108,19 @@ def get_crazy_functions():
"保存当前的对话": {
"Function": HotReload(对话历史存档)
},
- "[多线程Demo] 解析此项目本身(源码自译解)": {
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(解析项目本身)
- },
- "[老旧的Demo] 把本项目源代码切换成全英文": {
- # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
- "AsButton": False, # 加入下拉菜单中
- "Function": HotReload(全项目切换英文)
- },
- "[插件demo] 历史上的今天": {
- # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
- "Function": HotReload(高阶功能模板函数)
- },
+# "[多线程Demo] 解析此项目本身(源码自译解)": {
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(解析项目本身)
+# },
+# "[老旧的Demo] 把本项目源代码切换成全英文": {
+# # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
+# "AsButton": False, # 加入下拉菜单中
+# "Function": HotReload(全项目切换英文)
+# # },
+# "[插件demo] 历史上的今天": {
+# # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
+# "Function": HotReload(高阶功能模板函数)
+# },
}
###################### 第二组插件 ###########################
@@ -142,10 +142,10 @@ def get_crazy_functions():
"AsButton": True, # 加入下拉菜单中
"Function": HotReload(批量翻译PDF文档)
},
- "询问多个GPT模型": {
- "Color": "stop", # 按钮颜色
- "Function": HotReload(同时问询)
- },
+# "询问多个GPT模型": {
+# "Color": "stop", # 按钮颜色
+# "Function": HotReload(同时问询)
+# },
"[测试功能] 批量总结PDF文档": {
"Color": "stop",
"AsButton": False, # 加入下拉菜单中
@@ -204,6 +204,25 @@ def get_crazy_functions():
"AsButton": False, # 加入下拉菜单中
"Function": HotReload(Markdown中译英)
},
+ from crazy_functions.Latex输出PDF结果 import Latex英文纠错加PDF对比
+ from crazy_functions.Latex输出PDF结果 import Latex翻译中文并重新编译PDF
+
+ "[功能尚不稳定] Latex英文纠错+LatexDiff高亮修正位置": {
+ "Color": "stop",
+ "AsButton": False,
+ # "AdvancedArgs": True,
+ # "ArgsReminder": "",
+ "Function": HotReload(Latex英文纠错加PDF对比)
+ },
+
+ "[功能尚不稳定] Latex翻译/Arixv翻译+重构PDF": {
+ "Color": "stop",
+ "AsButton": False,
+ # "AdvancedArgs": True,
+ # "ArgsReminder": "",
+ "Function": HotReload(Latex翻译中文并重新编译PDF)
+ }
+ },
})
@@ -345,29 +364,7 @@ def get_crazy_functions():
except:
print('Load function plugin failed')
- try:
- from crazy_functions.Latex输出PDF结果 import Latex英文纠错加PDF对比
- function_plugins.update({
- "[功能尚不稳定] Latex英文纠错+LatexDiff高亮修正位置": {
- "Color": "stop",
- "AsButton": False,
- # "AdvancedArgs": True,
- # "ArgsReminder": "",
- "Function": HotReload(Latex英文纠错加PDF对比)
- }
- })
- from crazy_functions.Latex输出PDF结果 import Latex翻译中文并重新编译PDF
- function_plugins.update({
- "[功能尚不稳定] Latex翻译/Arixv翻译+重构PDF": {
- "Color": "stop",
- "AsButton": False,
- # "AdvancedArgs": True,
- # "ArgsReminder": "",
- "Function": HotReload(Latex翻译中文并重新编译PDF)
- }
- })
- except:
- print('Load function plugin failed')
+
try:
from crazy_functions.虚空终端 import 终端