Photo by

Creating Meaningful Article Summaries Using Python

With just a few lines of code it is possible to summarize articles using Python.

Michael Gur
Michael Gur
·
Aug 24, 2022
·
3 min read
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.

Setup

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

```import oneai
import keys
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=[
    oneai.skills.Summarize(),

])```

You can control summary length (in words) with the `min_length`, `max_length` parameters in the constructor of `Summarize` , i.e

```oneai.skills.Summarize(max_length=20)```

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

```import oneai

import keys

# 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

    oneai.skills.Summarize(max_length=20),

])

# process the input and store the output

output = pipeline.run(your_input)

# print the summary

print(output.summary.text)```

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

    oneai.skills.Sentiments(),

    # create a summary

    oneai.skills.Summarize(),

    # extract topics from the summary

    oneai.skills.Topics(),

]).run(your_input)```

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.

Check out the Language Studio, a visual interface for Language Skills, and let us know what you think at devrel@oneai.com.

Find out why we chose One AI over OpenAI