Creating Meaningful Article Summaries Using Python
With just a few lines of code it is possible to summarize articles using Python.
In this tutorial, we will build an automatic article summarizer using Python and One AI. One AI allows you to define custom language processing behaviors, including summarization, via Pipeline objects. Each Pipeline object comprises multiple language models that process texts and extract data points from them.
By the way, this TL;DR was generated with the code shown below (don’t believe it? see here 😉)
In this tutorial, we will build an automatic article summarizer using Python. This can be used in your projects wherever you have textual data you want to summarize automatically.
We’ll be using One AI, an API that provides various AI-powered text processing capabilities, including article summarization.
We first need to install One AI’s SDK in our Python environment via
```pip install oneai```
The SDK supports Python 3.6.1+, so you should be good to go.
Now let’s create an API key- register and get a key in the Language Studio
Creating an API key
Create a file `keys.py` with the generated API key and add it to `.gitignore`
```oneai_api_key = "4EDjaB8zxBy55GoNk5eVGOjbdzhl6wSY6vTnVfAkBCWg0JZZ9E"```
Now we can use the key in our code. In a different file, add
oneai.api_key = keys.oneai_api_key```
The actual code
One AI allows you to define custom language processing behaviors via `Pipeline` objects, which are defined with a list of `Skill`s. Each `Skill` wraps multiple language models which process texts and extract data points from them.
Here’s how to define a `Pipeline` for text summarization
```pipeline = oneai.Pipeline(steps=[
You can control summary length (in words) with the `min_length`, `max_length` parameters in the constructor of `Summarize` , i.e
And that’s basically it- pass your `str` input to `pipeline` with
```output = pipeline.run(your_input)```
The summary will be stored in `output.summary.text`. Here’s the entire code
# set a One AI API key, following API calls will use this key
oneai.api_key = keys.oneai_api_key
# insert your input text here
your_input = "summarize this text"
# define a pipeline for processing str inputs
pipeline = oneai.Pipeline(steps=[
# summarize inputs, with at most 20 words
# process the input and store the output
output = pipeline.run(your_input)
# print the summary
You can achieve a lot more with Language AI `Pipeline`s & `Skill`s, for example, to extract topics from texts, detect sentiment, and more. `Pipeline`s allow you to perform multiple language processing tasks in a single call, for example:
```output = oneai.Pipeline(steps=[
# detect positive or negative sentiments
# create a summary
# extract topics from the summary
In this example, `output.sentiments` will contain a list of sentiments detected in the input, `output.summary.text` will contain the summary, and `output.summary.topics` will contain a list of topics extracted from the summary.