Home Technology peripherals AI 30 lines of Python code can call the ChatGPT API to summarize the main content of the paper

30 lines of Python code can call the ChatGPT API to summarize the main content of the paper

Apr 04, 2023 pm 12:05 PM
python AI chatgpt

Reading papers can be said to be one of our daily tasks. There are too many papers. How can we read and summarize them quickly? Since the emergence of ChatGPT, there are many services available for reading papers. In fact, using the ChatGPT API is very simple. We can build our own application locally with only 30 lines of python code.

Reading papers can be said to be one of our daily tasks. There are too many papers. How can we read and summarize them quickly? Since the emergence of ChatGPT, there are many services available for reading papers. In fact, using the ChatGPT API is very simple. We can build our own application locally with only 30 lines of python code.

30 lines of Python code can call the ChatGPT API to summarize the main content of the paper

The steps to summarize the paper using Python and the ChatGPT API are simple:

  • PyPDF2 for PDF processing and GPT-3.5- OpenAI with turbo interface.
  • Use PyPDF2 to open and read PDF files.
  • Traverse each page in the PDF document and extract text.
  • Use GPT-3.5-turbo to generate summaries for each page's text.
  • Merge summaries and save the final summary text to a file.

import PyPDF2
import openai
pdf_summary_text = ""

Parse pdf

pdf_file_path = "./pdfs/paper.pdf"
pdf_file = open(pdf_file_path, 'rb')
pdf_reader = PyPDF2.PdfReader(pdf_file)

Get the text of each page:

for page_num in range(len(pdf_reader. pages)):
page_text = pdf_reader.pages[page_num].extract_text().lower()

Use openai’s api for summary

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"]

Merge summary

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)

Done, close the pdf file and recycle memory

pdf_file.close( )

The complete code is as follows:

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

There are 2 things to note Things:

1. Openai’s free API call limit is limited. A paper with this method costs about 0.2-0.5 US dollars, which will vary depending on the length of the paper.

2. Gpt4’s API I haven't tested it because I haven't applied for it yet, and the price is too expensive (20 times more expensive). I don't think it's worth it, but you can try to upload the charts of the paper to see if it will have better results (not sure) )

The above is the detailed content of 30 lines of Python code can call the ChatGPT API to summarize the main content of the paper. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

Can visual studio code run python Can visual studio code run python Apr 15, 2025 pm 08:00 PM

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

See all articles