Spaces:
Running
Running
修复pdf翻译的问题
Browse files- crazy_functions/批量翻译PDF文档_多线程.py +94 -9
- toolbox.py +7 -4
crazy_functions/批量翻译PDF文档_多线程.py
CHANGED
|
@@ -58,14 +58,17 @@ def 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys_
|
|
| 58 |
|
| 59 |
def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt):
|
| 60 |
import os
|
|
|
|
| 61 |
import tiktoken
|
| 62 |
TOKEN_LIMIT_PER_FRAGMENT = 1280
|
| 63 |
generated_conclusion_files = []
|
|
|
|
| 64 |
for index, fp in enumerate(file_manifest):
|
| 65 |
|
| 66 |
# 读取PDF文件
|
| 67 |
file_content, page_one = read_and_clean_pdf_text(fp)
|
| 68 |
-
|
|
|
|
| 69 |
# 递归地切割PDF文件
|
| 70 |
from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
|
| 71 |
from request_llm.bridge_all import model_info
|
|
@@ -74,7 +77,7 @@ def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot,
|
|
| 74 |
paper_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf(
|
| 75 |
txt=file_content, get_token_fn=get_token_num, limit=TOKEN_LIMIT_PER_FRAGMENT)
|
| 76 |
page_one_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf(
|
| 77 |
-
txt=
|
| 78 |
|
| 79 |
# 为了更好的效果,我们剥离Introduction之后的部分(如果有)
|
| 80 |
paper_meta = page_one_fragments[0].split('introduction')[0].split('Introduction')[0].split('INTRODUCTION')[0]
|
|
@@ -100,15 +103,15 @@ def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot,
|
|
| 100 |
"请你作为一个学术翻译,负责把学术论文准确翻译成中文。注意文章中的每一句话都要翻译。" for _ in paper_fragments],
|
| 101 |
# max_workers=5 # OpenAI所允许的最大并行过载
|
| 102 |
)
|
| 103 |
-
|
| 104 |
# 整理报告的格式
|
| 105 |
-
for i,k in enumerate(
|
| 106 |
if i%2==0:
|
| 107 |
-
|
| 108 |
else:
|
| 109 |
-
|
| 110 |
final = ["一、论文概况\n\n---\n\n", paper_meta_info.replace('# ', '### ') + '\n\n---\n\n', "二、论文翻译", ""]
|
| 111 |
-
final.extend(
|
| 112 |
create_report_file_name = f"{os.path.basename(fp)}.trans.md"
|
| 113 |
res = write_results_to_file(final, file_name=create_report_file_name)
|
| 114 |
|
|
@@ -117,15 +120,97 @@ def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot,
|
|
| 117 |
chatbot.append((f"{fp}完成了吗?", res))
|
| 118 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
# 准备文件的下载
|
| 121 |
import shutil
|
| 122 |
for pdf_path in generated_conclusion_files:
|
| 123 |
# 重命名文件
|
| 124 |
-
rename_file = f'./gpt_log
|
| 125 |
if os.path.exists(rename_file):
|
| 126 |
os.remove(rename_file)
|
| 127 |
shutil.copyfile(pdf_path, rename_file)
|
| 128 |
if os.path.exists(pdf_path):
|
| 129 |
os.remove(pdf_path)
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt):
|
| 60 |
import os
|
| 61 |
+
import copy
|
| 62 |
import tiktoken
|
| 63 |
TOKEN_LIMIT_PER_FRAGMENT = 1280
|
| 64 |
generated_conclusion_files = []
|
| 65 |
+
generated_html_files = []
|
| 66 |
for index, fp in enumerate(file_manifest):
|
| 67 |
|
| 68 |
# 读取PDF文件
|
| 69 |
file_content, page_one = read_and_clean_pdf_text(fp)
|
| 70 |
+
file_content = file_content.encode('utf-8', 'ignore').decode() # avoid reading non-utf8 chars
|
| 71 |
+
page_one = str(page_one).encode('utf-8', 'ignore').decode() # avoid reading non-utf8 chars
|
| 72 |
# 递归地切割PDF文件
|
| 73 |
from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
|
| 74 |
from request_llm.bridge_all import model_info
|
|
|
|
| 77 |
paper_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf(
|
| 78 |
txt=file_content, get_token_fn=get_token_num, limit=TOKEN_LIMIT_PER_FRAGMENT)
|
| 79 |
page_one_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf(
|
| 80 |
+
txt=page_one, get_token_fn=get_token_num, limit=TOKEN_LIMIT_PER_FRAGMENT//4)
|
| 81 |
|
| 82 |
# 为了更好的效果,我们剥离Introduction之后的部分(如果有)
|
| 83 |
paper_meta = page_one_fragments[0].split('introduction')[0].split('Introduction')[0].split('INTRODUCTION')[0]
|
|
|
|
| 103 |
"请你作为一个学术翻译,负责把学术论文准确翻译成中文。注意文章中的每一句话都要翻译。" for _ in paper_fragments],
|
| 104 |
# max_workers=5 # OpenAI所允许的最大并行过载
|
| 105 |
)
|
| 106 |
+
gpt_response_collection_md = copy.deepcopy(gpt_response_collection)
|
| 107 |
# 整理报告的格式
|
| 108 |
+
for i,k in enumerate(gpt_response_collection_md):
|
| 109 |
if i%2==0:
|
| 110 |
+
gpt_response_collection_md[i] = f"\n\n---\n\n ## 原文[{i//2}/{len(gpt_response_collection_md)//2}]: \n\n {paper_fragments[i//2].replace('#', '')} \n\n---\n\n ## 翻译[{i//2}/{len(gpt_response_collection_md)//2}]:\n "
|
| 111 |
else:
|
| 112 |
+
gpt_response_collection_md[i] = gpt_response_collection_md[i]
|
| 113 |
final = ["一、论文概况\n\n---\n\n", paper_meta_info.replace('# ', '### ') + '\n\n---\n\n', "二、论文翻译", ""]
|
| 114 |
+
final.extend(gpt_response_collection_md)
|
| 115 |
create_report_file_name = f"{os.path.basename(fp)}.trans.md"
|
| 116 |
res = write_results_to_file(final, file_name=create_report_file_name)
|
| 117 |
|
|
|
|
| 120 |
chatbot.append((f"{fp}完成了吗?", res))
|
| 121 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 122 |
|
| 123 |
+
# write html
|
| 124 |
+
try:
|
| 125 |
+
ch = construct_html()
|
| 126 |
+
orig = ""
|
| 127 |
+
trans = ""
|
| 128 |
+
gpt_response_collection_html = copy.deepcopy(gpt_response_collection)
|
| 129 |
+
for i,k in enumerate(gpt_response_collection_html):
|
| 130 |
+
if i%2==0:
|
| 131 |
+
gpt_response_collection_html[i] = paper_fragments[i//2].replace('#', '')
|
| 132 |
+
else:
|
| 133 |
+
gpt_response_collection_html[i] = gpt_response_collection_html[i]
|
| 134 |
+
final = ["论文概况", paper_meta_info.replace('# ', '### '), "二、论文翻译", ""]
|
| 135 |
+
final.extend(gpt_response_collection_html)
|
| 136 |
+
for i, k in enumerate(final):
|
| 137 |
+
if i%2==0:
|
| 138 |
+
orig = k
|
| 139 |
+
if i%2==1:
|
| 140 |
+
trans = k
|
| 141 |
+
ch.add_row(a=orig, b=trans)
|
| 142 |
+
create_report_file_name = f"{os.path.basename(fp)}.trans.html"
|
| 143 |
+
ch.save_file(create_report_file_name)
|
| 144 |
+
generated_html_files.append(f'./gpt_log/{create_report_file_name}')
|
| 145 |
+
except:
|
| 146 |
+
from toolbox import trimmed_format_exc
|
| 147 |
+
print('writing html result failed:', trimmed_format_exc())
|
| 148 |
+
|
| 149 |
# 准备文件的下载
|
| 150 |
import shutil
|
| 151 |
for pdf_path in generated_conclusion_files:
|
| 152 |
# 重命名文件
|
| 153 |
+
rename_file = f'./gpt_log/翻译-{os.path.basename(pdf_path)}'
|
| 154 |
if os.path.exists(rename_file):
|
| 155 |
os.remove(rename_file)
|
| 156 |
shutil.copyfile(pdf_path, rename_file)
|
| 157 |
if os.path.exists(pdf_path):
|
| 158 |
os.remove(pdf_path)
|
| 159 |
+
for html_path in generated_html_files:
|
| 160 |
+
# 重命名文件
|
| 161 |
+
rename_file = f'./gpt_log/翻译-{os.path.basename(html_path)}'
|
| 162 |
+
if os.path.exists(rename_file):
|
| 163 |
+
os.remove(rename_file)
|
| 164 |
+
shutil.copyfile(html_path, rename_file)
|
| 165 |
+
if os.path.exists(html_path):
|
| 166 |
+
os.remove(html_path)
|
| 167 |
+
chatbot.append(("给出输出文件清单", str(generated_conclusion_files + generated_html_files)))
|
| 168 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
class construct_html():
|
| 172 |
+
def __init__(self) -> None:
|
| 173 |
+
self.css = """
|
| 174 |
+
.row {
|
| 175 |
+
display: flex;
|
| 176 |
+
flex-wrap: wrap;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
.column {
|
| 180 |
+
flex: 1;
|
| 181 |
+
padding: 10px;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
.table-header {
|
| 185 |
+
font-weight: bold;
|
| 186 |
+
border-bottom: 1px solid black;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
.table-row {
|
| 190 |
+
border-bottom: 1px solid lightgray;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
.table-cell {
|
| 194 |
+
padding: 5px;
|
| 195 |
+
}
|
| 196 |
+
"""
|
| 197 |
+
self.html_string = f'<!DOCTYPE html><head><meta charset="utf-8"><title>翻译结果</title><style>{self.css}</style></head>'
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def add_row(self, a, b):
|
| 201 |
+
tmp = """
|
| 202 |
+
<div class="row table-row">
|
| 203 |
+
<div class="column table-cell">REPLACE_A</div>
|
| 204 |
+
<div class="column table-cell">REPLACE_B</div>
|
| 205 |
+
</div>
|
| 206 |
+
"""
|
| 207 |
+
from toolbox import markdown_convertion
|
| 208 |
+
tmp = tmp.replace('REPLACE_A', markdown_convertion(a))
|
| 209 |
+
tmp = tmp.replace('REPLACE_B', markdown_convertion(b))
|
| 210 |
+
self.html_string += tmp
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def save_file(self, file_name):
|
| 214 |
+
with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f:
|
| 215 |
+
f.write(self.html_string.encode('utf-8', 'ignore').decode())
|
| 216 |
+
|
toolbox.py
CHANGED
|
@@ -168,14 +168,17 @@ def write_results_to_file(history, file_name=None):
|
|
| 168 |
with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f:
|
| 169 |
f.write('# chatGPT 分析报告\n')
|
| 170 |
for i, content in enumerate(history):
|
| 171 |
-
try:
|
| 172 |
-
if type(content) != str:
|
| 173 |
-
content = str(content)
|
| 174 |
except:
|
| 175 |
continue
|
| 176 |
if i % 2 == 0:
|
| 177 |
f.write('## ')
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
f.write('\n\n')
|
| 180 |
res = '以上材料已经被写入' + os.path.abspath(f'./gpt_log/{file_name}')
|
| 181 |
print(res)
|
|
|
|
| 168 |
with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f:
|
| 169 |
f.write('# chatGPT 分析报告\n')
|
| 170 |
for i, content in enumerate(history):
|
| 171 |
+
try:
|
| 172 |
+
if type(content) != str: content = str(content)
|
|
|
|
| 173 |
except:
|
| 174 |
continue
|
| 175 |
if i % 2 == 0:
|
| 176 |
f.write('## ')
|
| 177 |
+
try:
|
| 178 |
+
f.write(content)
|
| 179 |
+
except:
|
| 180 |
+
# remove everything that cannot be handled by utf8
|
| 181 |
+
f.write(content.encode('utf-8', 'ignore').decode())
|
| 182 |
f.write('\n\n')
|
| 183 |
res = '以上材料已经被写入' + os.path.abspath(f'./gpt_log/{file_name}')
|
| 184 |
print(res)
|