Skip to main content

OpenAI Summarize

OpenAI Summarize is a Python package that generates summaries of text using OpenAI's text-davinci-003 model. It chunks the input text into smaller pieces and generates summaries for each chunk separately using the OpenAI API. The generated summaries are then combined into a final summary. If the final summary is too long, it is recursively summarized until it reaches the desired length.

Installation

OpenAI Summarize can be installed from PyPI using pip. Simply run:

pip install openai-summarize

Alternatively, you can install OpenAI Summarize from Git by cloning the repository and running setup.py:

git clone https://github.com/kixpanganiban/openai_summarize.git
cd openai-summarize
python setup.py install

Usage

import openai_summarize

openai_summarizer = openai_summarize.OpenAISummarize("your_openai_key")

text = "This is a long piece of text that needs to be summarized."
summary = openai_summarizer.summarize_text(text)

print(summary)

Examples

Here's an example of how to use OpenAI Summarize to summarize a long piece of text:

import openai_summarize

openai_summarizer = openai_summarize.OpenAISummarize("your_openai_key")

text = """The COVID-19 pandemic, also known as the coronavirus pandemic, is an ongoing pandemic of coronavirus disease 2019 (COVID-19), caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2). The virus was first identified in December 2019 in Wuhan, China. The World Health Organization declared a Public Health Emergency of International Concern regarding COVID-19 on 30 January 2020, and later declared a pandemic on 11 March 2020. As of 18 March 2023, more than 472 million cases have been confirmed, with more than 6.5 million deaths attributed to COVID-19, making it one of the deadliest pandemics in history.

Efforts to prevent the spread of COVID-19 include vaccination programs, lockdowns, travel restrictions, and the use of masks and other protective equipment. Vaccines have been developed and authorized for emergency use, with the Pfizer-BioNTech vaccine being the first to receive emergency use authorization in December 2020.

The pandemic has had significant social, economic, and political impacts. Many businesses have closed, and unemployment rates have risen in many countries. The pandemic has also highlighted disparities in access to healthcare and education, and has led to an increase in domestic violence and mental health issues."""

summary = openai_summarizer.summarize_text(text)
print(summary)

This generates the following summary:

The COVID-19 pandemic is caused by the SARS-CoV-2 virus and has resulted in over 6.5 million deaths worldwide. Efforts to prevent its spread include vaccination programs, lockdowns, travel restrictions, and the use of masks and protective equipment. The pandemic has had significant social, economic, and political impacts, including business closures and rising unemployment rates.

Here's another example of how to use OpenAI Summarize to summarize a news article:

from newspaper3k import Article
import openai_summarize

openai_summarizer = openai_summarize.OpenAISummarize("your_openai_key")

article = Article("https://www.nytimes.com/2023/03/18/world/europe/russia-nato-ukraine.html")
article.download()
article.parse()
summary = openai_summarizer.summarize_text(article.text)
print(summary)

API Reference

OpenAISummarize class

__init__(self, openai_key)

Creates an instance of the OpenAISummarize class.

Arguments
  • openai_key (str): Your OpenAI API token.

count_tokens(self, text)

Counts the number of tokens in a given text.

Arguments
  • text (str): The text to count the tokens of.
Returns
  • int: The number of tokens in the text.

chunk_text(self, text, max_tokens=500)

Breaks up a given text into chunks of at most max_tokens tokens.

Arguments
  • text (str): The text to chunk.
  • max_tokens (int): The maximum number of tokens allowed in each chunk. Defaults to 500.
Returns
  • list of str: The chunks of text.

summarize_text(self, text, max_chunk_size=500, max_combined_summary_size=4000)

Generates a summary of a given text using OpenAI's text-davinci-003 model.

Arguments
  • text (str): The text to summarize.
  • max_chunk_size (int, optional): The size of each chunk of text to summarize. Defaults to 500.
  • max_combined_summary_size (int, optional): The maximum size of the combined summary. Defaults to 4000.
Returns
  • str: The generated summary of the text.

extract_article_text(self, url)

Extracts the main text content of an article from a given URL.

Arguments
  • url (str): The URL of the article to extract the text from.
Returns
  • str: The main text content of the article.

Metadata

Release files for openai-summarize 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-summarize 0.1.4
File Size Uploaded Provenance Actions
openai_summarize-0.1.4.tar.gz 4.4 kB Without provenance Details for openai_summarize-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openai-summarize 0.1.4
File Interpreter ABI Platform Actions
openai_summarize-0.1.4-py3-none-any.whl Python 3 none any Details for openai_summarize-0.1.4-py3-none-any.whl

Total release size: 9.1 kB

Release files / openai_summarize-0.1.4.tar.gz

Download URL openai_summarize-0.1.4.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ad924b1dc81a041a8b4451ff2abe9ec81cd8fbb639f898067e7379571dd548df
BLAKE2b-256 checksum
How to use checksums
eab949b83a5b8aef8ab48fd4211e514281bfdfe19a3db8150c8f64396e3bc1c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / openai_summarize-0.1.4-py3-none-any.whl

Download URL openai_summarize-0.1.4-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06e392dc7e6707c2f899c51ca4cf35078d56ac9cd782726df3c3649cad4ae647
BLAKE2b-256 checksum
How to use checksums
61a0f50dd99289671234db3708fc9e467a3fc0fc72a9e7dff857e0555c41157e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Security for openai-summarize 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page