From af7734dd35c62de6f85a18b00c3598527b85cfb4 Mon Sep 17 00:00:00 2001
From: 505030475 <505030475@qq.com>
Date: Mon, 19 Jun 2023 16:57:11 +1000
Subject: [PATCH] avoid file fusion
---
crazy_functions/latex_utils.py | 2 +-
main.py | 4 ++--
toolbox.py | 14 ++++++++++----
3 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/crazy_functions/latex_utils.py b/crazy_functions/latex_utils.py
index 78eec29..163d0e2 100644
--- a/crazy_functions/latex_utils.py
+++ b/crazy_functions/latex_utils.py
@@ -685,7 +685,7 @@ def 编译Latex(chatbot, history, main_file_original, main_file_modified, work_f
result_pdf = pj(work_folder_modified, f'{main_file_modified}.pdf')
if os.path.exists(pj(work_folder, '..', 'translation')):
shutil.copyfile(result_pdf, pj(work_folder, '..', 'translation', 'translate_zh.pdf'))
- promote_file_to_downloadzone(result_pdf)
+ promote_file_to_downloadzone(result_pdf, chatbot)
return True # 成功啦
else:
if n_fix>=max_try: break
diff --git a/main.py b/main.py
index 7dbf17f..65e1f4c 100644
--- a/main.py
+++ b/main.py
@@ -155,7 +155,7 @@ def main():
for k in crazy_fns:
if not crazy_fns[k].get("AsButton", True): continue
click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
- click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
+ click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
cancel_handles.append(click_handle)
# 函数插件-下拉菜单与随变按钮的互动
def on_dropdown_changed(k):
@@ -175,7 +175,7 @@ def main():
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs)
click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
- click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
+ click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
cancel_handles.append(click_handle)
# 终止按钮的回调函数注册
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
diff --git a/toolbox.py b/toolbox.py
index 4ab1116..ac49afc 100644
--- a/toolbox.py
+++ b/toolbox.py
@@ -439,13 +439,15 @@ def find_recent_files(directory):
return recent_files
-def promote_file_to_downloadzone(file, rename_file=None):
+def promote_file_to_downloadzone(file, rename_file=None, chatbot=None):
# 将文件复制一份到下载区
import shutil
if rename_file is None: rename_file = f'{gen_time_str()}-{os.path.basename(file)}'
new_path = os.path.join(f'./gpt_log/', rename_file)
if os.path.exists(new_path): os.remove(new_path)
shutil.copyfile(file, new_path)
+ if chatbot:
+ chatbot._cookies.update({'file_to_promote': [new_path]})
def on_file_uploaded(files, chatbot, txt, txt2, checkboxes):
"""
@@ -485,16 +487,20 @@ def on_file_uploaded(files, chatbot, txt, txt2, checkboxes):
return chatbot, txt, txt2
-def on_report_generated(files, chatbot):
+def on_report_generated(cookies, files, chatbot):
from toolbox import find_recent_files
- report_files = find_recent_files('gpt_log')
+ if 'file_to_promote' in cookies:
+ report_files = cookies['file_to_promote']
+ cookies.pop('file_to_promote')
+ else:
+ report_files = find_recent_files('gpt_log')
if len(report_files) == 0:
return None, chatbot
# files.extend(report_files)
file_links = ''
for f in report_files: file_links += f'
{f}'
chatbot.append(['报告如何远程获取?', f'报告已经添加到右侧“文件上传区”(可能处于折叠状态),请查收。{file_links}'])
- return report_files, chatbot
+ return cookies, report_files, chatbot
def is_openai_api_key(key):
API_MATCH_ORIGINAL = re.match(r"sk-[a-zA-Z0-9]{48}$", key)