30行Python程式碼就可以呼叫ChatGPT API總結論文的主要內容

PHPz
發布: 2023-04-04 12:05:06
轉載
1075 人瀏覽過

閱讀論文可以說是我們的日常工作之一,論文的數量太多,我們如何快速閱讀歸納?自從ChatGPT出現以後,有許多閱讀論文的服務可以使用。其實使用ChatGPT API非常簡單,我們只用30行python程式碼就可以在本地搭建一個自己的應用程式。

閱讀論文可以說是我們的日常工作之一,論文的數量太多,我們如何快速閱讀歸納?自從ChatGPT出現以後,有許多閱讀論文的服務可以使用。其實使用ChatGPT API非常簡單,我們只用30行python程式碼就可以在本地搭建一個自己的應用程式。

30行Python程式碼就可以呼叫ChatGPT API總結論文的主要內容

使用Python 和ChatGPT API 總結論文的步驟很簡單:

  • 用於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進行匯總

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())

#要說明的是2個事情:

1、openai的API免費呼叫額度是有限的,這個方法一篇論文大概在0.2-0.5美元左右,根據論文長度會有變化

2、gpt4的API我沒測試,因為我還沒申請到,並且看價格那個太貴了(貴20倍)我覺得不值,但是可以試試把論文的圖表一同傳過去,是不是會有更好效果(不確定)###

以上是30行Python程式碼就可以呼叫ChatGPT API總結論文的主要內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:51cto.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!