ShiwenNi commited on
Commit
8fc0ebc
1 Parent(s): f905ed0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -65
app.py CHANGED
@@ -8,7 +8,7 @@ import argparse
8
  import configparser
9
  import json
10
  import tiktoken
11
- from get_paper_from_pdf import Paper
12
  import gradio
13
 
14
  # 定义Reviewer类
@@ -25,69 +25,11 @@ class Reviewer:
25
 
26
 
27
  def review_by_chatgpt(self, paper_list):
28
- for paper_index, paper in enumerate(paper_list):
29
- sections_of_interest = self.stage_1(paper)
30
- # extract the essential parts of the paper
31
- text = ''
32
- try:
33
- text += 'Title:' + paper.title + '. '
34
- text += 'Abstract: ' + paper.section_texts['Abstract']
35
- except:
36
- pass
37
- intro_title = next((item for item in paper.section_names if 'ntroduction' in item.lower()), None)
38
- if intro_title is not None:
39
- text += 'Introduction: ' + paper.section_texts[intro_title]
40
- # Similar for conclusion section
41
- conclusion_title = next((item for item in paper.section_names if 'onclusion' in item), None)
42
- if conclusion_title is not None:
43
- text += 'Conclusion: ' + paper.section_texts[conclusion_title]
44
- for heading in sections_of_interest:
45
- if heading in paper.section_names:
46
- text += heading + ': ' + paper.section_texts[heading]
47
- chat_review_text, total_token_used = self.chat_review(text=text)
48
  return chat_review_text, total_token_used
49
-
50
-
51
-
52
- def stage_1(self, paper):
53
- htmls = []
54
- text = ''
55
- paper_Abstract = 'Abstract'
56
- try:
57
- text += 'Title:' + paper.title + '. '
58
- paper_Abstract = paper.section_texts['Abstract']
59
-
60
- except:
61
- pass
62
- text += 'Abstract: ' + paper_Abstract
63
- text_token = len(self.encoding.encode(text))
64
- if text_token > (self.max_token_num/2) - 1000:
65
- input_text_index = int(len(text)*((self.max_token_num/2)-1200)/text_token)
66
- text = text[:input_text_index]
67
- openai.api_key = self.api
68
- messages = [
69
- {"role": "system",
70
- "content": f"You are a professional reviewer. "
71
- f"I will give you a paper. You need to review this paper and discuss the novelty and originality of ideas, correctness, clarity, the significance of results, potential impact and quality of the presentation. "
72
- f"Due to the length limitations, I am only allowed to provide you the abstract, introduction, conclusion and at most two sections of this paper."
73
- f"Now I will give you the title and abstract and the headings of potential sections. "
74
- f"You need to reply at most two headings. Then I will further provide you the full information, includes aforementioned sections and at most two sections you called for.\n\n"
75
- f"Title: {paper.title}\n\n"
76
- f"Abstract: {paper_Abstract}\n\n"
77
- f"Potential Sections: {paper.section_names[2:-1]}\n\n"
78
- f"Follow the following format to output your choice of sections:"
79
- f"{{chosen section 1}}, {{chosen section 2}}\n\n"},
80
- {"role": "user", "content": text},
81
- ]
82
- response = openai.ChatCompletion.create(
83
- model="gpt-3.5-turbo",
84
- messages=messages,
85
- )
86
- result = ''
87
- for choice in response.choices:
88
- result += choice.message.content
89
- # print(result)
90
- return result.split(',')
91
 
92
  @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=4, max=10),
93
  stop=tenacity.stop_after_attempt(5),
@@ -118,7 +60,32 @@ class Reviewer:
118
  print("total_token_used:", response.usage.total_tokens)
119
  print("response_time:", response.response_ms/1000.0, 's')
120
  return result, response.usage.total_tokens
121
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  def main(api, review_format, paper_pdf, language):
124
  start_time = time.time()
@@ -126,7 +93,7 @@ def main(api, review_format, paper_pdf, language):
126
  return "请输入完整内容!"
127
  # 判断PDF文件
128
  else:
129
- paper_list = [Paper(path=paper_pdf)]
130
  # 创建一个Reader对象
131
  reviewer1 = Reviewer(api, review_format, paper_pdf, language)
132
  # 开始判断是路径还是文件:
 
8
  import configparser
9
  import json
10
  import tiktoken
11
+ import PyPDF2
12
  import gradio
13
 
14
  # 定义Reviewer类
 
25
 
26
 
27
  def review_by_chatgpt(self, paper_list):
28
+ text = extract_chapter(paper_list)
29
+ chat_review_text, total_token_used = self.chat_review(text=text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  return chat_review_text, total_token_used
31
+
32
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=4, max=10),
35
  stop=tenacity.stop_after_attempt(5),
 
60
  print("total_token_used:", response.usage.total_tokens)
61
  print("response_time:", response.response_ms/1000.0, 's')
62
  return result, response.usage.total_tokens
63
+
64
+ def extract_chapter(self, pdf_path):
65
+ with open(pdf_path, 'rb') as file:
66
+ # 创建一个PDF阅读器对象
67
+ pdf_reader = PyPDF2.PdfReader(file)
68
+ # 获取PDF的总页数
69
+ num_pages = len(pdf_reader.pages)
70
+ # 初始化提取状态和提取文本
71
+ extraction_started = False
72
+ extracted_text = ""
73
+ # 遍历PDF中的每一页
74
+ for page_number in range(num_pages):
75
+ page = pdf_reader.pages[page_number]
76
+ page_text = page.extract_text()
77
+
78
+ # 如果找到了章节标题,开始提取
79
+ if 'Abstract'.lower() in page_text.lower() and not extraction_started:
80
+ extraction_started = True
81
+ page_number_start = page_number
82
+ # 如果提取已开始,将页面文本添加到提取文本中
83
+ if extraction_started:
84
+ extracted_text += page_text
85
+ # 如果找到下一章节标题,停止提取
86
+ if page_number_start + 1 < page_number:
87
+ break
88
+ return extracted_text
89
 
90
  def main(api, review_format, paper_pdf, language):
91
  start_time = time.time()
 
93
  return "请输入完整内容!"
94
  # 判断PDF文件
95
  else:
96
+ paper_list = paper_pdf
97
  # 创建一个Reader对象
98
  reviewer1 = Reviewer(api, review_format, paper_pdf, language)
99
  # 开始判断是路径还是文件: