Summarize your Article with Python in 3 Minutes

Sumanth PSumanth P
3 min read

In this Tutorial we'll learn how to perform Text Summarization in 3 min using Python and the One AI Summarization API. For this app we will need:

  1. Python

  2. Streamlit

  3. Summarization API

Glimpse of the Web App

Why We Need This?

We have to read entire articles or conversations which are so long and boring to understand.

But why do we have to read when we have tools that can extract short summaries of them and makes easy to understand and save time?

We gonna explore one such tool in this blog post and create our own application.

Importing Necessary Libraries

import streamlit as st
import requests

As the first step Import Streamlit Library and Requests Module

If Streamlit Library is not installed try to install it using pip command

pip install streamlit

Streamlit Application

Now, let's set up the structure of the Streamlit application. This will be the user-facing interface of the application.

st.title('Text Summarization')
text = st.text_area('Text to Summarize')
st.button('Summarize')
  • First the Title of the Application
  • Second Text Area where a user can enter the text they want to Summarize
  • Third a Button to Summarize

Get the API Key

The API key is the unique key that you can use to access the API.

If you don't have a ONE AI Account try to create one and get the API Key using the below link

Free API KEY

Hitting the API

Now once the Initial Application is ready it's time to send the Article through API for Summarization

authorization = api_key
end_point = "https://api.oneai.com/api/v0/pipeline"

headers = {
  "api-key": authorization,
  "content-type": "application/json"
}
payload = {
  "input": text,
  "input_type": "article",
  "steps": [
      {
        "skill": "summarize"
      }
    ]
}

Understanding the above code line by line, authorization is the variable to store the API Key and through the end_point we are accessing the ONE AI pipeline. Headers and payload are specifying all the details which are required to call the API

Getting the Results from the API

results = requests.post(end_point, json=payload, headers=headers)
summarized_article = results.json()['output'][0]['text']

Next, we will make a POST request to the summarization endpoint, by attaching a JSON variable and headers

Now the result will get stored in results variable which is a JSON file and from that, we can only get the summarized output

Displaying the Results

st.write(summarized_article)

Once we get the Summarized Output of the Article, we can display that on the Streamlit Application using the write() function from steamlit.

TLDR

Create a file app.py and put everything together:

import streamlit as st
import requests

st.title('Text Summarization')
text = st.text_area('Text to Summarize')
st.button('Summarize')

authorization = api_key
end_point = "https://api.oneai.com/api/v0/pipeline"

headers = {
  "api-key": authorization,
  "content-type": "application/json"
}
payload = {
  "input": text,
  "input_type": "article",
  "steps": [
      {
        "skill": "summarize"
      }
    ]
}

results = requests.post(url, json=payload, headers=headers)
summarized_article = results.json()['output'][0]['text']

st.write(summarized_article)

Now we can run the app with the following command:

streamlit run app.py

This will start the Server and we can interact with our app by sending the Articles and Summarizing them.

You can even Summarize the articles and play with them using the ONE AI PLAYGROUND. Here is the short demo on how to do that.

And here is the link: PLAYGROUND

End

Yay! Finally, we build a simple Streamlit Application that can Summarize the Articles or Conversations within a few minutes. If you'd prefer to watch the tutorial you can check the Video here

I hope you enjoyed this article! The whole code is also available on Github

17
Subscribe to my newsletter

Read articles from Sumanth P directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sumanth P
Sumanth P

Machine Learning Engineer, enjoys creating content around Python, ML, and Data Science.