閱讀論文可以說是我們的日常工作之一,論文的數(shù)量太多,我們?nèi)绾慰焖匍喿x歸納呢?自從ChatGPT出現(xiàn)以后,有很多閱讀論文的服務(wù)可以使用。其實(shí)使用ChatGPT API非常簡單,我們只用30行python代碼就可以在本地搭建一個自己的應(yīng)用。
閱讀論文可以說是我們的日常工作之一,論文的數(shù)量太多,我們?nèi)绾慰焖匍喿x歸納呢?自從ChatGPT出現(xiàn)以后,有很多閱讀論文的服務(wù)可以使用。其實(shí)使用ChatGPT API非常簡單,我們只用30行python代碼就可以在本地搭建一個自己的應(yīng)用。

使用 Python 和 ChatGPT API 總結(jié)論文的步驟很簡單:
- 用于 PDF 處理的 PyPDF2 和用于與 GPT-3.5-turbo 接口的 OpenAI。
- 使用 PyPDF2 打開并閱讀 PDF 文件。
- 遍歷 PDF 文檔中的每一頁,提取文本。
- 使用 GPT-3.5-turbo 為每個頁面的文本生成摘要。
- 合并摘要并將最終摘要文本保存到文件中。
import PyPDF2
import openai
pdf_summary_text = ""
解析pdf
pdf_file_path = "./pdfs/paper.pdf"
pdf_file = open(pdf_file_path, 'rb')
pdf_reader = PyPDF2.PdfReader(pdf_file)
獲取每一頁的文本:
for page_num in range(len(pdf_reader.pages)):
page_text = pdf_reader.pages[page_num].extract_text().lower()
使用openai的api進(jìn)行匯總
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful research assistant."},
{"role": "user", "content": f"Summarize this: {page_text}"},
],
)
page_summary = response["choices"][0]["message"]["content"]
合并摘要
pdf_summary_text += page_summary + "\n"
pdf_summary_file = pdf_file_path.replace(os.path.splitext(pdf_file_path)[1], "_summary.txt")
with open(pdf_summary_file, "w+") as file:
file.write(pdf_summary_text)
搞定,關(guān)閉pdf文件,回收內(nèi)存
完整代碼如下:
import os
import PyPDF2
import re
import openai
# Here I assume you are on a Jupiter Notebook and download the paper directly from the URL
!curl -o paper.pdf https://arxiv.org/pdf/2301.00810v3.pdf?utm_source=pocket_saves
# Set the string that will contain the summary
pdf_summary_text = ""
# Open the PDF file
pdf_file_path = "paper.pdf"
# Read the PDF file using PyPDF2
pdf_file = open(pdf_file_path, 'rb')
pdf_reader = PyPDF2.PdfReader(pdf_file)
# Loop through all the pages in the PDF file
for page_num in range(len(pdf_reader.pages)):
# Extract the text from the page
page_text = pdf_reader.pages[page_num].extract_text().lower()
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful research assistant."},
{"role": "user", "content": f"Summarize this: {page_text}"},
],
)
page_summary = response["choices"][0]["message"]["content"]
pdf_summary_text+=page_summary + "\n"
pdf_summary_file = pdf_file_path.replace(os.path.splitext(pdf_file_path)[1], "_summary.txt")
with open(pdf_summary_file, "w+") as file:
file.write(pdf_summary_text)
pdf_file.close()
with open(pdf_summary_file, "r") as file:
print(file.read())
需要說明的是2個事情:
1、openai的API免費(fèi)調(diào)用額度是有限的,這個方法一篇論文大概在0.2-0.5美元左右,根據(jù)論文長度會有變化
2、gpt4的API我沒測試,因為我還沒有申請到,并且看價格那個太貴了(貴20倍)我覺得不值,但是可以試試把論文的圖表一同傳過去,是不是會有更好效果(不確定)