Spaces:
Running
Running
增扩框架的参数IO
Browse files- crazy_functions/Latex全文润色.py +12 -12
- crazy_functions/crazy_utils.py +10 -11
- crazy_functions/下载arxiv论文翻译摘要.py +8 -8
- crazy_functions/代码重写为全英文_多线程.py +8 -8
- crazy_functions/总结word文档.py +15 -15
- crazy_functions/批量总结PDF文档.py +14 -14
- crazy_functions/批量总结PDF文档pdfminer.py +14 -14
- crazy_functions/批量翻译PDF文档_多线程.py +11 -11
- crazy_functions/理解PDF文档内容.py +17 -17
- crazy_functions/生成函数注释.py +9 -9
- crazy_functions/解析项目源代码.py +32 -33
- crazy_functions/读文章写摘要.py +12 -12
- crazy_functions/谷歌检索小助手.py +7 -7
- crazy_functions/高级功能函数模板.py +14 -5
- main.py +3 -2
- request_llm/bridge_chatgpt.py +13 -14
- request_llm/bridge_tgui.py +3 -3
- toolbox.py +37 -8
crazy_functions/Latex全文润色.py
CHANGED
|
@@ -3,7 +3,7 @@ from toolbox import CatchException, report_execption, write_results_to_file, pre
|
|
| 3 |
fast_debug = False
|
| 4 |
|
| 5 |
|
| 6 |
-
def 解析Paper(file_manifest, project_folder,
|
| 7 |
import time, glob, os
|
| 8 |
print('begin analysis on:', file_manifest)
|
| 9 |
for index, fp in enumerate(file_manifest):
|
|
@@ -14,39 +14,39 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|
| 14 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 15 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 16 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 17 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 18 |
|
| 19 |
if not fast_debug:
|
| 20 |
msg = '正常'
|
| 21 |
# ** gpt request **
|
| 22 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 23 |
|
| 24 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 25 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 26 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 27 |
if not fast_debug: time.sleep(2)
|
| 28 |
|
| 29 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 30 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 31 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 32 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 33 |
|
| 34 |
if not fast_debug:
|
| 35 |
msg = '正常'
|
| 36 |
# ** gpt request **
|
| 37 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot,
|
| 38 |
|
| 39 |
chatbot[-1] = (i_say, gpt_say)
|
| 40 |
history.append(i_say); history.append(gpt_say)
|
| 41 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 42 |
res = write_results_to_file(history)
|
| 43 |
chatbot.append(("完成了吗?", res))
|
| 44 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@CatchException
|
| 49 |
-
def 读文章写摘要(txt,
|
| 50 |
history = [] # 清空历史,以免输入溢出
|
| 51 |
import glob, os
|
| 52 |
if os.path.exists(txt):
|
|
@@ -54,13 +54,13 @@ def 读文章写摘要(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
| 54 |
else:
|
| 55 |
if txt == "": txt = '空空如也的输入栏'
|
| 56 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 57 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 58 |
return
|
| 59 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] # + \
|
| 60 |
# [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
|
| 61 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 62 |
if len(file_manifest) == 0:
|
| 63 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
| 64 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 65 |
return
|
| 66 |
-
yield from 解析Paper(file_manifest, project_folder,
|
|
|
|
| 3 |
fast_debug = False
|
| 4 |
|
| 5 |
|
| 6 |
+
def 解析Paper(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 7 |
import time, glob, os
|
| 8 |
print('begin analysis on:', file_manifest)
|
| 9 |
for index, fp in enumerate(file_manifest):
|
|
|
|
| 14 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 15 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 16 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 17 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 18 |
|
| 19 |
if not fast_debug:
|
| 20 |
msg = '正常'
|
| 21 |
# ** gpt request **
|
| 22 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 23 |
|
| 24 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 25 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 26 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 27 |
if not fast_debug: time.sleep(2)
|
| 28 |
|
| 29 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 30 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 31 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 32 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 33 |
|
| 34 |
if not fast_debug:
|
| 35 |
msg = '正常'
|
| 36 |
# ** gpt request **
|
| 37 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, llm_kwargs, plugin_kwargs, history=history) # 带超时倒计时
|
| 38 |
|
| 39 |
chatbot[-1] = (i_say, gpt_say)
|
| 40 |
history.append(i_say); history.append(gpt_say)
|
| 41 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 42 |
res = write_results_to_file(history)
|
| 43 |
chatbot.append(("完成了吗?", res))
|
| 44 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@CatchException
|
| 49 |
+
def 读文章写摘要(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 50 |
history = [] # 清空历史,以免输入溢出
|
| 51 |
import glob, os
|
| 52 |
if os.path.exists(txt):
|
|
|
|
| 54 |
else:
|
| 55 |
if txt == "": txt = '空空如也的输入栏'
|
| 56 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 57 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 58 |
return
|
| 59 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] # + \
|
| 60 |
# [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
|
| 61 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 62 |
if len(file_manifest) == 0:
|
| 63 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
| 64 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 65 |
return
|
| 66 |
+
yield from 解析Paper(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
crazy_functions/crazy_utils.py
CHANGED
|
@@ -37,7 +37,7 @@ def input_clipping(inputs, history, max_token_limit):
|
|
| 37 |
return inputs, history
|
| 38 |
|
| 39 |
def request_gpt_model_in_new_thread_with_ui_alive(
|
| 40 |
-
inputs, inputs_show_user,
|
| 41 |
chatbot, history, sys_prompt, refresh_interval=0.2,
|
| 42 |
handle_token_exceed=True,
|
| 43 |
retry_times_at_unknown_error=2,
|
|
@@ -66,7 +66,7 @@ def request_gpt_model_in_new_thread_with_ui_alive(
|
|
| 66 |
# 用户反馈
|
| 67 |
chatbot.append([inputs_show_user, ""])
|
| 68 |
msg = '正常'
|
| 69 |
-
yield from update_ui(chatbot=chatbot, history=[])
|
| 70 |
executor = ThreadPoolExecutor(max_workers=16)
|
| 71 |
mutable = ["", time.time()]
|
| 72 |
def _req_gpt(inputs, history, sys_prompt):
|
|
@@ -76,7 +76,7 @@ def request_gpt_model_in_new_thread_with_ui_alive(
|
|
| 76 |
try:
|
| 77 |
# 【第一种情况】:顺利完成
|
| 78 |
result = predict_no_ui_long_connection(
|
| 79 |
-
inputs=inputs,
|
| 80 |
history=history, sys_prompt=sys_prompt, observe_window=mutable)
|
| 81 |
return result
|
| 82 |
except ConnectionAbortedError as token_exceeded_error:
|
|
@@ -118,12 +118,12 @@ def request_gpt_model_in_new_thread_with_ui_alive(
|
|
| 118 |
if future.done():
|
| 119 |
break
|
| 120 |
chatbot[-1] = [chatbot[-1][0], mutable[0]]
|
| 121 |
-
yield from update_ui(chatbot=chatbot, history=[])
|
| 122 |
return future.result()
|
| 123 |
|
| 124 |
|
| 125 |
def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
| 126 |
-
inputs_array, inputs_show_user_array,
|
| 127 |
chatbot, history_array, sys_prompt_array,
|
| 128 |
refresh_interval=0.2, max_workers=10, scroller_max_len=30,
|
| 129 |
handle_token_exceed=True, show_user_at_complete=False,
|
|
@@ -141,8 +141,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
| 141 |
输入参数 Args (以_array结尾的输入变量都是列表,列表长度为子任务的数量,执行时,会把列表拆解,放到每个子线程中分别执行):
|
| 142 |
inputs_array (list): List of inputs (每个子任务的输入)
|
| 143 |
inputs_show_user_array (list): List of inputs to show user(每个子任务展现在报告中的输入,借助此参数,在汇总报告中隐藏啰嗦的真实输入,增强报告的可读性)
|
| 144 |
-
|
| 145 |
-
temperature (float): Temperature value for sampling from model distribution(GPT参数,浮点数)
|
| 146 |
chatbot: chatbot (用户界面对话窗口句柄,用于数据流可视化)
|
| 147 |
history_array (list): List of chat history (历史对话输入,双层列表,第一层列表是子任务分解,第二层列表是对话历史)
|
| 148 |
sys_prompt_array (list): List of system prompts (系统输入,列表,用于输入给GPT的前提提示,比如你是翻译官怎样怎样)
|
|
@@ -167,7 +166,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
| 167 |
# 用户反馈
|
| 168 |
chatbot.append(["请开始多线程操作。", ""])
|
| 169 |
msg = '正常'
|
| 170 |
-
yield from update_ui(chatbot=chatbot, history=[])
|
| 171 |
# 异步原子
|
| 172 |
mutable = [["", time.time(), "等待中"] for _ in range(n_frag)]
|
| 173 |
|
|
@@ -181,7 +180,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
| 181 |
# 【第一种情况】:顺利完成
|
| 182 |
# time.sleep(10); raise RuntimeError("测试")
|
| 183 |
gpt_say = predict_no_ui_long_connection(
|
| 184 |
-
inputs=inputs,
|
| 185 |
sys_prompt=sys_prompt, observe_window=mutable[index], console_slience=True
|
| 186 |
)
|
| 187 |
mutable[index][2] = "已成功"
|
|
@@ -253,7 +252,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
| 253 |
for thread_index, done, obs in zip(range(len(worker_done)), worker_done, observe_win)])
|
| 254 |
chatbot[-1] = [chatbot[-1][0], f'多线程操作已经开始,完成情况: \n\n{stat_str}' + ''.join(['.']*(cnt % 10+1))]
|
| 255 |
msg = "正常"
|
| 256 |
-
yield from update_ui(chatbot=chatbot, history=[])
|
| 257 |
# 异步任务结束
|
| 258 |
gpt_response_collection = []
|
| 259 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
|
@@ -264,7 +263,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
| 264 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
| 265 |
gpt_res = f.result()
|
| 266 |
chatbot.append([inputs_show_user, gpt_res])
|
| 267 |
-
yield from update_ui(chatbot=chatbot, history=[])
|
| 268 |
time.sleep(1)
|
| 269 |
return gpt_response_collection
|
| 270 |
|
|
|
|
| 37 |
return inputs, history
|
| 38 |
|
| 39 |
def request_gpt_model_in_new_thread_with_ui_alive(
|
| 40 |
+
inputs, inputs_show_user, llm_kwargs,
|
| 41 |
chatbot, history, sys_prompt, refresh_interval=0.2,
|
| 42 |
handle_token_exceed=True,
|
| 43 |
retry_times_at_unknown_error=2,
|
|
|
|
| 66 |
# 用户反馈
|
| 67 |
chatbot.append([inputs_show_user, ""])
|
| 68 |
msg = '正常'
|
| 69 |
+
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
| 70 |
executor = ThreadPoolExecutor(max_workers=16)
|
| 71 |
mutable = ["", time.time()]
|
| 72 |
def _req_gpt(inputs, history, sys_prompt):
|
|
|
|
| 76 |
try:
|
| 77 |
# 【第一种情况】:顺利完成
|
| 78 |
result = predict_no_ui_long_connection(
|
| 79 |
+
inputs=inputs, llm_kwargs=llm_kwargs,
|
| 80 |
history=history, sys_prompt=sys_prompt, observe_window=mutable)
|
| 81 |
return result
|
| 82 |
except ConnectionAbortedError as token_exceeded_error:
|
|
|
|
| 118 |
if future.done():
|
| 119 |
break
|
| 120 |
chatbot[-1] = [chatbot[-1][0], mutable[0]]
|
| 121 |
+
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
| 122 |
return future.result()
|
| 123 |
|
| 124 |
|
| 125 |
def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
| 126 |
+
inputs_array, inputs_show_user_array, llm_kwargs,
|
| 127 |
chatbot, history_array, sys_prompt_array,
|
| 128 |
refresh_interval=0.2, max_workers=10, scroller_max_len=30,
|
| 129 |
handle_token_exceed=True, show_user_at_complete=False,
|
|
|
|
| 141 |
输入参数 Args (以_array结尾的输入变量都是列表,列表长度为子任务的数量,执行时,会把列表拆解,放到每个子线程中分别执行):
|
| 142 |
inputs_array (list): List of inputs (每个子任务的输入)
|
| 143 |
inputs_show_user_array (list): List of inputs to show user(每个子任务展现在报告中的输入,借助此参数,在汇总报告中隐藏啰嗦的真实输入,增强报告的可读性)
|
| 144 |
+
llm_kwargs: llm_kwargs参数
|
|
|
|
| 145 |
chatbot: chatbot (用户界面对话窗口句柄,用于数据流可视化)
|
| 146 |
history_array (list): List of chat history (历史对话输入,双层列表,第一层列表是子任务分解,第二层列表是对话历史)
|
| 147 |
sys_prompt_array (list): List of system prompts (系统输入,列表,用于输入给GPT的前提提示,比如你是翻译官怎样怎样)
|
|
|
|
| 166 |
# 用户反馈
|
| 167 |
chatbot.append(["请开始多线程操作。", ""])
|
| 168 |
msg = '正常'
|
| 169 |
+
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
| 170 |
# 异步原子
|
| 171 |
mutable = [["", time.time(), "等待中"] for _ in range(n_frag)]
|
| 172 |
|
|
|
|
| 180 |
# 【第一种情况】:顺利完成
|
| 181 |
# time.sleep(10); raise RuntimeError("测试")
|
| 182 |
gpt_say = predict_no_ui_long_connection(
|
| 183 |
+
inputs=inputs, llm_kwargs=llm_kwargs, history=history,
|
| 184 |
sys_prompt=sys_prompt, observe_window=mutable[index], console_slience=True
|
| 185 |
)
|
| 186 |
mutable[index][2] = "已成功"
|
|
|
|
| 252 |
for thread_index, done, obs in zip(range(len(worker_done)), worker_done, observe_win)])
|
| 253 |
chatbot[-1] = [chatbot[-1][0], f'多线程操作已经开始,完成情况: \n\n{stat_str}' + ''.join(['.']*(cnt % 10+1))]
|
| 254 |
msg = "正常"
|
| 255 |
+
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
| 256 |
# 异步任务结束
|
| 257 |
gpt_response_collection = []
|
| 258 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
|
|
|
| 263 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
| 264 |
gpt_res = f.result()
|
| 265 |
chatbot.append([inputs_show_user, gpt_res])
|
| 266 |
+
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
| 267 |
time.sleep(1)
|
| 268 |
return gpt_response_collection
|
| 269 |
|
crazy_functions/下载arxiv论文翻译摘要.py
CHANGED
|
@@ -133,7 +133,7 @@ def get_name(_url_):
|
|
| 133 |
|
| 134 |
|
| 135 |
@CatchException
|
| 136 |
-
def 下载arxiv论文并翻译摘要(txt,
|
| 137 |
|
| 138 |
CRAZY_FUNCTION_INFO = "下载arxiv论文并翻译摘要,函数插件作者[binary-husky]。正在提取摘要并下载PDF文档……"
|
| 139 |
import glob
|
|
@@ -141,7 +141,7 @@ def 下载arxiv论文并翻译摘要(txt, top_p, temperature, chatbot, history,
|
|
| 141 |
|
| 142 |
# 基本信息:功能、贡献者
|
| 143 |
chatbot.append(["函数插件功能?", CRAZY_FUNCTION_INFO])
|
| 144 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 145 |
|
| 146 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 147 |
try:
|
|
@@ -150,7 +150,7 @@ def 下载arxiv论文并翻译摘要(txt, top_p, temperature, chatbot, history,
|
|
| 150 |
report_execption(chatbot, history,
|
| 151 |
a = f"解析项目: {txt}",
|
| 152 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pdfminer beautifulsoup4```。")
|
| 153 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 154 |
return
|
| 155 |
|
| 156 |
# 清空历史,以免输入溢出
|
|
@@ -163,25 +163,25 @@ def 下载arxiv论文并翻译摘要(txt, top_p, temperature, chatbot, history,
|
|
| 163 |
report_execption(chatbot, history,
|
| 164 |
a = f"解析项目: {txt}",
|
| 165 |
b = f"下载pdf文件未成功")
|
| 166 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 167 |
return
|
| 168 |
|
| 169 |
# 翻译摘要等
|
| 170 |
i_say = f"请你阅读以下学术论文相关的材料,提取摘要,翻译为中文。材料如下:{str(info)}"
|
| 171 |
i_say_show_user = f'请你阅读以下学术论文相关的材料,提取摘要,翻译为中文。论文:{pdf_path}'
|
| 172 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 173 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 174 |
msg = '正常'
|
| 175 |
# ** gpt request **
|
| 176 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 177 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 178 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 179 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 180 |
# 写入文件
|
| 181 |
import shutil
|
| 182 |
# 重置文件的创建时间
|
| 183 |
shutil.copyfile(pdf_path, f'./gpt_log/{os.path.basename(pdf_path)}'); os.remove(pdf_path)
|
| 184 |
res = write_results_to_file(history)
|
| 185 |
chatbot.append(("完成了吗?", res + "\n\nPDF文件也已经下载"))
|
| 186 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 187 |
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
@CatchException
|
| 136 |
+
def 下载arxiv论文并翻译摘要(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 137 |
|
| 138 |
CRAZY_FUNCTION_INFO = "下载arxiv论文并翻译摘要,函数插件作者[binary-husky]。正在提取摘要并下载PDF文档……"
|
| 139 |
import glob
|
|
|
|
| 141 |
|
| 142 |
# 基本信息:功能、贡献者
|
| 143 |
chatbot.append(["函数插件功能?", CRAZY_FUNCTION_INFO])
|
| 144 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 145 |
|
| 146 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 147 |
try:
|
|
|
|
| 150 |
report_execption(chatbot, history,
|
| 151 |
a = f"解析项目: {txt}",
|
| 152 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pdfminer beautifulsoup4```。")
|
| 153 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 154 |
return
|
| 155 |
|
| 156 |
# 清空历史,以免输入溢出
|
|
|
|
| 163 |
report_execption(chatbot, history,
|
| 164 |
a = f"解析项目: {txt}",
|
| 165 |
b = f"下载pdf文件未成功")
|
| 166 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 167 |
return
|
| 168 |
|
| 169 |
# 翻译摘要等
|
| 170 |
i_say = f"请你阅读以下学术论文相关的材料,提取摘要,翻译为中文。材料如下:{str(info)}"
|
| 171 |
i_say_show_user = f'请你阅读以下学术论文相关的材料,提取摘要,翻译为中文。论文:{pdf_path}'
|
| 172 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 173 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 174 |
msg = '正常'
|
| 175 |
# ** gpt request **
|
| 176 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 177 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 178 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 179 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 180 |
# 写入文件
|
| 181 |
import shutil
|
| 182 |
# 重置文件的创建时间
|
| 183 |
shutil.copyfile(pdf_path, f'./gpt_log/{os.path.basename(pdf_path)}'); os.remove(pdf_path)
|
| 184 |
res = write_results_to_file(history)
|
| 185 |
chatbot.append(("完成了吗?", res + "\n\nPDF文件也已经下载"))
|
| 186 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 187 |
|
crazy_functions/代码重写为全英文_多线程.py
CHANGED
|
@@ -23,7 +23,7 @@ def break_txt_into_half_at_some_linebreak(txt):
|
|
| 23 |
|
| 24 |
|
| 25 |
@CatchException
|
| 26 |
-
def 全项目切换英文(txt,
|
| 27 |
# 第1步:清空历史,以免输入溢出
|
| 28 |
history = []
|
| 29 |
|
|
@@ -34,7 +34,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 34 |
report_execption(chatbot, history,
|
| 35 |
a = f"解析项目: {txt}",
|
| 36 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade openai transformers```。")
|
| 37 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 38 |
return
|
| 39 |
|
| 40 |
# 第3步:集合文件
|
|
@@ -54,7 +54,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 54 |
i_say_show_user =f'[{index}/{len(file_manifest)}] 接下来请将以下代码中包含的所有中文转化为英文,只输出转化后的英文代码,请用代码块输出代码: {os.path.abspath(fp)}'
|
| 55 |
i_say_show_user_buffer.append(i_say_show_user)
|
| 56 |
chatbot.append((i_say_show_user, "[Local Message] 等待多线程操作,中间过程不予显示."))
|
| 57 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 58 |
|
| 59 |
|
| 60 |
# 第5步:Token限制下的截断与处理
|
|
@@ -82,7 +82,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 82 |
for file_content_partial in file_content_breakdown:
|
| 83 |
i_say = i_say_template(fp, file_content_partial)
|
| 84 |
# # ** gpt request **
|
| 85 |
-
gpt_say_partial = predict_no_ui_long_connection(inputs=i_say,
|
| 86 |
gpt_say_partial = extract_code_block_carefully(gpt_say_partial)
|
| 87 |
gpt_say += gpt_say_partial
|
| 88 |
mutable_return[index] = gpt_say
|
|
@@ -97,7 +97,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 97 |
h.daemon = True
|
| 98 |
h.start()
|
| 99 |
chatbot.append(('开始了吗?', f'多线程操作已经开始'))
|
| 100 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 101 |
|
| 102 |
# 第8步:循环轮询各个线程是否执行完毕
|
| 103 |
cnt = 0
|
|
@@ -113,7 +113,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 113 |
stat = [f'执行中: {obs}\n\n' if alive else '已完成\n\n' for alive, obs in zip(th_alive, observe_win)]
|
| 114 |
stat_str = ''.join(stat)
|
| 115 |
chatbot[-1] = (chatbot[-1][0], f'多线程操作已经开始,完成情况: \n\n{stat_str}' + ''.join(['.']*(cnt%10+1)))
|
| 116 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 117 |
|
| 118 |
# 第9步:把结果写入文件
|
| 119 |
for index, h in enumerate(handles):
|
|
@@ -130,10 +130,10 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 130 |
shutil.copyfile(file_manifest[index], where_to_relocate)
|
| 131 |
chatbot.append((i_say_show_user, f'[Local Message] 已完成{os.path.abspath(fp)}的转化,\n\n存入{os.path.abspath(where_to_relocate)}'))
|
| 132 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 133 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 134 |
time.sleep(1)
|
| 135 |
|
| 136 |
# 第10步:备份一个文件
|
| 137 |
res = write_results_to_file(history)
|
| 138 |
chatbot.append(("生成一份任务执行报告", res))
|
| 139 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
@CatchException
|
| 26 |
+
def 全项目切换英文(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt, web_port):
|
| 27 |
# 第1步:清空历史,以免输入溢出
|
| 28 |
history = []
|
| 29 |
|
|
|
|
| 34 |
report_execption(chatbot, history,
|
| 35 |
a = f"解析项目: {txt}",
|
| 36 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade openai transformers```。")
|
| 37 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 38 |
return
|
| 39 |
|
| 40 |
# 第3步:集合文件
|
|
|
|
| 54 |
i_say_show_user =f'[{index}/{len(file_manifest)}] 接下来请将以下代码中包含的所有中文转化为英文,只输出转化后的英文代码,请用代码块输出代码: {os.path.abspath(fp)}'
|
| 55 |
i_say_show_user_buffer.append(i_say_show_user)
|
| 56 |
chatbot.append((i_say_show_user, "[Local Message] 等待多线程操作,中间过程不予显示."))
|
| 57 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 58 |
|
| 59 |
|
| 60 |
# 第5步:Token限制下的截断与处理
|
|
|
|
| 82 |
for file_content_partial in file_content_breakdown:
|
| 83 |
i_say = i_say_template(fp, file_content_partial)
|
| 84 |
# # ** gpt request **
|
| 85 |
+
gpt_say_partial = predict_no_ui_long_connection(inputs=i_say, llm_kwargs=llm_kwargs, history=[], sys_prompt=sys_prompt, observe_window=observe_window[index])
|
| 86 |
gpt_say_partial = extract_code_block_carefully(gpt_say_partial)
|
| 87 |
gpt_say += gpt_say_partial
|
| 88 |
mutable_return[index] = gpt_say
|
|
|
|
| 97 |
h.daemon = True
|
| 98 |
h.start()
|
| 99 |
chatbot.append(('开始了吗?', f'多线程操作已经开始'))
|
| 100 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 101 |
|
| 102 |
# 第8步:循环轮询各个线程是否执行完毕
|
| 103 |
cnt = 0
|
|
|
|
| 113 |
stat = [f'执行中: {obs}\n\n' if alive else '已完成\n\n' for alive, obs in zip(th_alive, observe_win)]
|
| 114 |
stat_str = ''.join(stat)
|
| 115 |
chatbot[-1] = (chatbot[-1][0], f'多线程操作已经开始,完成情况: \n\n{stat_str}' + ''.join(['.']*(cnt%10+1)))
|
| 116 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 117 |
|
| 118 |
# 第9步:把结果写入文件
|
| 119 |
for index, h in enumerate(handles):
|
|
|
|
| 130 |
shutil.copyfile(file_manifest[index], where_to_relocate)
|
| 131 |
chatbot.append((i_say_show_user, f'[Local Message] 已完成{os.path.abspath(fp)}的转化,\n\n存入{os.path.abspath(where_to_relocate)}'))
|
| 132 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 133 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 134 |
time.sleep(1)
|
| 135 |
|
| 136 |
# 第10步:备份一个文件
|
| 137 |
res = write_results_to_file(history)
|
| 138 |
chatbot.append(("生成一份任务执行报告", res))
|
| 139 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
crazy_functions/总结word文档.py
CHANGED
|
@@ -4,7 +4,7 @@ from toolbox import CatchException, report_execption, write_results_to_file, pre
|
|
| 4 |
fast_debug = False
|
| 5 |
|
| 6 |
|
| 7 |
-
def 解析docx(file_manifest, project_folder,
|
| 8 |
import time, os
|
| 9 |
# pip install python-docx 用于docx格式,跨平台
|
| 10 |
# pip install pywin32 用于doc格式,仅支持Win平台
|
|
@@ -36,58 +36,58 @@ def 解析docx(file_manifest, project_folder, top_p, temperature, chatbot, histo
|
|
| 36 |
f'文章内容是 ```{file_content}```'
|
| 37 |
i_say_show_user = prefix + f'[{index+1}/{len(file_manifest)}] 假设你是论文审稿专家,请对下面的文章片段做概述: {os.path.abspath(fp)}'
|
| 38 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 39 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 40 |
|
| 41 |
if not fast_debug:
|
| 42 |
msg = '正常'
|
| 43 |
# ** gpt request **
|
| 44 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 45 |
history=[]) # 带超时倒计时
|
| 46 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 47 |
history.append(i_say_show_user);
|
| 48 |
history.append(gpt_say)
|
| 49 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 50 |
if not fast_debug: time.sleep(2)
|
| 51 |
|
| 52 |
"""
|
| 53 |
# 可按需启用
|
| 54 |
i_say = f'根据你上述的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一篇英文的。'
|
| 55 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 56 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 57 |
|
| 58 |
|
| 59 |
i_say = f'我想让你做一个论文写作导师。您的任务是使用人工智能工具(例如自然语言处理)提供有关如何改进其上述文章的反馈。' \
|
| 60 |
f'您还应该利用您在有效写作技巧方面的修辞知识和经验来建议作者可以更好地以书面形式表达他们的想法和想法的方法。' \
|
| 61 |
f'根据你之前的分析,提出建议'
|
| 62 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 63 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 64 |
|
| 65 |
"""
|
| 66 |
|
| 67 |
if not fast_debug:
|
| 68 |
msg = '正常'
|
| 69 |
# ** gpt request **
|
| 70 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot,
|
| 71 |
history=history) # 带超时倒计时
|
| 72 |
|
| 73 |
chatbot[-1] = (i_say, gpt_say)
|
| 74 |
history.append(i_say)
|
| 75 |
history.append(gpt_say)
|
| 76 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 77 |
res = write_results_to_file(history)
|
| 78 |
chatbot.append(("完成了吗?", res))
|
| 79 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 80 |
|
| 81 |
|
| 82 |
@CatchException
|
| 83 |
-
def 总结word文档(txt,
|
| 84 |
import glob, os
|
| 85 |
|
| 86 |
# 基本信息:功能、贡献者
|
| 87 |
chatbot.append([
|
| 88 |
"函数插件功能?",
|
| 89 |
"批量总结Word文档。函数插件贡献者: JasonGuo1"])
|
| 90 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 91 |
|
| 92 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 93 |
try:
|
|
@@ -96,7 +96,7 @@ def 总结word文档(txt, top_p, temperature, chatbot, history, systemPromptTxt,
|
|
| 96 |
report_execption(chatbot, history,
|
| 97 |
a=f"解析项目: {txt}",
|
| 98 |
b=f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade python-docx pywin32```。")
|
| 99 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 100 |
return
|
| 101 |
|
| 102 |
# 清空历史,以免输入溢出
|
|
@@ -108,7 +108,7 @@ def 总结word文档(txt, top_p, temperature, chatbot, history, systemPromptTxt,
|
|
| 108 |
else:
|
| 109 |
if txt == "": txt = '空空如也的输入栏'
|
| 110 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 111 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 112 |
return
|
| 113 |
|
| 114 |
# 搜索需要处理的文件清单
|
|
@@ -121,8 +121,8 @@ def 总结word文档(txt, top_p, temperature, chatbot, history, systemPromptTxt,
|
|
| 121 |
# 如果没找到任何文件
|
| 122 |
if len(file_manifest) == 0:
|
| 123 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何.docx或doc文件: {txt}")
|
| 124 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 125 |
return
|
| 126 |
|
| 127 |
# 开始正式执行任务
|
| 128 |
-
yield from 解析docx(file_manifest, project_folder,
|
|
|
|
| 4 |
fast_debug = False
|
| 5 |
|
| 6 |
|
| 7 |
+
def 解析docx(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 8 |
import time, os
|
| 9 |
# pip install python-docx 用于docx格式,跨平台
|
| 10 |
# pip install pywin32 用于doc格式,仅支持Win平台
|
|
|
|
| 36 |
f'文章内容是 ```{file_content}```'
|
| 37 |
i_say_show_user = prefix + f'[{index+1}/{len(file_manifest)}] 假设你是论文审稿专家,请对下面的文章片段做概述: {os.path.abspath(fp)}'
|
| 38 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 39 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 40 |
|
| 41 |
if not fast_debug:
|
| 42 |
msg = '正常'
|
| 43 |
# ** gpt request **
|
| 44 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs,
|
| 45 |
history=[]) # 带超时倒计时
|
| 46 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 47 |
history.append(i_say_show_user);
|
| 48 |
history.append(gpt_say)
|
| 49 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 50 |
if not fast_debug: time.sleep(2)
|
| 51 |
|
| 52 |
"""
|
| 53 |
# 可按需启用
|
| 54 |
i_say = f'根据你上述的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一篇英文的。'
|
| 55 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 56 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 57 |
|
| 58 |
|
| 59 |
i_say = f'我想让你做一个论文写作导师。您的任务是使用人工智能工具(例如自然语言处理)提供有关如何改进其上述文章的反馈。' \
|
| 60 |
f'您还应该利用您在有效写作技巧方面的修辞知识和经验来建议作者可以更好地以书面形式表达他们的想法和想法的方法。' \
|
| 61 |
f'根据你之前的分析,提出建议'
|
| 62 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 63 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 64 |
|
| 65 |
"""
|
| 66 |
|
| 67 |
if not fast_debug:
|
| 68 |
msg = '正常'
|
| 69 |
# ** gpt request **
|
| 70 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, llm_kwargs, plugin_kwargs,
|
| 71 |
history=history) # 带超时倒计时
|
| 72 |
|
| 73 |
chatbot[-1] = (i_say, gpt_say)
|
| 74 |
history.append(i_say)
|
| 75 |
history.append(gpt_say)
|
| 76 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 77 |
res = write_results_to_file(history)
|
| 78 |
chatbot.append(("完成了吗?", res))
|
| 79 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 80 |
|
| 81 |
|
| 82 |
@CatchException
|
| 83 |
+
def 总结word文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 84 |
import glob, os
|
| 85 |
|
| 86 |
# 基本信息:功能、贡献者
|
| 87 |
chatbot.append([
|
| 88 |
"函数插件功能?",
|
| 89 |
"批量总结Word文档。函数插件贡献者: JasonGuo1"])
|
| 90 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 91 |
|
| 92 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 93 |
try:
|
|
|
|
| 96 |
report_execption(chatbot, history,
|
| 97 |
a=f"解析项目: {txt}",
|
| 98 |
b=f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade python-docx pywin32```。")
|
| 99 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 100 |
return
|
| 101 |
|
| 102 |
# 清空历史,以免输入溢出
|
|
|
|
| 108 |
else:
|
| 109 |
if txt == "": txt = '空空如也的输入栏'
|
| 110 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 111 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 112 |
return
|
| 113 |
|
| 114 |
# 搜索需要处理的文件清单
|
|
|
|
| 121 |
# 如果没找到任何文件
|
| 122 |
if len(file_manifest) == 0:
|
| 123 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何.docx或doc文件: {txt}")
|
| 124 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 125 |
return
|
| 126 |
|
| 127 |
# 开始正式执行任务
|
| 128 |
+
yield from 解析docx(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
crazy_functions/批量总结PDF文档.py
CHANGED
|
@@ -58,7 +58,7 @@ def clean_text(raw_text):
|
|
| 58 |
|
| 59 |
return final_text.strip()
|
| 60 |
|
| 61 |
-
def 解析PDF(file_manifest, project_folder,
|
| 62 |
import time, glob, os, fitz
|
| 63 |
print('begin analysis on:', file_manifest)
|
| 64 |
for index, fp in enumerate(file_manifest):
|
|
@@ -73,45 +73,45 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|
| 73 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 74 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 75 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 76 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 77 |
|
| 78 |
if not fast_debug:
|
| 79 |
msg = '正常'
|
| 80 |
# ** gpt request **
|
| 81 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 82 |
|
| 83 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 84 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 85 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 86 |
if not fast_debug: time.sleep(2)
|
| 87 |
|
| 88 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 89 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 90 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 91 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 92 |
|
| 93 |
if not fast_debug:
|
| 94 |
msg = '正常'
|
| 95 |
# ** gpt request **
|
| 96 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot,
|
| 97 |
|
| 98 |
chatbot[-1] = (i_say, gpt_say)
|
| 99 |
history.append(i_say); history.append(gpt_say)
|
| 100 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 101 |
res = write_results_to_file(history)
|
| 102 |
chatbot.append(("完成了吗?", res))
|
| 103 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 104 |
|
| 105 |
|
| 106 |
@CatchException
|
| 107 |
-
def 批量总结PDF文档(txt,
|
| 108 |
import glob, os
|
| 109 |
|
| 110 |
# 基本信息:功能、贡献者
|
| 111 |
chatbot.append([
|
| 112 |
"函数插件功能?",
|
| 113 |
"批量总结PDF文档。函数插件贡献者: ValeriaWong,Eralien"])
|
| 114 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 115 |
|
| 116 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 117 |
try:
|
|
@@ -120,7 +120,7 @@ def 批量总结PDF文档(txt, top_p, temperature, chatbot, history, systemPromp
|
|
| 120 |
report_execption(chatbot, history,
|
| 121 |
a = f"解析项目: {txt}",
|
| 122 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf```。")
|
| 123 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 124 |
return
|
| 125 |
|
| 126 |
# 清空历史,以免输入溢出
|
|
@@ -132,7 +132,7 @@ def 批量总结PDF文档(txt, top_p, temperature, chatbot, history, systemPromp
|
|
| 132 |
else:
|
| 133 |
if txt == "": txt = '空空如也的输入栏'
|
| 134 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 135 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 136 |
return
|
| 137 |
|
| 138 |
# 搜索需要处理的文件清单
|
|
@@ -144,8 +144,8 @@ def 批量总结PDF文档(txt, top_p, temperature, chatbot, history, systemPromp
|
|
| 144 |
# 如果没找到任何文件
|
| 145 |
if len(file_manifest) == 0:
|
| 146 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex或.pdf文件: {txt}")
|
| 147 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 148 |
return
|
| 149 |
|
| 150 |
# 开始正式执行任务
|
| 151 |
-
yield from 解析PDF(file_manifest, project_folder,
|
|
|
|
| 58 |
|
| 59 |
return final_text.strip()
|
| 60 |
|
| 61 |
+
def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 62 |
import time, glob, os, fitz
|
| 63 |
print('begin analysis on:', file_manifest)
|
| 64 |
for index, fp in enumerate(file_manifest):
|
|
|
|
| 73 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 74 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 75 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 76 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 77 |
|
| 78 |
if not fast_debug:
|
| 79 |
msg = '正常'
|
| 80 |
# ** gpt request **
|
| 81 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 82 |
|
| 83 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 84 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 85 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 86 |
if not fast_debug: time.sleep(2)
|
| 87 |
|
| 88 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 89 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 90 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 91 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 92 |
|
| 93 |
if not fast_debug:
|
| 94 |
msg = '正常'
|
| 95 |
# ** gpt request **
|
| 96 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, llm_kwargs, plugin_kwargs, history=history) # 带超时倒计时
|
| 97 |
|
| 98 |
chatbot[-1] = (i_say, gpt_say)
|
| 99 |
history.append(i_say); history.append(gpt_say)
|
| 100 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 101 |
res = write_results_to_file(history)
|
| 102 |
chatbot.append(("完成了吗?", res))
|
| 103 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 104 |
|
| 105 |
|
| 106 |
@CatchException
|
| 107 |
+
def 批量总结PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 108 |
import glob, os
|
| 109 |
|
| 110 |
# 基本信息:功能、贡献者
|
| 111 |
chatbot.append([
|
| 112 |
"函数插件功能?",
|
| 113 |
"批量总结PDF文档。函数插件贡献者: ValeriaWong,Eralien"])
|
| 114 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 115 |
|
| 116 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 117 |
try:
|
|
|
|
| 120 |
report_execption(chatbot, history,
|
| 121 |
a = f"解析项目: {txt}",
|
| 122 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf```。")
|
| 123 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 124 |
return
|
| 125 |
|
| 126 |
# 清空历史,以免输入溢出
|
|
|
|
| 132 |
else:
|
| 133 |
if txt == "": txt = '空空如也的输入栏'
|
| 134 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 135 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 136 |
return
|
| 137 |
|
| 138 |
# 搜索需要处理的文件清单
|
|
|
|
| 144 |
# 如果没找到任何文件
|
| 145 |
if len(file_manifest) == 0:
|
| 146 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex或.pdf文件: {txt}")
|
| 147 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 148 |
return
|
| 149 |
|
| 150 |
# 开始正式执行任务
|
| 151 |
+
yield from 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
crazy_functions/批量总结PDF文档pdfminer.py
CHANGED
|
@@ -62,7 +62,7 @@ def readPdf(pdfPath):
|
|
| 62 |
return outTextList
|
| 63 |
|
| 64 |
|
| 65 |
-
def 解析Paper(file_manifest, project_folder,
|
| 66 |
import time, glob, os
|
| 67 |
from bs4 import BeautifulSoup
|
| 68 |
print('begin analysis on:', file_manifest)
|
|
@@ -78,39 +78,39 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|
| 78 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 79 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 80 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 81 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 82 |
|
| 83 |
if not fast_debug:
|
| 84 |
msg = '正常'
|
| 85 |
# ** gpt request **
|
| 86 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 87 |
|
| 88 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 89 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 90 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 91 |
if not fast_debug: time.sleep(2)
|
| 92 |
|
| 93 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 94 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 95 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 96 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 97 |
|
| 98 |
if not fast_debug:
|
| 99 |
msg = '正常'
|
| 100 |
# ** gpt request **
|
| 101 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot,
|
| 102 |
|
| 103 |
chatbot[-1] = (i_say, gpt_say)
|
| 104 |
history.append(i_say); history.append(gpt_say)
|
| 105 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 106 |
res = write_results_to_file(history)
|
| 107 |
chatbot.append(("完成了吗?", res))
|
| 108 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@CatchException
|
| 113 |
-
def 批量总结PDF文档pdfminer(txt,
|
| 114 |
history = [] # 清空历史,以免输入溢出
|
| 115 |
import glob, os
|
| 116 |
|
|
@@ -118,7 +118,7 @@ def 批量总结PDF文档pdfminer(txt, top_p, temperature, chatbot, history, sys
|
|
| 118 |
chatbot.append([
|
| 119 |
"函数插件功能?",
|
| 120 |
"批量总结PDF文档,此版本使用pdfminer插件,带token约简功能。函数插件贡献者: Euclid-Jie。"])
|
| 121 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 122 |
|
| 123 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 124 |
try:
|
|
@@ -127,14 +127,14 @@ def 批量总结PDF文档pdfminer(txt, top_p, temperature, chatbot, history, sys
|
|
| 127 |
report_execption(chatbot, history,
|
| 128 |
a = f"解析项目: {txt}",
|
| 129 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pdfminer beautifulsoup4```。")
|
| 130 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 131 |
return
|
| 132 |
if os.path.exists(txt):
|
| 133 |
project_folder = txt
|
| 134 |
else:
|
| 135 |
if txt == "": txt = '空空如也的输入栏'
|
| 136 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 137 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 138 |
return
|
| 139 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] + \
|
| 140 |
[f for f in glob.glob(f'{project_folder}/**/*.pdf', recursive=True)] # + \
|
|
@@ -142,7 +142,7 @@ def 批量总结PDF文档pdfminer(txt, top_p, temperature, chatbot, history, sys
|
|
| 142 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 143 |
if len(file_manifest) == 0:
|
| 144 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex或pdf文件: {txt}")
|
| 145 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 146 |
return
|
| 147 |
-
yield from 解析Paper(file_manifest, project_folder,
|
| 148 |
|
|
|
|
| 62 |
return outTextList
|
| 63 |
|
| 64 |
|
| 65 |
+
def 解析Paper(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 66 |
import time, glob, os
|
| 67 |
from bs4 import BeautifulSoup
|
| 68 |
print('begin analysis on:', file_manifest)
|
|
|
|
| 78 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 79 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 80 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 81 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 82 |
|
| 83 |
if not fast_debug:
|
| 84 |
msg = '正常'
|
| 85 |
# ** gpt request **
|
| 86 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 87 |
|
| 88 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 89 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 90 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 91 |
if not fast_debug: time.sleep(2)
|
| 92 |
|
| 93 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 94 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 95 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 96 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 97 |
|
| 98 |
if not fast_debug:
|
| 99 |
msg = '正常'
|
| 100 |
# ** gpt request **
|
| 101 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, llm_kwargs, plugin_kwargs, history=history) # 带超时倒计时
|
| 102 |
|
| 103 |
chatbot[-1] = (i_say, gpt_say)
|
| 104 |
history.append(i_say); history.append(gpt_say)
|
| 105 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 106 |
res = write_results_to_file(history)
|
| 107 |
chatbot.append(("完成了吗?", res))
|
| 108 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@CatchException
|
| 113 |
+
def 批量总结PDF文档pdfminer(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 114 |
history = [] # 清空历史,以免输入溢出
|
| 115 |
import glob, os
|
| 116 |
|
|
|
|
| 118 |
chatbot.append([
|
| 119 |
"函数插件功能?",
|
| 120 |
"批量总结PDF文档,此版本使用pdfminer插件,带token约简功能。函数插件贡献者: Euclid-Jie。"])
|
| 121 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 122 |
|
| 123 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 124 |
try:
|
|
|
|
| 127 |
report_execption(chatbot, history,
|
| 128 |
a = f"解析项目: {txt}",
|
| 129 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pdfminer beautifulsoup4```。")
|
| 130 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 131 |
return
|
| 132 |
if os.path.exists(txt):
|
| 133 |
project_folder = txt
|
| 134 |
else:
|
| 135 |
if txt == "": txt = '空空如也的输入栏'
|
| 136 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 137 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 138 |
return
|
| 139 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] + \
|
| 140 |
[f for f in glob.glob(f'{project_folder}/**/*.pdf', recursive=True)] # + \
|
|
|
|
| 142 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 143 |
if len(file_manifest) == 0:
|
| 144 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex或pdf文件: {txt}")
|
| 145 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 146 |
return
|
| 147 |
+
yield from 解析Paper(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 148 |
|
crazy_functions/批量翻译PDF文档_多线程.py
CHANGED
|
@@ -89,7 +89,7 @@ def read_and_clean_pdf_text(fp):
|
|
| 89 |
|
| 90 |
|
| 91 |
@CatchException
|
| 92 |
-
def 批量翻译PDF文档(txt,
|
| 93 |
import glob
|
| 94 |
import os
|
| 95 |
|
|
@@ -97,7 +97,7 @@ def 批量翻译PDF文档(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 97 |
chatbot.append([
|
| 98 |
"函数插件功能?",
|
| 99 |
"批量总结PDF文档。函数插件贡献者: Binary-Husky(二进制哈士奇)"])
|
| 100 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 101 |
|
| 102 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 103 |
try:
|
|
@@ -107,7 +107,7 @@ def 批量翻译PDF文档(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 107 |
report_execption(chatbot, history,
|
| 108 |
a=f"解析项目: {txt}",
|
| 109 |
b=f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf tiktoken```。")
|
| 110 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 111 |
return
|
| 112 |
|
| 113 |
# 清空历史,以免输入溢出
|
|
@@ -121,7 +121,7 @@ def 批量翻译PDF文档(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 121 |
txt = '空空如也的输入栏'
|
| 122 |
report_execption(chatbot, history,
|
| 123 |
a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 124 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 125 |
return
|
| 126 |
|
| 127 |
# 搜索需要处理的文件清单
|
|
@@ -132,14 +132,14 @@ def 批量翻译PDF文档(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|
| 132 |
if len(file_manifest) == 0:
|
| 133 |
report_execption(chatbot, history,
|
| 134 |
a=f"解析项目: {txt}", b=f"找不到任何.tex或.pdf文件: {txt}")
|
| 135 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 136 |
return
|
| 137 |
|
| 138 |
# 开始正式执行任务
|
| 139 |
-
yield from 解析PDF(file_manifest, project_folder,
|
| 140 |
|
| 141 |
|
| 142 |
-
def 解析PDF(file_manifest, project_folder,
|
| 143 |
import os
|
| 144 |
import tiktoken
|
| 145 |
TOKEN_LIMIT_PER_FRAGMENT = 1600
|
|
@@ -164,7 +164,7 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|
| 164 |
paper_meta_info = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 165 |
inputs=f"以下是一篇学术论文的基础信息,请从中提取出“标题”、“收录会议或期刊”、“作者”、“摘要”、“编号”、“作者邮箱”这六个部分。请用markdown格式输出,最后用中文翻译摘要部分。请提取:{paper_meta}",
|
| 166 |
inputs_show_user=f"请从{fp}中提取出“标题”、“收录会议或期刊”等基本信息。",
|
| 167 |
-
|
| 168 |
chatbot=chatbot, history=[],
|
| 169 |
sys_prompt="Your job is to collect information from materials。",
|
| 170 |
)
|
|
@@ -173,7 +173,7 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|
| 173 |
inputs_array=[
|
| 174 |
f"以下是你需要翻译的文章段落:\n{frag}" for frag in paper_fragments],
|
| 175 |
inputs_show_user_array=[f"" for _ in paper_fragments],
|
| 176 |
-
|
| 177 |
chatbot=chatbot,
|
| 178 |
history_array=[[paper_meta] for _ in paper_fragments],
|
| 179 |
sys_prompt_array=[
|
|
@@ -189,7 +189,7 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|
| 189 |
f'./gpt_log/{create_report_file_name}')
|
| 190 |
chatbot.append((f"{fp}完成了吗?", res))
|
| 191 |
msg = "完成"
|
| 192 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 193 |
|
| 194 |
# 准备文件的下载
|
| 195 |
import shutil
|
|
@@ -202,4 +202,4 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|
| 202 |
if os.path.exists(pdf_path):
|
| 203 |
os.remove(pdf_path)
|
| 204 |
chatbot.append(("给出输出文件清单", str(generated_conclusion_files)))
|
| 205 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
@CatchException
|
| 92 |
+
def 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt, web_port):
|
| 93 |
import glob
|
| 94 |
import os
|
| 95 |
|
|
|
|
| 97 |
chatbot.append([
|
| 98 |
"函数插件功能?",
|
| 99 |
"批量总结PDF文档。函数插件贡献者: Binary-Husky(二进制哈士奇)"])
|
| 100 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 101 |
|
| 102 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 103 |
try:
|
|
|
|
| 107 |
report_execption(chatbot, history,
|
| 108 |
a=f"解析项目: {txt}",
|
| 109 |
b=f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf tiktoken```。")
|
| 110 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 111 |
return
|
| 112 |
|
| 113 |
# 清空历史,以免输入溢出
|
|
|
|
| 121 |
txt = '空空如也的输入栏'
|
| 122 |
report_execption(chatbot, history,
|
| 123 |
a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 124 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 125 |
return
|
| 126 |
|
| 127 |
# 搜索需要处理的文件清单
|
|
|
|
| 132 |
if len(file_manifest) == 0:
|
| 133 |
report_execption(chatbot, history,
|
| 134 |
a=f"解析项目: {txt}", b=f"找不到任何.tex或.pdf文件: {txt}")
|
| 135 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 136 |
return
|
| 137 |
|
| 138 |
# 开始正式执行任务
|
| 139 |
+
yield from 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt)
|
| 140 |
|
| 141 |
|
| 142 |
+
def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt):
|
| 143 |
import os
|
| 144 |
import tiktoken
|
| 145 |
TOKEN_LIMIT_PER_FRAGMENT = 1600
|
|
|
|
| 164 |
paper_meta_info = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 165 |
inputs=f"以下是一篇学术论文的基础信息,请从中提取出“标题”、“收录会议或期刊”、“作者”、“摘要”、“编号”、“作者邮箱”这六个部分。请用markdown格式输出,最后用中文翻译摘要部分。请提取:{paper_meta}",
|
| 166 |
inputs_show_user=f"请从{fp}中提取出“标题”、“收录会议或期刊”等基本信息。",
|
| 167 |
+
llm_kwargs=llm_kwargs,
|
| 168 |
chatbot=chatbot, history=[],
|
| 169 |
sys_prompt="Your job is to collect information from materials。",
|
| 170 |
)
|
|
|
|
| 173 |
inputs_array=[
|
| 174 |
f"以下是你需要翻译的文章段落:\n{frag}" for frag in paper_fragments],
|
| 175 |
inputs_show_user_array=[f"" for _ in paper_fragments],
|
| 176 |
+
llm_kwargs=llm_kwargs,
|
| 177 |
chatbot=chatbot,
|
| 178 |
history_array=[[paper_meta] for _ in paper_fragments],
|
| 179 |
sys_prompt_array=[
|
|
|
|
| 189 |
f'./gpt_log/{create_report_file_name}')
|
| 190 |
chatbot.append((f"{fp}完成了吗?", res))
|
| 191 |
msg = "完成"
|
| 192 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 193 |
|
| 194 |
# 准备文件的下载
|
| 195 |
import shutil
|
|
|
|
| 202 |
if os.path.exists(pdf_path):
|
| 203 |
os.remove(pdf_path)
|
| 204 |
chatbot.append(("给出输出文件清单", str(generated_conclusion_files)))
|
| 205 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
crazy_functions/理解PDF文档内容.py
CHANGED
|
@@ -58,7 +58,7 @@ def clean_text(raw_text):
|
|
| 58 |
|
| 59 |
return final_text.strip()
|
| 60 |
|
| 61 |
-
def 解析PDF(file_name,
|
| 62 |
import time, glob, os, fitz
|
| 63 |
print('begin analysis on:', file_name)
|
| 64 |
|
|
@@ -82,37 +82,37 @@ def 解析PDF(file_name, top_p, temperature, chatbot, history, systemPromptTxt):
|
|
| 82 |
i_say = f'你只需要回答“接受完成”。文章内容第{i+1}/{split_group}部分是 ```{file_content[i*split_number:(i+1)*split_number]}```'
|
| 83 |
i_say_show_user = f'当前发送{i+1}/{split_group}部分'
|
| 84 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 85 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 86 |
while "完成" not in gpt_say:
|
| 87 |
i_say = f'你只需要回答“接受完成”。文章内容第{i+1}/{split_group}部分是 ```{file_content[i*split_number:(i+1)*split_number]}```'
|
| 88 |
i_say_show_user = f'出现error,重新发送{i+1}/{split_group}部分'
|
| 89 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 90 |
time.sleep(1)
|
| 91 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 92 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 93 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 94 |
time.sleep(2)
|
| 95 |
|
| 96 |
i_say = f'接下来,请你扮演一名专业的学术教授,利用你的所有知识并且结合这篇文章,回答我的问题。(请牢记:1.直到我说“退出”,你才能结束任务;2.所有问题需要紧密围绕文章内容;3.如果有公式,请使用tex渲染)'
|
| 97 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 98 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 99 |
|
| 100 |
# ** gpt request **
|
| 101 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot,
|
| 102 |
chatbot[-1] = (i_say, gpt_say)
|
| 103 |
history.append(i_say); history.append(gpt_say)
|
| 104 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 105 |
|
| 106 |
|
| 107 |
@CatchException
|
| 108 |
-
def 理解PDF文档内容(txt,
|
| 109 |
import glob, os
|
| 110 |
|
| 111 |
# 基本信息:功能、贡献者
|
| 112 |
chatbot.append([
|
| 113 |
"函数插件功能?",
|
| 114 |
"理解PDF论文内容,并且将结合上下文内容,进行学术解答。函数插件贡献者: Hanzoe。"])
|
| 115 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 116 |
|
| 117 |
import tkinter as tk
|
| 118 |
from tkinter import filedialog
|
|
@@ -128,26 +128,26 @@ def 理解PDF文档内容(txt, top_p, temperature, chatbot, history, systemPromp
|
|
| 128 |
report_execption(chatbot, history,
|
| 129 |
a = f"解析项目: {txt}",
|
| 130 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf```。")
|
| 131 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 132 |
return
|
| 133 |
|
| 134 |
# 清空历史,以免输入溢出
|
| 135 |
history = []
|
| 136 |
|
| 137 |
# 开始正式执行任务
|
| 138 |
-
yield from 解析PDF(txt,
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@CatchException
|
| 143 |
-
def 理解PDF文档内容标准文件输入(txt,
|
| 144 |
import glob, os
|
| 145 |
|
| 146 |
# 基本信息:功能、贡献者
|
| 147 |
chatbot.append([
|
| 148 |
"函数插件功能?",
|
| 149 |
"理解PDF论文内容,并且将结合上下文内容,进行学术解答。函数插件贡献者: Hanzoe。"])
|
| 150 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 151 |
|
| 152 |
# 尝试导入依赖,如果缺少依赖,���给出安装建议
|
| 153 |
try:
|
|
@@ -156,7 +156,7 @@ def 理解PDF文档内容标准文件输入(txt, top_p, temperature, chatbot, hi
|
|
| 156 |
report_execption(chatbot, history,
|
| 157 |
a = f"解析项目: {txt}",
|
| 158 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf```。")
|
| 159 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 160 |
return
|
| 161 |
|
| 162 |
# 清空历史,以免输入溢出
|
|
@@ -170,7 +170,7 @@ def 理解PDF文档内容标准文件输入(txt, top_p, temperature, chatbot, hi
|
|
| 170 |
txt = '空空如也的输入栏'
|
| 171 |
report_execption(chatbot, history,
|
| 172 |
a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 173 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 174 |
return
|
| 175 |
|
| 176 |
# 搜索需要处理的文件清单
|
|
@@ -179,8 +179,8 @@ def 理解PDF文档内容标准文件输入(txt, top_p, temperature, chatbot, hi
|
|
| 179 |
if len(file_manifest) == 0:
|
| 180 |
report_execption(chatbot, history,
|
| 181 |
a=f"解析项目: {txt}", b=f"找不到任何.tex或.pdf文件: {txt}")
|
| 182 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 183 |
return
|
| 184 |
txt = file_manifest[0]
|
| 185 |
# 开始正式执行任务
|
| 186 |
-
yield from 解析PDF(txt,
|
|
|
|
| 58 |
|
| 59 |
return final_text.strip()
|
| 60 |
|
| 61 |
+
def 解析PDF(file_name, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 62 |
import time, glob, os, fitz
|
| 63 |
print('begin analysis on:', file_name)
|
| 64 |
|
|
|
|
| 82 |
i_say = f'你只需要回答“接受完成”。文章内容第{i+1}/{split_group}部分是 ```{file_content[i*split_number:(i+1)*split_number]}```'
|
| 83 |
i_say_show_user = f'当前发送{i+1}/{split_group}部分'
|
| 84 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 85 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 86 |
while "完成" not in gpt_say:
|
| 87 |
i_say = f'你只需要回答“接受完成”。文章内容第{i+1}/{split_group}部分是 ```{file_content[i*split_number:(i+1)*split_number]}```'
|
| 88 |
i_say_show_user = f'出现error,重新发送{i+1}/{split_group}部分'
|
| 89 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 90 |
time.sleep(1)
|
| 91 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 92 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 93 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 94 |
time.sleep(2)
|
| 95 |
|
| 96 |
i_say = f'接下来,请你扮演一名专业的学术教授,利用你的所有知识并且结合这篇文章,回答我的问题。(请牢记:1.直到我说“退出”,你才能结束任务;2.所有问题需要紧密围绕文章内容;3.如果有公式,请使用tex渲染)'
|
| 97 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 98 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 99 |
|
| 100 |
# ** gpt request **
|
| 101 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, llm_kwargs, plugin_kwargs, history=history) # 带超时倒计时
|
| 102 |
chatbot[-1] = (i_say, gpt_say)
|
| 103 |
history.append(i_say); history.append(gpt_say)
|
| 104 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 105 |
|
| 106 |
|
| 107 |
@CatchException
|
| 108 |
+
def 理解PDF文档内容(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 109 |
import glob, os
|
| 110 |
|
| 111 |
# 基本信息:功能、贡献者
|
| 112 |
chatbot.append([
|
| 113 |
"函数插件功能?",
|
| 114 |
"理解PDF论文内容,并且将结合上下文内容,进行学术解答。函数插件贡献者: Hanzoe。"])
|
| 115 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 116 |
|
| 117 |
import tkinter as tk
|
| 118 |
from tkinter import filedialog
|
|
|
|
| 128 |
report_execption(chatbot, history,
|
| 129 |
a = f"解析项目: {txt}",
|
| 130 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf```。")
|
| 131 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 132 |
return
|
| 133 |
|
| 134 |
# 清空历史,以免输入溢出
|
| 135 |
history = []
|
| 136 |
|
| 137 |
# 开始正式执行任务
|
| 138 |
+
yield from 解析PDF(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@CatchException
|
| 143 |
+
def 理解PDF文档内容标准文件输入(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 144 |
import glob, os
|
| 145 |
|
| 146 |
# 基本信息:功能、贡献者
|
| 147 |
chatbot.append([
|
| 148 |
"函数插件功能?",
|
| 149 |
"理解PDF论文内容,并且将结合上下文内容,进行学术解答。函数插件贡献者: Hanzoe。"])
|
| 150 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 151 |
|
| 152 |
# 尝试导入依赖,如果缺少依赖,���给出安装建议
|
| 153 |
try:
|
|
|
|
| 156 |
report_execption(chatbot, history,
|
| 157 |
a = f"解析项目: {txt}",
|
| 158 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade pymupdf```。")
|
| 159 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 160 |
return
|
| 161 |
|
| 162 |
# 清空历史,以免输入溢出
|
|
|
|
| 170 |
txt = '空空如也的输入栏'
|
| 171 |
report_execption(chatbot, history,
|
| 172 |
a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 173 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 174 |
return
|
| 175 |
|
| 176 |
# 搜索需要处理的文件清单
|
|
|
|
| 179 |
if len(file_manifest) == 0:
|
| 180 |
report_execption(chatbot, history,
|
| 181 |
a=f"解析项目: {txt}", b=f"找不到任何.tex或.pdf文件: {txt}")
|
| 182 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 183 |
return
|
| 184 |
txt = file_manifest[0]
|
| 185 |
# 开始正式执行任务
|
| 186 |
+
yield from 解析PDF(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
crazy_functions/生成函数注释.py
CHANGED
|
@@ -4,7 +4,7 @@ from toolbox import CatchException, report_execption, write_results_to_file, pre
|
|
| 4 |
fast_debug = False
|
| 5 |
|
| 6 |
|
| 7 |
-
def 生成函数注释(file_manifest, project_folder,
|
| 8 |
import time, glob, os
|
| 9 |
print('begin analysis on:', file_manifest)
|
| 10 |
for index, fp in enumerate(file_manifest):
|
|
@@ -14,27 +14,27 @@ def 生成函数注释(file_manifest, project_folder, top_p, temperature, chatbo
|
|
| 14 |
i_say = f'请对下面的程序文件做一个概述,并对文件中的所有函数生成注释,使用markdown表格输出结果,文件名是{os.path.relpath(fp, project_folder)},文件内容是 ```{file_content}```'
|
| 15 |
i_say_show_user = f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述,并对文件中的所有函数生成注释: {os.path.abspath(fp)}'
|
| 16 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 17 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 18 |
|
| 19 |
if not fast_debug:
|
| 20 |
msg = '正常'
|
| 21 |
# ** gpt request **
|
| 22 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 23 |
|
| 24 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 25 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 26 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 27 |
if not fast_debug: time.sleep(2)
|
| 28 |
|
| 29 |
if not fast_debug:
|
| 30 |
res = write_results_to_file(history)
|
| 31 |
chatbot.append(("完成了吗?", res))
|
| 32 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
@CatchException
|
| 37 |
-
def 批量生成函数注释(txt,
|
| 38 |
history = [] # 清空历史,以免输入溢出
|
| 39 |
import glob, os
|
| 40 |
if os.path.exists(txt):
|
|
@@ -42,13 +42,13 @@ def 批量生成函数注释(txt, top_p, temperature, chatbot, history, systemPr
|
|
| 42 |
else:
|
| 43 |
if txt == "": txt = '空空如也的输入栏'
|
| 44 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 45 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 46 |
return
|
| 47 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.py', recursive=True)] + \
|
| 48 |
[f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)]
|
| 49 |
|
| 50 |
if len(file_manifest) == 0:
|
| 51 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
| 52 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 53 |
return
|
| 54 |
-
yield from 生成函数注释(file_manifest, project_folder,
|
|
|
|
| 4 |
fast_debug = False
|
| 5 |
|
| 6 |
|
| 7 |
+
def 生成函数注释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 8 |
import time, glob, os
|
| 9 |
print('begin analysis on:', file_manifest)
|
| 10 |
for index, fp in enumerate(file_manifest):
|
|
|
|
| 14 |
i_say = f'请对下面的程序文件做一个概述,并对文件中的所有函数生成注释,使用markdown表格输出结果,文件名是{os.path.relpath(fp, project_folder)},文件内容是 ```{file_content}```'
|
| 15 |
i_say_show_user = f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述,并对文件中的所有函数生成注释: {os.path.abspath(fp)}'
|
| 16 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 17 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 18 |
|
| 19 |
if not fast_debug:
|
| 20 |
msg = '正常'
|
| 21 |
# ** gpt request **
|
| 22 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 23 |
|
| 24 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 25 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 26 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 27 |
if not fast_debug: time.sleep(2)
|
| 28 |
|
| 29 |
if not fast_debug:
|
| 30 |
res = write_results_to_file(history)
|
| 31 |
chatbot.append(("完成了吗?", res))
|
| 32 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
@CatchException
|
| 37 |
+
def 批量生成函数注释(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 38 |
history = [] # 清空历史,以免输入溢出
|
| 39 |
import glob, os
|
| 40 |
if os.path.exists(txt):
|
|
|
|
| 42 |
else:
|
| 43 |
if txt == "": txt = '空空如也的输入栏'
|
| 44 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 45 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 46 |
return
|
| 47 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.py', recursive=True)] + \
|
| 48 |
[f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)]
|
| 49 |
|
| 50 |
if len(file_manifest) == 0:
|
| 51 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
| 52 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 53 |
return
|
| 54 |
+
yield from 生成函数注释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
crazy_functions/解析项目源代码.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from toolbox import update_ui
|
| 2 |
from toolbox import CatchException, report_execption, write_results_to_file, predict_no_ui_but_counting_down
|
| 3 |
|
| 4 |
-
def 解析源代码新(file_manifest, project_folder,
|
| 5 |
import os, copy
|
| 6 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
| 7 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive, WithRetry
|
|
@@ -30,8 +30,7 @@ def 解析源代码新(file_manifest, project_folder, top_p, temperature, chatbo
|
|
| 30 |
inputs_show_user_array = inputs_show_user_array,
|
| 31 |
history_array = history_array,
|
| 32 |
sys_prompt_array = sys_prompt_array,
|
| 33 |
-
|
| 34 |
-
temperature = temperature,
|
| 35 |
chatbot = chatbot,
|
| 36 |
show_user_at_complete = True
|
| 37 |
)
|
|
@@ -40,7 +39,7 @@ def 解析源代码新(file_manifest, project_folder, top_p, temperature, chatbo
|
|
| 40 |
history_to_return = report_part_1
|
| 41 |
res = write_results_to_file(report_part_1)
|
| 42 |
chatbot.append(("完成?", "逐个文件分析已完成。" + res + "\n\n正在开始汇总。"))
|
| 43 |
-
yield from update_ui(chatbot=chatbot, history=history_to_return)
|
| 44 |
|
| 45 |
############################## <第二步,综合,单线程,分组+迭代处理> ##################################
|
| 46 |
batchsize = 16 # 10个文件为一组
|
|
@@ -62,7 +61,7 @@ def 解析源代码新(file_manifest, project_folder, top_p, temperature, chatbo
|
|
| 62 |
this_iteration_history = copy.deepcopy(this_iteration_gpt_response_collection)
|
| 63 |
this_iteration_history.extend(report_part_2)
|
| 64 |
result = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 65 |
-
inputs=i_say, inputs_show_user=inputs_show_user,
|
| 66 |
history=this_iteration_history, # 迭代之前的分析
|
| 67 |
sys_prompt="你是一个程序架构分析师,正在分析一个源代码项目。")
|
| 68 |
report_part_2.extend([i_say, result])
|
|
@@ -74,11 +73,11 @@ def 解析源代码新(file_manifest, project_folder, top_p, temperature, chatbo
|
|
| 74 |
history_to_return.extend(report_part_2)
|
| 75 |
res = write_results_to_file(history_to_return)
|
| 76 |
chatbot.append(("完成了吗?", res))
|
| 77 |
-
yield from update_ui(chatbot=chatbot, history=history_to_return)
|
| 78 |
|
| 79 |
|
| 80 |
@CatchException
|
| 81 |
-
def 解析项目本身(txt,
|
| 82 |
history = [] # 清空历史,以免输入溢出
|
| 83 |
import glob
|
| 84 |
file_manifest = [f for f in glob.glob('./*.py') if ('test_project' not in f) and ('gpt_log' not in f)] + \
|
|
@@ -87,12 +86,12 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
| 87 |
project_folder = './'
|
| 88 |
if len(file_manifest) == 0:
|
| 89 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何python文件: {txt}")
|
| 90 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 91 |
return
|
| 92 |
-
yield from 解析源代码新(file_manifest, project_folder,
|
| 93 |
|
| 94 |
@CatchException
|
| 95 |
-
def 解析一个Python项目(txt,
|
| 96 |
history = [] # 清空历史,以免输入溢出
|
| 97 |
import glob, os
|
| 98 |
if os.path.exists(txt):
|
|
@@ -100,18 +99,18 @@ def 解析一个Python项目(txt, top_p, temperature, chatbot, history, systemPr
|
|
| 100 |
else:
|
| 101 |
if txt == "": txt = '空空如也的输入栏'
|
| 102 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 103 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 104 |
return
|
| 105 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.py', recursive=True)]
|
| 106 |
if len(file_manifest) == 0:
|
| 107 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何python文件: {txt}")
|
| 108 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 109 |
return
|
| 110 |
-
yield from 解析源代码新(file_manifest, project_folder,
|
| 111 |
|
| 112 |
|
| 113 |
@CatchException
|
| 114 |
-
def 解析一个C项目的头文件(txt,
|
| 115 |
history = [] # 清空历史,以免输入溢出
|
| 116 |
import glob, os
|
| 117 |
if os.path.exists(txt):
|
|
@@ -119,19 +118,19 @@ def 解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, s
|
|
| 119 |
else:
|
| 120 |
if txt == "": txt = '空空如也的输入栏'
|
| 121 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 122 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 123 |
return
|
| 124 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \
|
| 125 |
[f for f in glob.glob(f'{project_folder}/**/*.hpp', recursive=True)] #+ \
|
| 126 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 127 |
if len(file_manifest) == 0:
|
| 128 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
| 129 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 130 |
return
|
| 131 |
-
yield from 解析源代码新(file_manifest, project_folder,
|
| 132 |
|
| 133 |
@CatchException
|
| 134 |
-
def 解析一个C项目(txt,
|
| 135 |
history = [] # 清空历史,以免输入溢出
|
| 136 |
import glob, os
|
| 137 |
if os.path.exists(txt):
|
|
@@ -139,7 +138,7 @@ def 解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptT
|
|
| 139 |
else:
|
| 140 |
if txt == "": txt = '空空如也的输入栏'
|
| 141 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 142 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 143 |
return
|
| 144 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \
|
| 145 |
[f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
|
|
@@ -147,13 +146,13 @@ def 解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptT
|
|
| 147 |
[f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 148 |
if len(file_manifest) == 0:
|
| 149 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
| 150 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 151 |
return
|
| 152 |
-
yield from 解析源代码新(file_manifest, project_folder,
|
| 153 |
|
| 154 |
|
| 155 |
@CatchException
|
| 156 |
-
def 解析一个Java项目(txt,
|
| 157 |
history = [] # 清空历史,以免输入溢出
|
| 158 |
import glob, os
|
| 159 |
if os.path.exists(txt):
|
|
@@ -161,7 +160,7 @@ def 解析一个Java项目(txt, top_p, temperature, chatbot, history, systemProm
|
|
| 161 |
else:
|
| 162 |
if txt == "": txt = '空空如也的输入栏'
|
| 163 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 164 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 165 |
return
|
| 166 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.java', recursive=True)] + \
|
| 167 |
[f for f in glob.glob(f'{project_folder}/**/*.jar', recursive=True)] + \
|
|
@@ -169,13 +168,13 @@ def 解析一个Java项目(txt, top_p, temperature, chatbot, history, systemProm
|
|
| 169 |
[f for f in glob.glob(f'{project_folder}/**/*.sh', recursive=True)]
|
| 170 |
if len(file_manifest) == 0:
|
| 171 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何java文件: {txt}")
|
| 172 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 173 |
return
|
| 174 |
-
yield from
|
| 175 |
|
| 176 |
|
| 177 |
@CatchException
|
| 178 |
-
def 解析一个Rect项目(txt,
|
| 179 |
history = [] # 清空历史,以免输入溢出
|
| 180 |
import glob, os
|
| 181 |
if os.path.exists(txt):
|
|
@@ -183,7 +182,7 @@ def 解析一个Rect项目(txt, top_p, temperature, chatbot, history, systemProm
|
|
| 183 |
else:
|
| 184 |
if txt == "": txt = '空空如也的输入栏'
|
| 185 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 186 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 187 |
return
|
| 188 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.ts', recursive=True)] + \
|
| 189 |
[f for f in glob.glob(f'{project_folder}/**/*.tsx', recursive=True)] + \
|
|
@@ -192,13 +191,13 @@ def 解析一个Rect项目(txt, top_p, temperature, chatbot, history, systemProm
|
|
| 192 |
[f for f in glob.glob(f'{project_folder}/**/*.jsx', recursive=True)]
|
| 193 |
if len(file_manifest) == 0:
|
| 194 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何Rect文件: {txt}")
|
| 195 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 196 |
return
|
| 197 |
-
yield from 解析源代码新(file_manifest, project_folder,
|
| 198 |
|
| 199 |
|
| 200 |
@CatchException
|
| 201 |
-
def 解析一个Golang项目(txt,
|
| 202 |
history = [] # 清空历史,以免输入溢出
|
| 203 |
import glob, os
|
| 204 |
if os.path.exists(txt):
|
|
@@ -206,11 +205,11 @@ def 解析一个Golang项目(txt, top_p, temperature, chatbot, history, systemPr
|
|
| 206 |
else:
|
| 207 |
if txt == "": txt = '空空如也的输入栏'
|
| 208 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 209 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 210 |
return
|
| 211 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.go', recursive=True)]
|
| 212 |
if len(file_manifest) == 0:
|
| 213 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何golang文件: {txt}")
|
| 214 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 215 |
return
|
| 216 |
-
yield from 解析源代码新(file_manifest, project_folder,
|
|
|
|
| 1 |
from toolbox import update_ui
|
| 2 |
from toolbox import CatchException, report_execption, write_results_to_file, predict_no_ui_but_counting_down
|
| 3 |
|
| 4 |
+
def 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 5 |
import os, copy
|
| 6 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
| 7 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive, WithRetry
|
|
|
|
| 30 |
inputs_show_user_array = inputs_show_user_array,
|
| 31 |
history_array = history_array,
|
| 32 |
sys_prompt_array = sys_prompt_array,
|
| 33 |
+
llm_kwargs = llm_kwargs,
|
|
|
|
| 34 |
chatbot = chatbot,
|
| 35 |
show_user_at_complete = True
|
| 36 |
)
|
|
|
|
| 39 |
history_to_return = report_part_1
|
| 40 |
res = write_results_to_file(report_part_1)
|
| 41 |
chatbot.append(("完成?", "逐个文件分析已完成。" + res + "\n\n正在开始汇总。"))
|
| 42 |
+
yield from update_ui(chatbot=chatbot, history=history_to_return) # 刷新界面
|
| 43 |
|
| 44 |
############################## <第二步,综合,单线程,分组+迭代处理> ##################################
|
| 45 |
batchsize = 16 # 10个文件为一组
|
|
|
|
| 61 |
this_iteration_history = copy.deepcopy(this_iteration_gpt_response_collection)
|
| 62 |
this_iteration_history.extend(report_part_2)
|
| 63 |
result = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 64 |
+
inputs=i_say, inputs_show_user=inputs_show_user, llm_kwargs=llm_kwargs, chatbot=chatbot,
|
| 65 |
history=this_iteration_history, # 迭代之前的分析
|
| 66 |
sys_prompt="你是一个程序架构分析师,正在分析一个源代码项目。")
|
| 67 |
report_part_2.extend([i_say, result])
|
|
|
|
| 73 |
history_to_return.extend(report_part_2)
|
| 74 |
res = write_results_to_file(history_to_return)
|
| 75 |
chatbot.append(("完成了吗?", res))
|
| 76 |
+
yield from update_ui(chatbot=chatbot, history=history_to_return) # 刷新界面
|
| 77 |
|
| 78 |
|
| 79 |
@CatchException
|
| 80 |
+
def 解析项目本身(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 81 |
history = [] # 清空历史,以免输入溢出
|
| 82 |
import glob
|
| 83 |
file_manifest = [f for f in glob.glob('./*.py') if ('test_project' not in f) and ('gpt_log' not in f)] + \
|
|
|
|
| 86 |
project_folder = './'
|
| 87 |
if len(file_manifest) == 0:
|
| 88 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何python文件: {txt}")
|
| 89 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 90 |
return
|
| 91 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 92 |
|
| 93 |
@CatchException
|
| 94 |
+
def 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 95 |
history = [] # 清空历史,以免输入溢出
|
| 96 |
import glob, os
|
| 97 |
if os.path.exists(txt):
|
|
|
|
| 99 |
else:
|
| 100 |
if txt == "": txt = '空空如也的输入栏'
|
| 101 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 102 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 103 |
return
|
| 104 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.py', recursive=True)]
|
| 105 |
if len(file_manifest) == 0:
|
| 106 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何python文件: {txt}")
|
| 107 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 108 |
return
|
| 109 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 110 |
|
| 111 |
|
| 112 |
@CatchException
|
| 113 |
+
def 解析一个C项目的头文件(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 114 |
history = [] # 清空历史,以免输入溢出
|
| 115 |
import glob, os
|
| 116 |
if os.path.exists(txt):
|
|
|
|
| 118 |
else:
|
| 119 |
if txt == "": txt = '空空如也的输入栏'
|
| 120 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 121 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 122 |
return
|
| 123 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \
|
| 124 |
[f for f in glob.glob(f'{project_folder}/**/*.hpp', recursive=True)] #+ \
|
| 125 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 126 |
if len(file_manifest) == 0:
|
| 127 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
| 128 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 129 |
return
|
| 130 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 131 |
|
| 132 |
@CatchException
|
| 133 |
+
def 解析一个C项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 134 |
history = [] # 清空历史,以免输入溢出
|
| 135 |
import glob, os
|
| 136 |
if os.path.exists(txt):
|
|
|
|
| 138 |
else:
|
| 139 |
if txt == "": txt = '空空如也的输入栏'
|
| 140 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 141 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 142 |
return
|
| 143 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \
|
| 144 |
[f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
|
|
|
|
| 146 |
[f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 147 |
if len(file_manifest) == 0:
|
| 148 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
| 149 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 150 |
return
|
| 151 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 152 |
|
| 153 |
|
| 154 |
@CatchException
|
| 155 |
+
def 解析一个Java项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 156 |
history = [] # 清空历史,以免输入溢出
|
| 157 |
import glob, os
|
| 158 |
if os.path.exists(txt):
|
|
|
|
| 160 |
else:
|
| 161 |
if txt == "": txt = '空空如也的输入栏'
|
| 162 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 163 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 164 |
return
|
| 165 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.java', recursive=True)] + \
|
| 166 |
[f for f in glob.glob(f'{project_folder}/**/*.jar', recursive=True)] + \
|
|
|
|
| 168 |
[f for f in glob.glob(f'{project_folder}/**/*.sh', recursive=True)]
|
| 169 |
if len(file_manifest) == 0:
|
| 170 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何java文件: {txt}")
|
| 171 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 172 |
return
|
| 173 |
+
yield from 解析源代��新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 174 |
|
| 175 |
|
| 176 |
@CatchException
|
| 177 |
+
def 解析一个Rect项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 178 |
history = [] # 清空历史,以免输入溢出
|
| 179 |
import glob, os
|
| 180 |
if os.path.exists(txt):
|
|
|
|
| 182 |
else:
|
| 183 |
if txt == "": txt = '空空如也的输入栏'
|
| 184 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 185 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 186 |
return
|
| 187 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.ts', recursive=True)] + \
|
| 188 |
[f for f in glob.glob(f'{project_folder}/**/*.tsx', recursive=True)] + \
|
|
|
|
| 191 |
[f for f in glob.glob(f'{project_folder}/**/*.jsx', recursive=True)]
|
| 192 |
if len(file_manifest) == 0:
|
| 193 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何Rect文件: {txt}")
|
| 194 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 195 |
return
|
| 196 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
| 197 |
|
| 198 |
|
| 199 |
@CatchException
|
| 200 |
+
def 解析一个Golang项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 201 |
history = [] # 清空历史,以免输入溢出
|
| 202 |
import glob, os
|
| 203 |
if os.path.exists(txt):
|
|
|
|
| 205 |
else:
|
| 206 |
if txt == "": txt = '空空如也的输入栏'
|
| 207 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
| 208 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 209 |
return
|
| 210 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.go', recursive=True)]
|
| 211 |
if len(file_manifest) == 0:
|
| 212 |
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何golang文件: {txt}")
|
| 213 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 214 |
return
|
| 215 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
crazy_functions/读文章写摘要.py
CHANGED
|
@@ -4,7 +4,7 @@ from toolbox import CatchException, report_execption, write_results_to_file, pre
|
|
| 4 |
fast_debug = False
|
| 5 |
|
| 6 |
|
| 7 |
-
def 解析Paper(file_manifest, project_folder,
|
| 8 |
import time, glob, os
|
| 9 |
print('begin analysis on:', file_manifest)
|
| 10 |
for index, fp in enumerate(file_manifest):
|
|
@@ -15,39 +15,39 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|
| 15 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 16 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 17 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 18 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 19 |
|
| 20 |
if not fast_debug:
|
| 21 |
msg = '正常'
|
| 22 |
# ** gpt request **
|
| 23 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot,
|
| 24 |
|
| 25 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 26 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 27 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 28 |
if not fast_debug: time.sleep(2)
|
| 29 |
|
| 30 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 31 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 32 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 33 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 34 |
|
| 35 |
if not fast_debug:
|
| 36 |
msg = '正常'
|
| 37 |
# ** gpt request **
|
| 38 |
-
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot,
|
| 39 |
|
| 40 |
chatbot[-1] = (i_say, gpt_say)
|
| 41 |
history.append(i_say); history.append(gpt_say)
|
| 42 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 43 |
res = write_results_to_file(history)
|
| 44 |
chatbot.append(("完成了吗?", res))
|
| 45 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
@CatchException
|
| 50 |
-
def 读文章写摘要(txt,
|
| 51 |
history = [] # 清空历史,以免输入溢出
|
| 52 |
import glob, os
|
| 53 |
if os.path.exists(txt):
|
|
@@ -55,13 +55,13 @@ def 读文章写摘要(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
| 55 |
else:
|
| 56 |
if txt == "": txt = '空空如也的输入栏'
|
| 57 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 58 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 59 |
return
|
| 60 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] # + \
|
| 61 |
# [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
|
| 62 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 63 |
if len(file_manifest) == 0:
|
| 64 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
| 65 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 66 |
return
|
| 67 |
-
yield from 解析Paper(file_manifest, project_folder,
|
|
|
|
| 4 |
fast_debug = False
|
| 5 |
|
| 6 |
|
| 7 |
+
def 解析Paper(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 8 |
import time, glob, os
|
| 9 |
print('begin analysis on:', file_manifest)
|
| 10 |
for index, fp in enumerate(file_manifest):
|
|
|
|
| 15 |
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
| 16 |
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
| 17 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
| 18 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 19 |
|
| 20 |
if not fast_debug:
|
| 21 |
msg = '正常'
|
| 22 |
# ** gpt request **
|
| 23 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, plugin_kwargs, history=[]) # 带超时倒计时
|
| 24 |
|
| 25 |
chatbot[-1] = (i_say_show_user, gpt_say)
|
| 26 |
history.append(i_say_show_user); history.append(gpt_say)
|
| 27 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 28 |
if not fast_debug: time.sleep(2)
|
| 29 |
|
| 30 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
| 31 |
i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
|
| 32 |
chatbot.append((i_say, "[Local Message] waiting gpt response."))
|
| 33 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 34 |
|
| 35 |
if not fast_debug:
|
| 36 |
msg = '正常'
|
| 37 |
# ** gpt request **
|
| 38 |
+
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, llm_kwargs, plugin_kwargs, history=history) # 带超时倒计时
|
| 39 |
|
| 40 |
chatbot[-1] = (i_say, gpt_say)
|
| 41 |
history.append(i_say); history.append(gpt_say)
|
| 42 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 43 |
res = write_results_to_file(history)
|
| 44 |
chatbot.append(("完成了吗?", res))
|
| 45 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
@CatchException
|
| 50 |
+
def 读文章写摘要(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 51 |
history = [] # 清空历史,以免输入溢出
|
| 52 |
import glob, os
|
| 53 |
if os.path.exists(txt):
|
|
|
|
| 55 |
else:
|
| 56 |
if txt == "": txt = '空空如也的输入栏'
|
| 57 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
| 58 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 59 |
return
|
| 60 |
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] # + \
|
| 61 |
# [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
|
| 62 |
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
| 63 |
if len(file_manifest) == 0:
|
| 64 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
| 65 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 66 |
return
|
| 67 |
+
yield from 解析Paper(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
crazy_functions/谷歌检索小助手.py
CHANGED
|
@@ -56,16 +56,16 @@ def get_meta_information(url, chatbot, history):
|
|
| 56 |
})
|
| 57 |
|
| 58 |
chatbot[-1] = [chatbot[-1][0], title + f'\n\n是否在arxiv中(不在arxiv中无法获取完整摘要):{is_paper_in_arxiv}\n\n' + abstract]
|
| 59 |
-
yield from update_ui(chatbot=chatbot, history=[])
|
| 60 |
return profile
|
| 61 |
|
| 62 |
@CatchException
|
| 63 |
-
def 谷歌检索小助手(txt,
|
| 64 |
# 基本信息:功能、贡献者
|
| 65 |
chatbot.append([
|
| 66 |
"函数插件功能?",
|
| 67 |
"分析用户提供的谷歌学术(google scholar)搜索页面中,出现的所有文章: binary-husky,插件初始化中..."])
|
| 68 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 69 |
|
| 70 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 71 |
try:
|
|
@@ -75,7 +75,7 @@ def 谷歌检索小助手(txt, top_p, temperature, chatbot, history, systemPromp
|
|
| 75 |
report_execption(chatbot, history,
|
| 76 |
a = f"解析项目: {txt}",
|
| 77 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade beautifulsoup4 arxiv```。")
|
| 78 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 79 |
return
|
| 80 |
|
| 81 |
# 清空历史,以免输入溢出
|
|
@@ -91,7 +91,7 @@ def 谷歌检索小助手(txt, top_p, temperature, chatbot, history, systemPromp
|
|
| 91 |
inputs_show_user = f"请分析此页面中出现的所有文章:{txt}"
|
| 92 |
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 93 |
inputs=i_say, inputs_show_user=inputs_show_user,
|
| 94 |
-
|
| 95 |
sys_prompt="你是一个学术翻译,请从数据中提取信息。你必须使用Markdown格式。你必须逐个文献进行处理。"
|
| 96 |
)
|
| 97 |
|
|
@@ -100,7 +100,7 @@ def 谷歌检索小助手(txt, top_p, temperature, chatbot, history, systemPromp
|
|
| 100 |
|
| 101 |
chatbot.append(["状态?", "已经全部完成"])
|
| 102 |
msg = '正常'
|
| 103 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
| 104 |
res = write_results_to_file(history)
|
| 105 |
chatbot.append(("完成了吗?", res));
|
| 106 |
-
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg)
|
|
|
|
| 56 |
})
|
| 57 |
|
| 58 |
chatbot[-1] = [chatbot[-1][0], title + f'\n\n是否在arxiv中(不在arxiv中无法获取完整摘要):{is_paper_in_arxiv}\n\n' + abstract]
|
| 59 |
+
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
| 60 |
return profile
|
| 61 |
|
| 62 |
@CatchException
|
| 63 |
+
def 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 64 |
# 基本信息:功能、贡献者
|
| 65 |
chatbot.append([
|
| 66 |
"函数插件功能?",
|
| 67 |
"分析用户提供的谷歌学术(google scholar)搜索页面中,出现的所有文章: binary-husky,插件初始化中..."])
|
| 68 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 69 |
|
| 70 |
# 尝试导入依赖,如果缺少依赖,则给出安装建议
|
| 71 |
try:
|
|
|
|
| 75 |
report_execption(chatbot, history,
|
| 76 |
a = f"解析项目: {txt}",
|
| 77 |
b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade beautifulsoup4 arxiv```。")
|
| 78 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 79 |
return
|
| 80 |
|
| 81 |
# 清空历史,以免输入溢出
|
|
|
|
| 91 |
inputs_show_user = f"请分析此页面中出现的所有文章:{txt}"
|
| 92 |
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 93 |
inputs=i_say, inputs_show_user=inputs_show_user,
|
| 94 |
+
llm_kwargs=llm_kwargs, chatbot=chatbot, history=[],
|
| 95 |
sys_prompt="你是一个学术翻译,请从数据中提取信息。你必须使用Markdown格式。你必须逐个文献进行处理。"
|
| 96 |
)
|
| 97 |
|
|
|
|
| 100 |
|
| 101 |
chatbot.append(["状态?", "已经全部完成"])
|
| 102 |
msg = '正常'
|
| 103 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
| 104 |
res = write_results_to_file(history)
|
| 105 |
chatbot.append(("完成了吗?", res));
|
| 106 |
+
yield from update_ui(chatbot=chatbot, history=chatbot, msg=msg) # 刷新界面
|
crazy_functions/高级功能函数模板.py
CHANGED
|
@@ -2,19 +2,28 @@ from toolbox import CatchException, update_ui
|
|
| 2 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
| 3 |
import datetime
|
| 4 |
@CatchException
|
| 5 |
-
def 高阶功能模板函数(txt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
history = [] # 清空历史,以免输入溢出
|
| 7 |
-
chatbot.append(("这是什么功能?", "[Local Message] 请注意,您正在调用一个[函数插件]的模板,该函数面向希望实现更多有趣功能的开发者,它可以作为创建新功能函数的模板(该函数只有20
|
| 8 |
-
yield from update_ui(chatbot=chatbot, history=history) # 由于请求gpt需要一段时间,我们先及时地做一次界面更新
|
| 9 |
for i in range(5):
|
| 10 |
currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month
|
| 11 |
currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day
|
| 12 |
i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。发送图片时,请使用Markdown,将Unsplash API中的PUT_YOUR_QUERY_HERE替换成描述该事件的一个最重要的单词。'
|
| 13 |
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 14 |
inputs=i_say, inputs_show_user=i_say,
|
| 15 |
-
|
| 16 |
sys_prompt="当你想发送一张照片时,请使用Markdown, 并且不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT_YOUR_QUERY_HERE >)。"
|
| 17 |
)
|
| 18 |
chatbot[-1] = (i_say, gpt_say)
|
| 19 |
history.append(i_say);history.append(gpt_say)
|
| 20 |
-
yield from update_ui(chatbot=chatbot, history=history) # 界面更新
|
|
|
|
| 2 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
| 3 |
import datetime
|
| 4 |
@CatchException
|
| 5 |
+
def 高阶功能模板函数(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
| 6 |
+
"""
|
| 7 |
+
txt 输入栏用户输入的文本,例如需要翻译的一段话,再例如一个包含了待处理文件的路径
|
| 8 |
+
llm_kwargs gpt模型参数,如温度和top_p等,一般原样传递下去就行
|
| 9 |
+
plugin_kwargs 插件模型的参数,如温度和top_p等,一般原样传递下去就行
|
| 10 |
+
chatbot 聊天显示框的句柄,用于显示给用户
|
| 11 |
+
history 聊天历史,前情提要
|
| 12 |
+
system_prompt 给gpt的静默提醒
|
| 13 |
+
web_port 当前软件运行的端口号
|
| 14 |
+
"""
|
| 15 |
history = [] # 清空历史,以免输入溢出
|
| 16 |
+
chatbot.append(("这是什么功能?", "[Local Message] 请注意,您正在调用一个[函数插件]的模板,该函数面向希望实现更多有趣功能的开发者,它可以作为创建新功能函数的模板(该函数只有20多行代码)。此外我们也提供可同步处理大量文件的多线程Demo供您参考。您若希望分享新的功能模组,请不吝PR!"))
|
| 17 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新
|
| 18 |
for i in range(5):
|
| 19 |
currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month
|
| 20 |
currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day
|
| 21 |
i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。发送图片时,请使用Markdown,将Unsplash API中的PUT_YOUR_QUERY_HERE替换成描述该事件的一个最重要的单词。'
|
| 22 |
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
| 23 |
inputs=i_say, inputs_show_user=i_say,
|
| 24 |
+
llm_kwargs=llm_kwargs, chatbot=chatbot, history=[],
|
| 25 |
sys_prompt="当你想发送一张照片时,请使用Markdown, 并且不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT_YOUR_QUERY_HERE >)。"
|
| 26 |
)
|
| 27 |
chatbot[-1] = (i_say, gpt_say)
|
| 28 |
history.append(i_say);history.append(gpt_say)
|
| 29 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|
main.py
CHANGED
|
@@ -52,6 +52,7 @@ if LAYOUT == "TOP-DOWN":
|
|
| 52 |
cancel_handles = []
|
| 53 |
with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
| 54 |
gr.HTML(title_html)
|
|
|
|
| 55 |
with gr_L1():
|
| 56 |
with gr_L2(scale=2):
|
| 57 |
chatbot = gr.Chatbot()
|
|
@@ -117,8 +118,8 @@ with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=
|
|
| 117 |
return ret
|
| 118 |
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] )
|
| 119 |
# 整理反复出现的控件句柄组合
|
| 120 |
-
input_combo = [txt, txt2, top_p, temperature, chatbot, history, system_prompt]
|
| 121 |
-
output_combo = [chatbot, history, status]
|
| 122 |
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
|
| 123 |
# 提交按钮、重置按钮
|
| 124 |
cancel_handles.append(txt.submit(**predict_args))
|
|
|
|
| 52 |
cancel_handles = []
|
| 53 |
with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
| 54 |
gr.HTML(title_html)
|
| 55 |
+
cookies = gr.State({})
|
| 56 |
with gr_L1():
|
| 57 |
with gr_L2(scale=2):
|
| 58 |
chatbot = gr.Chatbot()
|
|
|
|
| 118 |
return ret
|
| 119 |
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] )
|
| 120 |
# 整理反复出现的控件句柄组合
|
| 121 |
+
input_combo = [cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt]
|
| 122 |
+
output_combo = [cookies, chatbot, history, status]
|
| 123 |
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
|
| 124 |
# 提交按钮、重置按钮
|
| 125 |
cancel_handles.append(txt.submit(**predict_args))
|
request_llm/bridge_chatgpt.py
CHANGED
|
@@ -72,14 +72,14 @@ def predict_no_ui(inputs, top_p, temperature, history=[], sys_prompt=""):
|
|
| 72 |
raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
|
| 73 |
|
| 74 |
|
| 75 |
-
def predict_no_ui_long_connection(inputs,
|
| 76 |
"""
|
| 77 |
发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。
|
| 78 |
inputs:
|
| 79 |
是本次问询的输入
|
| 80 |
sys_prompt:
|
| 81 |
系统静默prompt
|
| 82 |
-
|
| 83 |
chatGPT的内部调优参数
|
| 84 |
history:
|
| 85 |
是之前的对话列表
|
|
@@ -87,7 +87,7 @@ def predict_no_ui_long_connection(inputs, top_p, temperature, history=[], sys_pr
|
|
| 87 |
用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗
|
| 88 |
"""
|
| 89 |
watch_dog_patience = 5 # 看门狗的耐心, 设置5秒即可
|
| 90 |
-
headers, payload = generate_payload(inputs,
|
| 91 |
retry = 0
|
| 92 |
while True:
|
| 93 |
try:
|
|
@@ -135,8 +135,7 @@ def predict_no_ui_long_connection(inputs, top_p, temperature, history=[], sys_pr
|
|
| 135 |
return result
|
| 136 |
|
| 137 |
|
| 138 |
-
def predict(inputs,
|
| 139 |
-
stream = True, additional_fn=None):
|
| 140 |
"""
|
| 141 |
发送至chatGPT,流式获取输出。
|
| 142 |
用于基础的对话功能。
|
|
@@ -157,9 +156,9 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
| 157 |
raw_input = inputs
|
| 158 |
logging.info(f'[raw_input] {raw_input}')
|
| 159 |
chatbot.append((inputs, ""))
|
| 160 |
-
yield from update_ui(chatbot=chatbot, history=history, msg="等待响应")
|
| 161 |
|
| 162 |
-
headers, payload = generate_payload(inputs,
|
| 163 |
history.append(inputs); history.append(" ")
|
| 164 |
|
| 165 |
retry = 0
|
|
@@ -172,7 +171,7 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
| 172 |
retry += 1
|
| 173 |
chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg))
|
| 174 |
retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else ""
|
| 175 |
-
yield from update_ui(chatbot=chatbot, history=history, msg="请求超时"+retry_msg)
|
| 176 |
if retry > MAX_RETRY: raise TimeoutError
|
| 177 |
|
| 178 |
gpt_replying_buffer = ""
|
|
@@ -200,11 +199,11 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
| 200 |
gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
|
| 201 |
history[-1] = gpt_replying_buffer
|
| 202 |
chatbot[-1] = (history[-2], history[-1])
|
| 203 |
-
yield from update_ui(chatbot=chatbot, history=history, msg=status_text)
|
| 204 |
|
| 205 |
except Exception as e:
|
| 206 |
traceback.print_exc()
|
| 207 |
-
yield from update_ui(chatbot=chatbot, history=history, msg="Json解析不合常规")
|
| 208 |
chunk = get_full_error(chunk, stream_response)
|
| 209 |
error_msg = chunk.decode()
|
| 210 |
if "reduce the length" in error_msg:
|
|
@@ -218,10 +217,10 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
| 218 |
from toolbox import regular_txt_to_markdown
|
| 219 |
tb_str = '```\n' + traceback.format_exc() + '```'
|
| 220 |
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk.decode()[4:])}")
|
| 221 |
-
yield from update_ui(chatbot=chatbot, history=history, msg="Json异常" + error_msg)
|
| 222 |
return
|
| 223 |
|
| 224 |
-
def generate_payload(inputs,
|
| 225 |
"""
|
| 226 |
整合所有信息,选择LLM模型,生成http请求,为发送请求做准备
|
| 227 |
"""
|
|
@@ -257,8 +256,8 @@ def generate_payload(inputs, top_p, temperature, history, system_prompt, stream)
|
|
| 257 |
payload = {
|
| 258 |
"model": LLM_MODEL,
|
| 259 |
"messages": messages,
|
| 260 |
-
"temperature": temperature, # 1.0,
|
| 261 |
-
"top_p": top_p, # 1.0,
|
| 262 |
"n": 1,
|
| 263 |
"stream": stream,
|
| 264 |
"presence_penalty": 0,
|
|
|
|
| 72 |
raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
|
| 73 |
|
| 74 |
|
| 75 |
+
def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_slience=False):
|
| 76 |
"""
|
| 77 |
发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。
|
| 78 |
inputs:
|
| 79 |
是本次问询的输入
|
| 80 |
sys_prompt:
|
| 81 |
系统静默prompt
|
| 82 |
+
llm_kwargs:
|
| 83 |
chatGPT的内部调优参数
|
| 84 |
history:
|
| 85 |
是之前的对话列表
|
|
|
|
| 87 |
用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗
|
| 88 |
"""
|
| 89 |
watch_dog_patience = 5 # 看门狗的耐心, 设置5秒即可
|
| 90 |
+
headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt=sys_prompt, stream=True)
|
| 91 |
retry = 0
|
| 92 |
while True:
|
| 93 |
try:
|
|
|
|
| 135 |
return result
|
| 136 |
|
| 137 |
|
| 138 |
+
def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None):
|
|
|
|
| 139 |
"""
|
| 140 |
发送至chatGPT,流式获取输出。
|
| 141 |
用于基础的对话功能。
|
|
|
|
| 156 |
raw_input = inputs
|
| 157 |
logging.info(f'[raw_input] {raw_input}')
|
| 158 |
chatbot.append((inputs, ""))
|
| 159 |
+
yield from update_ui(chatbot=chatbot, history=history, msg="等待响应") # 刷新界面
|
| 160 |
|
| 161 |
+
headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt, stream)
|
| 162 |
history.append(inputs); history.append(" ")
|
| 163 |
|
| 164 |
retry = 0
|
|
|
|
| 171 |
retry += 1
|
| 172 |
chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg))
|
| 173 |
retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else ""
|
| 174 |
+
yield from update_ui(chatbot=chatbot, history=history, msg="请求超时"+retry_msg) # 刷新界面
|
| 175 |
if retry > MAX_RETRY: raise TimeoutError
|
| 176 |
|
| 177 |
gpt_replying_buffer = ""
|
|
|
|
| 199 |
gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
|
| 200 |
history[-1] = gpt_replying_buffer
|
| 201 |
chatbot[-1] = (history[-2], history[-1])
|
| 202 |
+
yield from update_ui(chatbot=chatbot, history=history, msg=status_text) # 刷新界面
|
| 203 |
|
| 204 |
except Exception as e:
|
| 205 |
traceback.print_exc()
|
| 206 |
+
yield from update_ui(chatbot=chatbot, history=history, msg="Json解析不合常规") # 刷新界面
|
| 207 |
chunk = get_full_error(chunk, stream_response)
|
| 208 |
error_msg = chunk.decode()
|
| 209 |
if "reduce the length" in error_msg:
|
|
|
|
| 217 |
from toolbox import regular_txt_to_markdown
|
| 218 |
tb_str = '```\n' + traceback.format_exc() + '```'
|
| 219 |
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk.decode()[4:])}")
|
| 220 |
+
yield from update_ui(chatbot=chatbot, history=history, msg="Json异常" + error_msg) # 刷新界面
|
| 221 |
return
|
| 222 |
|
| 223 |
+
def generate_payload(inputs, llm_kwargs, history, system_prompt, stream):
|
| 224 |
"""
|
| 225 |
整合所有信息,选择LLM模型,生成http请求,为发送请求做准备
|
| 226 |
"""
|
|
|
|
| 256 |
payload = {
|
| 257 |
"model": LLM_MODEL,
|
| 258 |
"messages": messages,
|
| 259 |
+
"temperature": llm_kwargs['temperature'], # 1.0,
|
| 260 |
+
"top_p": llm_kwargs['top_p'], # 1.0,
|
| 261 |
"n": 1,
|
| 262 |
"stream": stream,
|
| 263 |
"presence_penalty": 0,
|
request_llm/bridge_tgui.py
CHANGED
|
@@ -90,7 +90,7 @@ async def run(context, max_token=512):
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
-
def predict_tgui(inputs, top_p, temperature, chatbot
|
| 94 |
"""
|
| 95 |
发送至chatGPT,流式获取输出。
|
| 96 |
用于基础的对话功能。
|
|
@@ -111,7 +111,7 @@ def predict_tgui(inputs, top_p, temperature, chatbot=[], history=[], system_prom
|
|
| 111 |
logging.info(f'[raw_input] {raw_input}')
|
| 112 |
history.extend([inputs, ""])
|
| 113 |
chatbot.append([inputs, ""])
|
| 114 |
-
yield from update_ui(chatbot=chatbot, history=history, msg="等待响应")
|
| 115 |
|
| 116 |
prompt = inputs
|
| 117 |
tgui_say = ""
|
|
@@ -138,7 +138,7 @@ def predict_tgui(inputs, top_p, temperature, chatbot=[], history=[], system_prom
|
|
| 138 |
tgui_say = mutable[0]
|
| 139 |
history[-1] = tgui_say
|
| 140 |
chatbot[-1] = (history[-2], history[-1])
|
| 141 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 142 |
|
| 143 |
logging.info(f'[response] {tgui_say}')
|
| 144 |
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
+
def predict_tgui(inputs, top_p, temperature, chatbot, history=[], system_prompt='', stream = True, additional_fn=None):
|
| 94 |
"""
|
| 95 |
发送至chatGPT,流式获取输出。
|
| 96 |
用于基础的对话功能。
|
|
|
|
| 111 |
logging.info(f'[raw_input] {raw_input}')
|
| 112 |
history.extend([inputs, ""])
|
| 113 |
chatbot.append([inputs, ""])
|
| 114 |
+
yield from update_ui(chatbot=chatbot, history=history, msg="等待响应") # 刷新界面
|
| 115 |
|
| 116 |
prompt = inputs
|
| 117 |
tgui_say = ""
|
|
|
|
| 138 |
tgui_say = mutable[0]
|
| 139 |
history[-1] = tgui_say
|
| 140 |
chatbot[-1] = (history[-2], history[-1])
|
| 141 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 142 |
|
| 143 |
logging.info(f'[response] {tgui_say}')
|
| 144 |
|
toolbox.py
CHANGED
|
@@ -9,23 +9,52 @@ import re
|
|
| 9 |
from latex2mathml.converter import convert as tex2mathml
|
| 10 |
from functools import wraps, lru_cache
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def ArgsGeneralWrapper(f):
|
| 14 |
"""
|
| 15 |
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
| 16 |
"""
|
| 17 |
-
def decorated(txt, txt2, *args
|
| 18 |
txt_passon = txt
|
| 19 |
-
if txt == "" and txt2 != "":
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return decorated
|
| 23 |
|
| 24 |
-
def update_ui(chatbot, history, msg='正常',
|
| 25 |
"""
|
| 26 |
刷新用户界面
|
| 27 |
"""
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def get_reduce_token_percent(text):
|
| 31 |
"""
|
|
@@ -102,7 +131,7 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp
|
|
| 102 |
cnt += 1
|
| 103 |
chatbot[-1] = (i_say_show_user,
|
| 104 |
f"[Local Message] {mutable[1]}waiting gpt response {cnt}/{TIMEOUT_SECONDS*2*(MAX_RETRY+1)}"+''.join(['.']*(cnt % 4)))
|
| 105 |
-
yield from update_ui(chatbot=chatbot, history=history)
|
| 106 |
time.sleep(1)
|
| 107 |
# 把gpt的输出从mutable中取出来
|
| 108 |
gpt_say = mutable[0]
|
|
@@ -166,7 +195,7 @@ def CatchException(f):
|
|
| 166 |
chatbot = [["插件调度异常", "异常原因"]]
|
| 167 |
chatbot[-1] = (chatbot[-1][0],
|
| 168 |
f"[Local Message] 实验性函数调用出错: \n\n{tb_str} \n\n当前代理可用性: \n\n{check_proxy(proxies)}")
|
| 169 |
-
yield from update_ui(chatbot=chatbot, history=history, msg=f'异常 {e}')
|
| 170 |
return decorated
|
| 171 |
|
| 172 |
|
|
|
|
| 9 |
from latex2mathml.converter import convert as tex2mathml
|
| 10 |
from functools import wraps, lru_cache
|
| 11 |
|
| 12 |
+
############################### 插件输入输出接驳区 #######################################
|
| 13 |
+
class ChatBotWithCookies(list):
|
| 14 |
+
def __init__(self, cookie):
|
| 15 |
+
self._cookie = cookie
|
| 16 |
+
|
| 17 |
+
def write_list(self, list):
|
| 18 |
+
for t in list:
|
| 19 |
+
self.append(t)
|
| 20 |
+
|
| 21 |
+
def get_list(self):
|
| 22 |
+
return [t for t in self]
|
| 23 |
+
|
| 24 |
+
def get_cookies(self):
|
| 25 |
+
return self._cookie
|
| 26 |
|
| 27 |
def ArgsGeneralWrapper(f):
|
| 28 |
"""
|
| 29 |
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
| 30 |
"""
|
| 31 |
+
def decorated(cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *args):
|
| 32 |
txt_passon = txt
|
| 33 |
+
if txt == "" and txt2 != "": txt_passon = txt2
|
| 34 |
+
# 引入一个有cookie的chatbot
|
| 35 |
+
cookies.update({
|
| 36 |
+
'top_p':top_p,
|
| 37 |
+
'temperature':temperature,
|
| 38 |
+
})
|
| 39 |
+
llm_kwargs = {
|
| 40 |
+
'top_p':top_p,
|
| 41 |
+
'temperature':temperature,
|
| 42 |
+
}
|
| 43 |
+
plugin_kwargs = {
|
| 44 |
+
}
|
| 45 |
+
chatbot_with_cookie = ChatBotWithCookies(cookies)
|
| 46 |
+
chatbot_with_cookie.write_list(txt_passon)
|
| 47 |
+
yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)
|
| 48 |
return decorated
|
| 49 |
|
| 50 |
+
def update_ui(chatbot, history, msg='正常', **kwargs): # 刷新界面
|
| 51 |
"""
|
| 52 |
刷新用户界面
|
| 53 |
"""
|
| 54 |
+
assert isinstance(chatbot, ChatBotWithCookies), "在传递chatbot的过程中不要将其丢弃。必要时,可用clear将其清空,然后用for+append循环重新赋值。"
|
| 55 |
+
yield chatbot.get_cookies(), chatbot.get_list(), history, msg
|
| 56 |
+
############################### ################## #######################################
|
| 57 |
+
##########################################################################################
|
| 58 |
|
| 59 |
def get_reduce_token_percent(text):
|
| 60 |
"""
|
|
|
|
| 131 |
cnt += 1
|
| 132 |
chatbot[-1] = (i_say_show_user,
|
| 133 |
f"[Local Message] {mutable[1]}waiting gpt response {cnt}/{TIMEOUT_SECONDS*2*(MAX_RETRY+1)}"+''.join(['.']*(cnt % 4)))
|
| 134 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 135 |
time.sleep(1)
|
| 136 |
# 把gpt的输出从mutable中取出来
|
| 137 |
gpt_say = mutable[0]
|
|
|
|
| 195 |
chatbot = [["插件调度异常", "异常原因"]]
|
| 196 |
chatbot[-1] = (chatbot[-1][0],
|
| 197 |
f"[Local Message] 实验性函数调用出错: \n\n{tb_str} \n\n当前代理可用性: \n\n{check_proxy(proxies)}")
|
| 198 |
+
yield from update_ui(chatbot=chatbot, history=history, msg=f'异常 {e}') # 刷新界面
|
| 199 |
return decorated
|
| 200 |
|
| 201 |
|