Lua工程解析+修正注释
Browse files- crazy_functional.py +7 -9
- crazy_functions/crazy_utils.py +18 -8
- crazy_functions/理解PDF文档内容.py +7 -6
crazy_functional.py
CHANGED
|
@@ -16,7 +16,7 @@ def get_crazy_functions():
|
|
| 16 |
from crazy_functions.高级功能函数模板 import 高阶功能模板函数
|
| 17 |
from crazy_functions.代码重写为全英文_多线程 import 全项目切换英文
|
| 18 |
from crazy_functions.Latex全文润色 import Latex英文润色
|
| 19 |
-
|
| 20 |
function_plugins = {
|
| 21 |
|
| 22 |
"解析整个Python项目": {
|
|
@@ -47,6 +47,11 @@ def get_crazy_functions():
|
|
| 47 |
"AsButton": False, # 加入下拉菜单中
|
| 48 |
"Function": HotReload(解析一个Rect项目)
|
| 49 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
"读Tex论文写摘要": {
|
| 51 |
"Color": "stop", # 按钮颜色
|
| 52 |
"Function": HotReload(读文章写摘要)
|
|
@@ -156,14 +161,7 @@ def get_crazy_functions():
|
|
| 156 |
except Exception as err:
|
| 157 |
print(f'[下载arxiv论文并翻译摘要] 插件导入失败 {str(err)}')
|
| 158 |
|
| 159 |
-
|
| 160 |
-
function_plugins.update({
|
| 161 |
-
"解析整个Lua项目": {
|
| 162 |
-
"Color": "stop", # 按钮颜色
|
| 163 |
-
"AsButton": False, # 加入下拉菜单中
|
| 164 |
-
"Function": HotReload(解析一个Lua项目)
|
| 165 |
-
},
|
| 166 |
-
})
|
| 167 |
|
| 168 |
###################### 第n组插件 ###########################
|
| 169 |
return function_plugins
|
|
|
|
| 16 |
from crazy_functions.高级功能函数模板 import 高阶功能模板函数
|
| 17 |
from crazy_functions.代码重写为全英文_多线程 import 全项目切换英文
|
| 18 |
from crazy_functions.Latex全文润色 import Latex英文润色
|
| 19 |
+
from crazy_functions.解析项目源代码 import 解析一个Lua项目
|
| 20 |
function_plugins = {
|
| 21 |
|
| 22 |
"解析整个Python项目": {
|
|
|
|
| 47 |
"AsButton": False, # 加入下拉菜单中
|
| 48 |
"Function": HotReload(解析一个Rect项目)
|
| 49 |
},
|
| 50 |
+
"解析整个Lua项目": {
|
| 51 |
+
"Color": "stop", # 按钮颜色
|
| 52 |
+
"AsButton": False, # 加入下拉菜单中
|
| 53 |
+
"Function": HotReload(解析一个Lua项目)
|
| 54 |
+
},
|
| 55 |
"读Tex论文写摘要": {
|
| 56 |
"Color": "stop", # 按钮颜色
|
| 57 |
"Function": HotReload(读文章写摘要)
|
|
|
|
| 161 |
except Exception as err:
|
| 162 |
print(f'[下载arxiv论文并翻译摘要] 插件导入失败 {str(err)}')
|
| 163 |
|
| 164 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
###################### 第n组插件 ###########################
|
| 167 |
return function_plugins
|
crazy_functions/crazy_utils.py
CHANGED
|
@@ -387,12 +387,15 @@ def read_and_clean_pdf_text(fp):
|
|
| 387 |
import re
|
| 388 |
import numpy as np
|
| 389 |
from colorful import print亮黄, print亮绿
|
| 390 |
-
fc = 0
|
| 391 |
-
fs = 1
|
| 392 |
-
fb = 2
|
| 393 |
-
REMOVE_FOOT_NOTE = True
|
| 394 |
-
REMOVE_FOOT_FFSIZE_PERCENT = 0.95
|
| 395 |
def primary_ffsize(l):
|
|
|
|
|
|
|
|
|
|
| 396 |
fsize_statiscs = {}
|
| 397 |
for wtf in l['spans']:
|
| 398 |
if wtf['size'] not in fsize_statiscs: fsize_statiscs[wtf['size']] = 0
|
|
@@ -400,14 +403,18 @@ def read_and_clean_pdf_text(fp):
|
|
| 400 |
return max(fsize_statiscs, key=fsize_statiscs.get)
|
| 401 |
|
| 402 |
def ffsize_same(a,b):
|
|
|
|
|
|
|
|
|
|
| 403 |
return abs((a-b)/max(a,b)) < 0.02
|
| 404 |
-
|
| 405 |
with fitz.open(fp) as doc:
|
| 406 |
meta_txt = []
|
| 407 |
meta_font = []
|
| 408 |
|
| 409 |
meta_line = []
|
| 410 |
meta_span = []
|
|
|
|
| 411 |
for index, page in enumerate(doc):
|
| 412 |
# file_content += page.get_text()
|
| 413 |
text_areas = page.get_text("dict") # 获取页面上的文本信息
|
|
@@ -429,7 +436,8 @@ def read_and_clean_pdf_text(fp):
|
|
| 429 |
if index == 0:
|
| 430 |
page_one_meta = [" ".join(["".join([wtf['text'] for wtf in l['spans']]) for l in t['lines']]).replace(
|
| 431 |
'- ', '') for t in text_areas['blocks'] if 'lines' in t]
|
| 432 |
-
|
|
|
|
| 433 |
fsize_statiscs = {}
|
| 434 |
for span in meta_span:
|
| 435 |
if span[1] not in fsize_statiscs: fsize_statiscs[span[1]] = 0
|
|
@@ -438,7 +446,7 @@ def read_and_clean_pdf_text(fp):
|
|
| 438 |
if REMOVE_FOOT_NOTE:
|
| 439 |
give_up_fize_threshold = main_fsize * REMOVE_FOOT_FFSIZE_PERCENT
|
| 440 |
|
| 441 |
-
|
| 442 |
mega_sec = []
|
| 443 |
sec = []
|
| 444 |
for index, line in enumerate(meta_line):
|
|
@@ -480,6 +488,7 @@ def read_and_clean_pdf_text(fp):
|
|
| 480 |
finals.append(final)
|
| 481 |
meta_txt = finals
|
| 482 |
|
|
|
|
| 483 |
def 把字符太少的块清除为回车(meta_txt):
|
| 484 |
for index, block_txt in enumerate(meta_txt):
|
| 485 |
if len(block_txt) < 100:
|
|
@@ -523,6 +532,7 @@ def read_and_clean_pdf_text(fp):
|
|
| 523 |
# 换行 -> 双换行
|
| 524 |
meta_txt = meta_txt.replace('\n', '\n\n')
|
| 525 |
|
|
|
|
| 526 |
for f in finals:
|
| 527 |
print亮黄(f)
|
| 528 |
print亮绿('***************************')
|
|
|
|
| 387 |
import re
|
| 388 |
import numpy as np
|
| 389 |
from colorful import print亮黄, print亮绿
|
| 390 |
+
fc = 0 # Index 0 文本
|
| 391 |
+
fs = 1 # Index 1 字体
|
| 392 |
+
fb = 2 # Index 2 框框
|
| 393 |
+
REMOVE_FOOT_NOTE = True # 是否丢弃掉 不是正文的内容 (比正文字体小,如参考文献、脚注、图注等)
|
| 394 |
+
REMOVE_FOOT_FFSIZE_PERCENT = 0.95 # 小于正文的?时,判定为不是正文(有些文章的正文部分字体大小不是100%统一的,有肉眼不可见的小变化)
|
| 395 |
def primary_ffsize(l):
|
| 396 |
+
"""
|
| 397 |
+
提取文本块主字体
|
| 398 |
+
"""
|
| 399 |
fsize_statiscs = {}
|
| 400 |
for wtf in l['spans']:
|
| 401 |
if wtf['size'] not in fsize_statiscs: fsize_statiscs[wtf['size']] = 0
|
|
|
|
| 403 |
return max(fsize_statiscs, key=fsize_statiscs.get)
|
| 404 |
|
| 405 |
def ffsize_same(a,b):
|
| 406 |
+
"""
|
| 407 |
+
提取字体大小是否近似相等
|
| 408 |
+
"""
|
| 409 |
return abs((a-b)/max(a,b)) < 0.02
|
| 410 |
+
|
| 411 |
with fitz.open(fp) as doc:
|
| 412 |
meta_txt = []
|
| 413 |
meta_font = []
|
| 414 |
|
| 415 |
meta_line = []
|
| 416 |
meta_span = []
|
| 417 |
+
############################## <第 1 步,搜集初始信息> ##################################
|
| 418 |
for index, page in enumerate(doc):
|
| 419 |
# file_content += page.get_text()
|
| 420 |
text_areas = page.get_text("dict") # 获取页面上的文本信息
|
|
|
|
| 436 |
if index == 0:
|
| 437 |
page_one_meta = [" ".join(["".join([wtf['text'] for wtf in l['spans']]) for l in t['lines']]).replace(
|
| 438 |
'- ', '') for t in text_areas['blocks'] if 'lines' in t]
|
| 439 |
+
|
| 440 |
+
############################## <第 2 步,获取正文主字体> ##################################
|
| 441 |
fsize_statiscs = {}
|
| 442 |
for span in meta_span:
|
| 443 |
if span[1] not in fsize_statiscs: fsize_statiscs[span[1]] = 0
|
|
|
|
| 446 |
if REMOVE_FOOT_NOTE:
|
| 447 |
give_up_fize_threshold = main_fsize * REMOVE_FOOT_FFSIZE_PERCENT
|
| 448 |
|
| 449 |
+
############################## <第 3 步,切分和重新整合> ##################################
|
| 450 |
mega_sec = []
|
| 451 |
sec = []
|
| 452 |
for index, line in enumerate(meta_line):
|
|
|
|
| 488 |
finals.append(final)
|
| 489 |
meta_txt = finals
|
| 490 |
|
| 491 |
+
############################## <第 4 步,乱七八糟的后处理> ##################################
|
| 492 |
def 把字符太少的块清除为回车(meta_txt):
|
| 493 |
for index, block_txt in enumerate(meta_txt):
|
| 494 |
if len(block_txt) < 100:
|
|
|
|
| 532 |
# 换行 -> 双换行
|
| 533 |
meta_txt = meta_txt.replace('\n', '\n\n')
|
| 534 |
|
| 535 |
+
############################## <第 5 步,展示分割效果> ##################################
|
| 536 |
for f in finals:
|
| 537 |
print亮黄(f)
|
| 538 |
print亮绿('***************************')
|
crazy_functions/理解PDF文档内容.py
CHANGED
|
@@ -8,11 +8,12 @@ fast_debug = False
|
|
| 8 |
def 解析PDF(file_name, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 9 |
import tiktoken
|
| 10 |
print('begin analysis on:', file_name)
|
| 11 |
-
file_content, page_one = read_and_clean_pdf_text(file_name)
|
| 12 |
|
| 13 |
-
##############################
|
| 14 |
# 递归地切割PDF文件,每一块(尽量是完整的一个section,比如introduction,experiment等,必要时再进行切割)
|
| 15 |
# 的长度必须小于 2500 个 Token
|
|
|
|
|
|
|
| 16 |
TOKEN_LIMIT_PER_FRAGMENT = 2500
|
| 17 |
|
| 18 |
from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
|
|
@@ -26,11 +27,11 @@ def 解析PDF(file_name, llm_kwargs, plugin_kwargs, chatbot, history, system_pro
|
|
| 26 |
# 为了更好的效果,我们剥离Introduction之后的部分(如果有)
|
| 27 |
paper_meta = page_one_fragments[0].split('introduction')[0].split('Introduction')[0].split('INTRODUCTION')[0]
|
| 28 |
|
| 29 |
-
##############################
|
| 30 |
final_results = []
|
| 31 |
final_results.append(paper_meta)
|
| 32 |
|
| 33 |
-
##############################
|
| 34 |
i_say_show_user = f'首先你在英文语境下通读整篇论文。'; gpt_say = "[Local Message] 收到。" # 用户提示
|
| 35 |
chatbot.append([i_say_show_user, gpt_say]); yield from update_ui(chatbot=chatbot, history=[]) # 更新UI
|
| 36 |
|
|
@@ -51,14 +52,14 @@ def 解析PDF(file_name, llm_kwargs, plugin_kwargs, chatbot, history, system_pro
|
|
| 51 |
iteration_results.append(gpt_say)
|
| 52 |
last_iteration_result = gpt_say
|
| 53 |
|
| 54 |
-
##############################
|
| 55 |
final_results.extend(iteration_results)
|
| 56 |
final_results.append(f'接下来,你是一名专业的学术教授,利用以上信息,使用中文回答我的问题。')
|
| 57 |
# 接下来两句话只显示在界面上,不起实际作用
|
| 58 |
i_say_show_user = f'接下来,你是一名专业的学术教授,利用以上信息,使用中文回答我的问题。'; gpt_say = "[Local Message] 收到。"
|
| 59 |
chatbot.append([i_say_show_user, gpt_say])
|
| 60 |
|
| 61 |
-
##############################
|
| 62 |
from .crazy_utils import input_clipping
|
| 63 |
_, final_results = input_clipping("", final_results, max_token_limit=3200)
|
| 64 |
yield from update_ui(chatbot=chatbot, history=final_results) # 注意这里的历史记录被替代了
|
|
|
|
| 8 |
def 解析PDF(file_name, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
| 9 |
import tiktoken
|
| 10 |
print('begin analysis on:', file_name)
|
|
|
|
| 11 |
|
| 12 |
+
############################## <第 0 步,切割PDF> ##################################
|
| 13 |
# 递归地切割PDF文件,每一块(尽量是完整的一个section,比如introduction,experiment等,必要时再进行切割)
|
| 14 |
# 的长度必须小于 2500 个 Token
|
| 15 |
+
file_content, page_one = read_and_clean_pdf_text(file_name) # (尝试)按照章节切割PDF
|
| 16 |
+
|
| 17 |
TOKEN_LIMIT_PER_FRAGMENT = 2500
|
| 18 |
|
| 19 |
from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
|
|
|
|
| 27 |
# 为了更好的效果,我们剥离Introduction之后的部分(如果有)
|
| 28 |
paper_meta = page_one_fragments[0].split('introduction')[0].split('Introduction')[0].split('INTRODUCTION')[0]
|
| 29 |
|
| 30 |
+
############################## <第 1 步,从摘要中提取高价值信息,放到history中> ##################################
|
| 31 |
final_results = []
|
| 32 |
final_results.append(paper_meta)
|
| 33 |
|
| 34 |
+
############################## <第 2 步,迭代地历遍整个文章,提取精炼信息> ##################################
|
| 35 |
i_say_show_user = f'首先你在英文语境下通读整篇论文。'; gpt_say = "[Local Message] 收到。" # 用户提示
|
| 36 |
chatbot.append([i_say_show_user, gpt_say]); yield from update_ui(chatbot=chatbot, history=[]) # 更新UI
|
| 37 |
|
|
|
|
| 52 |
iteration_results.append(gpt_say)
|
| 53 |
last_iteration_result = gpt_say
|
| 54 |
|
| 55 |
+
############################## <第 3 步,整理history> ##################################
|
| 56 |
final_results.extend(iteration_results)
|
| 57 |
final_results.append(f'接下来,你是一名专业的学术教授,利用以上信息,使用中文回答我的问题。')
|
| 58 |
# 接下来两句话只显示在界面上,不起实际作用
|
| 59 |
i_say_show_user = f'接下来,你是一名专业的学术教授,利用以上信息,使用中文回答我的问题。'; gpt_say = "[Local Message] 收到。"
|
| 60 |
chatbot.append([i_say_show_user, gpt_say])
|
| 61 |
|
| 62 |
+
############################## <第 4 步,设置一个token上限,防止回答时Token溢出> ##################################
|
| 63 |
from .crazy_utils import input_clipping
|
| 64 |
_, final_results = input_clipping("", final_results, max_token_limit=3200)
|
| 65 |
yield from update_ui(chatbot=chatbot, history=final_results) # 注意这里的历史记录被替代了
|