閱讀論文可以說是我們的日常工作之一,論文的數量太多,我們如何快速閱讀歸納?自從ChatGPT出現以後,有許多閱讀論文的服務可以使用。其實使用ChatGPT API非常簡單,我們只用30行python程式碼就可以在本地搭建一個自己的應用程式。
閱讀論文可以說是我們的日常工作之一,論文的數量太多,我們如何快速閱讀歸納?自從ChatGPT出現以後,有許多閱讀論文的服務可以使用。其實使用ChatGPT API非常簡單,我們只用30行python程式碼就可以在本地搭建一個自己的應用程式。
使用Python 和ChatGPT API 總結論文的步驟很簡單:
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進行匯總
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文件,回收記憶體
pdf_file.close( )
完整程式碼如下:
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())
以上是30行Python程式碼就可以呼叫ChatGPT API總結論文的主要內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!