valeriaWong commited on
Commit
3f635bc
1 Parent(s): c06c60b

feat(读文章写摘要):支持pdf文件批量阅读及总结

Browse files
Files changed (2) hide show
  1. crazy_functions/读文章写摘要.py +54 -4
  2. main.py +1 -2
crazy_functions/读文章写摘要.py CHANGED
@@ -48,8 +48,54 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
48
  yield chatbot, history, msg
49
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- @CatchException
53
  def 读文章写摘要(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
54
  history = [] # 清空历史,以免输入溢出
55
  import glob, os
@@ -60,11 +106,15 @@ def 读文章写摘要(txt, top_p, temperature, chatbot, history, systemPromptTx
60
  report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
61
  yield chatbot, history, '正常'
62
  return
63
- file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] # + \
 
64
  # [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
65
  # [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
66
  if len(file_manifest) == 0:
67
- report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
68
  yield chatbot, history, '正常'
69
  return
70
- yield from 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
 
 
 
 
48
  yield chatbot, history, msg
49
 
50
 
51
+ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
52
+ import time, glob, os, codecs, fitz
53
+ print('begin analysis on:', file_manifest)
54
+ for index, fp in enumerate(file_manifest):
55
+ with fitz.open(fp) as doc:
56
+ file_content = ""
57
+ for page in doc:
58
+ file_content += page.getText()
59
+ print(file_content)
60
+
61
+ prefix = "接下来请你逐文件分析下面的论文文件,概括其内容" if index==0 else ""
62
+ i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
63
+ i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
64
+ chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
65
+ print('[1] yield chatbot, history')
66
+ yield chatbot, history, '正常'
67
+
68
+ if not fast_debug:
69
+ msg = '正常'
70
+ # ** gpt request **
71
+ gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]) # 带超时倒计时
72
+
73
+ print('[2] end gpt req')
74
+ chatbot[-1] = (i_say_show_user, gpt_say)
75
+ history.append(i_say_show_user); history.append(gpt_say)
76
+ print('[3] yield chatbot, history')
77
+ yield chatbot, history, msg
78
+ print('[4] next')
79
+ if not fast_debug: time.sleep(2)
80
+
81
+ all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
82
+ i_say = f'根据以上你自己的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一段英文摘要(包括{all_file})。'
83
+ chatbot.append((i_say, "[Local Message] waiting gpt response."))
84
+ yield chatbot, history, '正常'
85
+
86
+ if not fast_debug:
87
+ msg = '正常'
88
+ # ** gpt request **
89
+ gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, temperature, history=history) # 带超时倒计时
90
+
91
+ chatbot[-1] = (i_say, gpt_say)
92
+ history.append(i_say); history.append(gpt_say)
93
+ yield chatbot, history, msg
94
+ res = write_results_to_file(history)
95
+ chatbot.append(("完成了吗?", res))
96
+ yield chatbot, history, msg
97
+
98
 
 
99
  def 读文章写摘要(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
100
  history = [] # 清空历史,以免输入溢出
101
  import glob, os
 
106
  report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
107
  yield chatbot, history, '正常'
108
  return
109
+ file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] + \
110
+ [f for f in glob.glob(f'{project_folder}/**/*.pdf', recursive=True)] # + \
111
  # [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
112
  # [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
113
  if len(file_manifest) == 0:
114
+ report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex或.pdf文件: {txt}")
115
  yield chatbot, history, '正常'
116
  return
117
+ if '.pdf' in file_manifest[0]:
118
+ yield from 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
119
+ else:
120
+ yield from 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
main.py CHANGED
@@ -4,8 +4,7 @@ from predict import predict
4
  from toolbox import format_io, find_free_port
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
- try: from config_private import proxies, WEB_PORT, LLM_MODEL
8
- except: from config import proxies, WEB_PORT, LLM_MODEL
9
 
10
  # 如果WEB_PORT是-1, 则随机选取WEB端口
11
  PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
 
4
  from toolbox import format_io, find_free_port
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
+ from config_private import proxies, WEB_PORT, LLM_MODEL
 
8
 
9
  # 如果WEB_PORT是-1, 则随机选取WEB端口
10
  PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT