diff --git a/crazy_functions/Latex_Function.py b/crazy_functions/Latex_Function.py index 236460eb..51b03283 100644 --- a/crazy_functions/Latex_Function.py +++ b/crazy_functions/Latex_Function.py @@ -3,7 +3,7 @@ from toolbox import CatchException, report_exception, update_ui_lastest_msg, zip from functools import partial from loguru import logger -import glob, os, requests, time, json, tarfile +import glob, os, requests, time, json, tarfile, threading pj = os.path.join ARXIV_CACHE_DIR = get_conf("ARXIV_CACHE_DIR") @@ -338,11 +338,17 @@ def Latex翻译中文并重新编译PDF(txt, llm_kwargs, plugin_kwargs, chatbot, # <-------------- more requirements -------------> if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg") more_req = plugin_kwargs.get("advanced_arg", "") - no_cache = more_req.startswith("--no-cache") - if no_cache: more_req.lstrip("--no-cache") + + no_cache = ("--no-cache" in more_req) + if no_cache: more_req = more_req.replace("--no-cache", "").strip() + + allow_gptac_cloud_io = ("--allow-cloudio" in more_req) # 从云端下载翻译结果,以及上传翻译结果到云端 + if allow_gptac_cloud_io: more_req = more_req.replace("--allow-cloudio", "").strip() + allow_cache = not no_cache _switch_prompt_ = partial(switch_prompt, more_requirement=more_req) + # <-------------- check deps -------------> try: import glob, os, time, subprocess @@ -364,29 +370,25 @@ def Latex翻译中文并重新编译PDF(txt, llm_kwargs, plugin_kwargs, chatbot, chatbot=chatbot, history=history) return - # allow_cloud_io = True - # arxiv_id = "2203.01927" - # if allow_cloud_io and arxiv_id: - # # 如果用户允许,我们将arxiv论文PDF上传到云端 - # for file_path in chatbot._cookies.get("files_to_promote", []): - # if file_path.endswith('comparison.pdf'): - # def compute_hash(file_path): - # return map_file_to_sha256(file_path) - # with open(file_path, 'rb') as f: - # import requests - # url = 'https://cloud-2.agent-matrix.com/upload' - # files = {'file': (file_path, f, 'application/octet-stream')} - # data = { - # 'arxiv_id': arxiv_id, - # 'file_hash': compute_hash(file_path), - # } - # resp = requests.get(url=url, files=files, data=data, timeout=10) - if txt.endswith('.pdf'): report_exception(chatbot, history, a=f"解析项目: {txt}", b=f"发现已经存在翻译好的PDF文档") yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return + # ################################################################# + if allow_gptac_cloud_io and arxiv_id: + # 访问 GPTAC学术云,查询云端是否存在该论文的翻译版本 + from crazy_functions.latex_fns.latex_actions import check_gptac_cloud + success, downloaded = check_gptac_cloud(arxiv_id, chatbot) + if success: + chatbot.append([ + f"检测到GPTAC云端存在翻译版本, 如果不满意翻译结果, 请禁用云端分享, 然后重新执行。", + None + ]) + yield from update_ui(chatbot=chatbot, history=history) + return + ################################################################# + if os.path.exists(txt): project_folder = txt else: @@ -424,24 +426,11 @@ def Latex翻译中文并重新编译PDF(txt, llm_kwargs, plugin_kwargs, chatbot, # <-------------- zip PDF -------------> zip_res = zip_result(project_folder) if success: - allow_cloud_io = True - arxiv_id = "2203.01927" - if allow_cloud_io and arxiv_id: - # 如果用户允许,我们将arxiv论文PDF上传到云端 - for file_path in chatbot._cookies.get("files_to_promote", []): - if file_path.endswith('translate_zh.pdf') or file_path.endswith('comparison.pdf'): - def compute_hash(file_path): - return map_file_to_sha256(file_path) - with open(file_path, 'rb') as f: - import requests - url = 'https://cloud-2.agent-matrix.com/upload' - files = {'file': (file_path, f, 'application/octet-stream')} - data = { - 'arxiv_id': arxiv_id, - 'file_hash': compute_hash(file_path), - } - resp = requests.post(url=url, files=files, data=data, timeout=10) - + if allow_gptac_cloud_io and arxiv_id: + # 如果用户允许,我们将翻译好的arxiv论文PDF上传到GPTAC学术云 + from crazy_functions.latex_fns.latex_actions import upload_to_gptac_cloud_if_user_allow + threading.Thread(target=upload_to_gptac_cloud_if_user_allow, + args=(chatbot, arxiv_id), daemon=True).start() chatbot.append((f"成功啦", '请查收结果(压缩包)...')) yield from update_ui(chatbot=chatbot, history=history) diff --git a/crazy_functions/Latex_Function_Wrap.py b/crazy_functions/Latex_Function_Wrap.py index 5d7b1f31..e591e380 100644 --- a/crazy_functions/Latex_Function_Wrap.py +++ b/crazy_functions/Latex_Function_Wrap.py @@ -30,6 +30,9 @@ class Arxiv_Localize(GptAcademicPluginTemplate): default_value="", type="string").model_dump_json(), # 高级参数输入区,自动同步 "allow_cache": ArgProperty(title="是否允许从缓存中调取结果", options=["允许缓存", "从头执行"], default_value="允许缓存", description="无", type="dropdown").model_dump_json(), + "allow_cloudio": + ArgProperty(title="是否允许向GPTAC学术云共享翻译结果", options=["允许", "禁止"], default_value="禁止", description="人人为我,我为人人", type="dropdown").model_dump_json(), + } return gui_definition @@ -38,9 +41,14 @@ class Arxiv_Localize(GptAcademicPluginTemplate): 执行插件 """ allow_cache = plugin_kwargs["allow_cache"] + allow_cloudio = plugin_kwargs["allow_cloudio"] advanced_arg = plugin_kwargs["advanced_arg"] if allow_cache == "从头执行": plugin_kwargs["advanced_arg"] = "--no-cache " + plugin_kwargs["advanced_arg"] + + # 从云端下载翻译结果,以及上传翻译结果到云端;人人为我,我为人人。 + if allow_cloudio == "允许": plugin_kwargs["advanced_arg"] = "--allow-cloudio " + plugin_kwargs["advanced_arg"] + yield from Latex翻译中文并重新编译PDF(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request) diff --git a/crazy_functions/latex_fns/latex_actions.py b/crazy_functions/latex_fns/latex_actions.py index 67c9b8c6..cfa0f155 100644 --- a/crazy_functions/latex_fns/latex_actions.py +++ b/crazy_functions/latex_fns/latex_actions.py @@ -3,7 +3,7 @@ import re import shutil import numpy as np from loguru import logger -from toolbox import update_ui, update_ui_lastest_msg, get_log_folder +from toolbox import update_ui, update_ui_lastest_msg, get_log_folder, gen_time_str from toolbox import get_conf, promote_file_to_downloadzone from crazy_functions.latex_fns.latex_toolbox import PRESERVE, TRANSFORM from crazy_functions.latex_fns.latex_toolbox import set_forbidden_text, set_forbidden_text_begin_end, set_forbidden_text_careful_brace @@ -471,3 +471,66 @@ def write_html(sp_file_contents, sp_file_result, chatbot, project_folder): except: from toolbox import trimmed_format_exc logger.error('writing html result failed:', trimmed_format_exc()) + + +def upload_to_gptac_cloud_if_user_allow(chatbot, arxiv_id): + try: + # 如果用户允许,我们将arxiv论文PDF上传到GPTAC学术云 + from toolbox import map_file_to_sha256 + # 检查是否顺利,如果没有生成预期的文件,则跳过 + is_result_good = False + for file_path in chatbot._cookies.get("files_to_promote", []): + if file_path.endswith('translate_zh.pdf'): + is_result_good = True + if not is_result_good: + return + # 上传文件 + for file_path in chatbot._cookies.get("files_to_promote", []): + align_name = None + # normalized name + for name in ['translate_zh.pdf', 'comparison.pdf']: + if file_path.endswith(name): align_name = name + # if match any align name + if align_name: + logger.info(f'Uploading to GPTAC cloud as the user has set `allow_cloud_io`: {file_path}') + with open(file_path, 'rb') as f: + import requests + url = 'https://cloud-2.agent-matrix.com/upload' + files = {'file': (align_name, f, 'application/octet-stream')} + data = { + 'arxiv_id': arxiv_id, + 'file_hash': map_file_to_sha256(file_path), + } + resp = requests.post(url=url, files=files, data=data, timeout=30) + logger.info(f'Uploading terminate ({resp.status_code})`: {file_path}') + except: + # 如果上传失败,不会中断程序,因为这是次要功能 + pass + +def check_gptac_cloud(arxiv_id, chatbot): + import requests + success = False + downloaded = [] + try: + for pdf_target in ['translate_zh.pdf', 'comparison.pdf']: + url = 'https://cloud-2.agent-matrix.com/paper_exist' + data = { + 'arxiv_id': arxiv_id, + 'name': pdf_target, + } + resp = requests.post(url=url, data=data) + cache_hit_result = resp.text.strip('"') + if cache_hit_result.startswith("http"): + url = cache_hit_result + logger.info(f'Downloading from GPTAC cloud: {url}') + resp = requests.get(url=url, timeout=30) + target = os.path.join(get_log_folder(plugin_name='gptac_cloud'), gen_time_str(), pdf_target) + os.makedirs(os.path.dirname(target), exist_ok=True) + with open(target, 'wb') as f: + f.write(resp.content) + new_path = promote_file_to_downloadzone(target, chatbot=chatbot) + success = True + downloaded.append(new_path) + except: + pass + return success, downloaded diff --git a/tests/test_latex_auto_correct.py b/tests/test_latex_auto_correct.py index 93c8f707..ea421370 100644 --- a/tests/test_latex_auto_correct.py +++ b/tests/test_latex_auto_correct.py @@ -20,4 +20,7 @@ if __name__ == "__main__": # plugin_test(plugin='crazy_functions.Latex_Function->Latex翻译中文并重新编译PDF', main_input="2203.01927") - plugin_test(plugin='crazy_functions.Latex_Function->Latex翻译中文并重新编译PDF', main_input="gpt_log/arxiv_cache/2203.01927/workfolder") + # plugin_test(plugin='crazy_functions.Latex_Function->Latex翻译中文并重新编译PDF', main_input="gpt_log/arxiv_cache/2203.01927/workfolder") + # plugin_test(plugin='crazy_functions.Latex_Function->Latex翻译中文并重新编译PDF', main_input="2410.05779") + plugin_test(plugin='crazy_functions.Latex_Function->Latex翻译中文并重新编译PDF', main_input="gpt_log/default_user/workfolder") +