Merge branch 'master' into frontier

This commit is contained in:
binary-husky 2023-11-30 22:24:34 +08:00
commit 22e00eb1c5
4 changed files with 37 additions and 3 deletions

View File

@ -88,6 +88,9 @@ def arxiv_download(chatbot, history, txt, allow_cache=True):
target_file = pj(translation_dir, 'translate_zh.pdf') target_file = pj(translation_dir, 'translate_zh.pdf')
if os.path.exists(target_file): if os.path.exists(target_file):
promote_file_to_downloadzone(target_file, rename_file=None, chatbot=chatbot) promote_file_to_downloadzone(target_file, rename_file=None, chatbot=chatbot)
target_file_compare = pj(translation_dir, 'comparison.pdf')
if os.path.exists(target_file_compare):
promote_file_to_downloadzone(target_file_compare, rename_file=None, chatbot=chatbot)
return target_file return target_file
return False return False
def is_float(s): def is_float(s):

View File

@ -418,6 +418,7 @@ def 编译Latex(chatbot, history, main_file_original, main_file_modified, work_f
merge_pdfs(origin_pdf, result_pdf, concat_pdf) merge_pdfs(origin_pdf, result_pdf, concat_pdf)
promote_file_to_downloadzone(concat_pdf, rename_file=None, chatbot=chatbot) # promote file to web UI promote_file_to_downloadzone(concat_pdf, rename_file=None, chatbot=chatbot) # promote file to web UI
except Exception as e: except Exception as e:
print(e)
pass pass
return True # 成功啦 return True # 成功啦
else: else:

View File

@ -493,11 +493,38 @@ def compile_latex_with_timeout(command, cwd, timeout=60):
return False return False
return True return True
def run_in_subprocess_wrapper_func(func, args, kwargs, return_dict, exception_dict):
import sys
try:
result = func(*args, **kwargs)
return_dict['result'] = result
except Exception as e:
exc_info = sys.exc_info()
exception_dict['exception'] = exc_info
def run_in_subprocess(func):
import multiprocessing
def wrapper(*args, **kwargs):
return_dict = multiprocessing.Manager().dict()
exception_dict = multiprocessing.Manager().dict()
process = multiprocessing.Process(target=run_in_subprocess_wrapper_func,
args=(func, args, kwargs, return_dict, exception_dict))
process.start()
process.join()
process.close()
if 'exception' in exception_dict:
# ooops, the subprocess ran into an exception
exc_info = exception_dict['exception']
raise exc_info[1].with_traceback(exc_info[2])
if 'result' in return_dict.keys():
# If the subprocess ran successfully, return the result
return return_dict['result']
return wrapper
def merge_pdfs(pdf1_path, pdf2_path, output_path): def _merge_pdfs(pdf1_path, pdf2_path, output_path):
import PyPDF2 import PyPDF2 # PyPDF2这个库有严重的内存泄露问题把它放到子进程中运行从而方便内存的释放
Percent = 0.95 Percent = 0.95
# raise RuntimeError('PyPDF2 has a serious memory leak problem, please use other tools to merge PDF files.')
# Open the first PDF file # Open the first PDF file
with open(pdf1_path, 'rb') as pdf1_file: with open(pdf1_path, 'rb') as pdf1_file:
pdf1_reader = PyPDF2.PdfFileReader(pdf1_file) pdf1_reader = PyPDF2.PdfFileReader(pdf1_file)
@ -531,3 +558,5 @@ def merge_pdfs(pdf1_path, pdf2_path, output_path):
# Save the merged PDF file # Save the merged PDF file
with open(output_path, 'wb') as output_file: with open(output_path, 'wb') as output_file:
output_writer.write(output_file) output_writer.write(output_file)
merge_pdfs = run_in_subprocess(_merge_pdfs) # PyPDF2这个库有严重的内存泄露问题把它放到子进程中运行从而方便内存的释放

View File

@ -563,7 +563,8 @@ def promote_file_to_downloadzone(file, rename_file=None, chatbot=None):
user_name = get_user(chatbot) user_name = get_user(chatbot)
else: else:
user_name = default_user_name user_name = default_user_name
if not os.path.exists(file):
raise FileNotFoundError(f'文件{file}不存在')
user_path = get_log_folder(user_name, plugin_name=None) user_path = get_log_folder(user_name, plugin_name=None)
if file_already_in_downloadzone(file, user_path): if file_already_in_downloadzone(file, user_path):
new_path = file new_path = file