panda2018/ChatReviewer
0
1import numpy as np2import os3import re4from io import BytesIO5import datetime6import time7import openai, tenacity8import argparse9import configparser10import json11import tiktoken12import PyPDF213import gradio14 15# 定义Reviewer类16class Reviewer:17 # 初始化方法,设置属性18 def __init__(self, api, review_format, paper_pdf, language):19 self.api = api20 self.review_format = review_format21 22 self.language = language23 self.paper_pdf = paper_pdf24 self.max_token_num = 409725 self.encoding = tiktoken.get_encoding("gpt2")26 27 28 def review_by_chatgpt(self, paper_list):29 text = self.extract_chapter(self.paper_pdf)30 chat_review_text, total_token_used = self.chat_review(text=text) 31 return chat_review_text, total_token_used32 33 34 35 @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=4, max=10),36 stop=tenacity.stop_after_attempt(5),37 reraise=True)38 def chat_review(self, text):39 openai.api_key = self.api # 读取api40 review_prompt_token = 1000 41 text_token = len(self.encoding.encode(text))42 input_text_index = int(len(text)*(self.max_token_num-review_prompt_token)/text_token)43 input_text = "This is the paper for your review:" + text[:input_text_index]44 messages=[45 {"role": "system", "content": "You are a professional reviewer. Now I will give you a paper. You need to give a complete review opinion according to the following requirements and format:"+ self.review_format +" Must be output in {}.".format(self.language)},46 {"role": "user", "content": input_text},47 ]48 49 response = openai.ChatCompletion.create(50 model="gpt-3.5-turbo",51 messages=messages,52 )53 result = ''54 for choice in response.choices:55 result += choice.message.content56 print("********"*10)57 print(result)58 print("********"*10)59 print("prompt_token_used:", response.usage.prompt_tokens)60 print("completion_token_used:", response.usage.completion_tokens)61 print("total_token_used:", response.usage.total_tokens)62 print("response_time:", response.response_ms/1000.0, 's') 63 return result, response.usage.total_tokens 64 65 def extract_chapter(self, pdf_path):66 file_object = BytesIO(pdf_path)67 # 创建一个PDF阅读器对象68 pdf_reader = PyPDF2.PdfReader(file_object)69 # 获取PDF的总页数70 num_pages = len(pdf_reader.pages)71 # 初始化提取状态和提取文本72 extraction_started = False73 extracted_text = ""74 # 遍历PDF中的每一页75 for page_number in range(num_pages):76 page = pdf_reader.pages[page_number]77 page_text = page.extract_text()78 79 # 如果找到了章节标题,开始提取80 if 'Abstract'.lower() in page_text.lower() and not extraction_started:81 extraction_started = True82 page_number_start = page_number83 # 如果提取已开始,将页面文本添加到提取文本中84 if extraction_started:85 extracted_text += page_text86 # 如果找到下一章节标题,停止提取87 if page_number_start + 1 < page_number:88 break89 return extracted_text90 91def main(api, review_format, paper_pdf, language): 92 start_time = time.time()93 if not api or not review_format or not paper_pdf:94 return "请输入完整内容!"95 # 判断PDF文件96 else:97 # 创建一个Reader对象98 reviewer1 = Reviewer(api, review_format, paper_pdf, language)99 # 开始判断是路径还是文件: 100 comments, total_token_used = reviewer1.review_by_chatgpt(paper_list=paper_pdf)101 time_used = time.time() - start_time102 output2 ="使用token数:"+ str(total_token_used)+"\n花费时间:"+ str(round(time_used, 2)) +"秒"103 return comments, output2104 105 106 107######################################################################################################## 108# 标题109title = "🤖ChatReviewer🤖"110# 描述111 112description = '''<div align='left'>113<img align='right' src='http://i.imgtg.com/2023/03/22/94PLN.png' width="270">114 115<strong>ChatReviewer是一款基于ChatGPT-3.5的API开发的论文自动评审AI助手。</strong>其用途如下:116 117⭐️对论文进行快速总结和评审,提高科研人员的文献阅读和理解的效率,紧跟研究前沿。118 119⭐️对自己的论文进行评审,根据ChatReviewer生成的审稿意见进行查漏补缺,进一步提高自己的论文质量。120 121⭐️辅助论文审稿,给出参考意见,提高审稿效率和质量。(🈲:禁止直接复制生成的评论用于任何论文审稿工作!)122 123如果觉得很卡,可以点击右上角的Duplicate this Space,把ChatReviewer复制到你自己的Space中!124 125本项目的[Github](https://github.com/nishiwen1214/ChatReviewer),欢迎Star和Fork,也欢迎大佬赞助让本项目快速成长!💗([获取Api Key](https://chatgpt.cn.obiscr.com/blog/posts/2023/How-to-get-api-key/))126</div>127'''128 129# 创建Gradio界面130inp = [gradio.inputs.Textbox(label="请输入你的API-key(sk开头的字符串)",131 default="",132 type='password'),133 gradio.inputs.Textbox(lines=5,134 label="请输入特定的评审要求和格式(否则为默认格式)",135 default="""* Overall Review136Please briefly summarize the main points and contributions of this paper.137xxx138* Paper Strength 139Please provide a list of the strengths of this paper, including but not limited to: innovative and practical methodology, insightful empirical findings or in-depth theoretical analysis, 140well-structured review of relevant literature, and any other factors that may make the paper valuable to readers. (Maximum length: 2,000 characters) 141(1) xxx142(2) xxx143(3) xxx144* Paper Weakness 145Please provide a numbered list of your main concerns regarding this paper (so authors could respond to the concerns individually). 146These may include, but are not limited to: inadequate implementation details for reproducing the study, limited evaluation and ablation studies for the proposed method, 147correctness of the theoretical analysis or experimental results, lack of comparisons or discussions with widely-known baselines in the field, lack of clarity in exposition, 148or any other factors that may impede the reader's understanding or benefit from the paper. Please kindly refrain from providing a general assessment of the paper's novelty without providing detailed explanations. (Maximum length: 2,000 characters) 149(1) xxx150(2) xxx151(3) xxx152* Questions To Authors And Suggestions For Rebuttal 153Please provide a numbered list of specific and clear questions that pertain to the details of the proposed method, evaluation setting, or additional results that would aid in supporting the authors' claims. 154The questions should be formulated in a manner that, after the authors have answered them during the rebuttal, it would enable a more thorough assessment of the paper's quality. (Maximum length: 2,000 characters)155*Overall score (1-10)156The paper is scored on a scale of 1-10, with 10 being the full mark, and 6 stands for borderline accept. Then give the reason for your rating.157xxx"""158 ),159 gradio.inputs.File(label="请上传论文PDF(必填)",type="bytes"),160 gradio.inputs.Radio(choices=["English", "Chinese"],161 default="English",162 label="选择输出语言"),163]164 165chat_reviewer_gui = gradio.Interface(fn=main,166 inputs=inp,167 outputs = [gradio.Textbox(lines=25, label="评审结果"), gradio.Textbox(lines=2, label="资源统计")],168 title=title,169 description=description)170 171# Start server172chat_reviewer_gui .launch(quiet=True, show_api=False)