fbpx

Text summarization web service with NLP machine learning – SEO Explorer’s Blog

Text summarization web service with NLP machine learning

I wanted to create a web service that takes the text and summarizes it using machine learning.

There are several API services on the market, but I wanted to see how easy (or hard) it is to create my own.

Step 1 – Use an existing NLP package

There’s no need to reinvent the wheel, and I took this NLP package: https://pypi.org/project/bert-extractive-summarizer/

I followed the instructions and got this error:

Step 2 – Install missing dependencies

The solution was easy, and I had to install the two packages:

pip3 install TensorFlow

pip3 install torch

If you plan to use a GPU, you might want to install TensorFlow-GPU instead.

Step 3 – Checking that the example works

I took their example, and tested it, before moving forward, we need to see it’s working. I used the “large example”, and it worked.

Now we are ready to create the server.

Step 4 – Python HTTP server

I used this example for Python’s web server: https://pythonbasics.org/webserver/

Remember that Python’s web server is not meant to be secure, so make sure you wrap it with an HTTP secure  server, or use it locally.

I tested their script, and it worked.

Step 5 – Combining it all

I did a merge of both source codes and added some magic. This server is not using any of the summarizer extra features, but it should be easy to customize it.

Here’s the code: (you can download it from: https://seo-explorer.io/code/open-source/machine-learning/python-nlp-summarizer-web-service)

Usage:

Run it with:

It will listen on port 80 on all interfaces, send it a base64 encoded text:

The server will send back the summarized reply. The base64 is to avoid any errors regarding text and text encoding.

No wonder the APIs cost is low, it’s straightforward to create your own service, one thing to consider is using a GPU for high volumes.

Translate »