Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

A Step-by-Step Approach To Building a Text Summarization Webapp in Python From Scratch
Latest

A Step-by-Step Approach To Building a Text Summarization Webapp in Python From Scratch

Last Updated on January 6, 2023 by Editorial Team

Author(s): Dr. Dharini R

Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.

Using Hugging Face Inference API, Flask, HTML &Β CSS

Photo by Patrick Tomasso onΒ Unsplash

What is the aim? To build a web application that can get an input text and show itsΒ summary.

What are we going to use? Hugging Face Accelerated Inference API, Python Flask Framework, HTML,Β CSS.

How is it done? By carrying out the following steps.

The 3-StepΒ process

1. Identify the inference API of a text summarization model from the Hugging FaceΒ library.

2. Build the front end with HTML andΒ CSS.

3. Build the back end with Python Flask and include the summarization task.

Text summarization is the task of extracting a brief from a given set of sentences. A summary can be of two typesβ€Šβ€”β€Šan abstractive summary or an extractive summary. An extractive summary has words extracted from the given input, placing them together to form a brief. The abstractive summary generates the summary not only by replicating the words in the input, but also coining new words based on the understanding of theΒ text.

We are going to build an abstractive summarization application using the Hugging Face Accelerated Inference API. To utilize a model by providing input to it and to get the model’s output, we just have to make an APIΒ call.

The accelerated inference enables the β€˜plug-and-play’ kind of usage to the machine learning models by means of APIΒ calls.

Kindly refer to the following blog link to understand the working of Inference API and its benefits with an implementation demo.

Plug-and-Play ML Models with β€˜Accelerated Inference API’ from Hugging Face

The web application part of our project can easily be built with the Flask framework. The Flask helps with the development of a web application and renders an HTML file, which can be viewed in a webΒ browser.

Welcome to Flask – Flask Documentation (2.2.x)

The front end of our web application is built with HTML and CSS. The Hyper Text Markup Language (HTML) and Cascading Style Sheets (CSS) are used to design the structure of a webpage and presentation of a webpage, respectively.

Having looked at the gist of everything we are going to use, let's have an idea of what we will build. Our web app is going toΒ have

  • a front endβ€Šβ€”β€Ša web page that gets the user input text and shows the summary asΒ output.
  • a back endβ€Šβ€”β€Šwhere the user input is fed to the model, and the results are extracted from theΒ model.

Now kindly make sure to go through the 3 step process of building our summarization application.

1. Identify and utilize a text summarization model from HuggingΒ Face

  • For our project, we will be using the model facebook/bart-large-cnn provided by Lewis et al. [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension]
  • To use the model’s Inference API, select the model from the Hugging Face library and then clickInference APIunder the DeployΒ button.
  • On clicking that, we will see a python script that can be utilized for the inference, as shownΒ below.
Selecting the Inference API from HuggingΒ Face
  • An Access Token is needed to get the API_URL andΒ headers.
  • Create a profile in Hugging Face and create a new access token by following this path Profile -> Settings -> Access TokensΒ Tab.
  • The newly created access token can be used in place of Bearer andΒ headers
  • Now let's move to the next part of our project to create a web application.

2. Build the front end with HTML andΒ CSS.

  • The code for the front end is written in two parts
    β€Šβ€”β€ŠA static file
    β€Šβ€”β€ŠA HTMLΒ file

The static file consists of customizations to improvise the look of our front end. As the name suggests, the contents of a static file are not going to be changed according to the user’s input or actions. A static file can include anything such as images, videos, cascading style sheets(CSS), flash files, etc., and these are not dynamically generated by the web server like a typical HTML response because they remainΒ static.

  • In the desired location, create two folders named staticand templates
  • In the static folder, create a new file named main.css, where CSS is the cascading style sheet that is used to style theΒ HTML.
  • main.css consists of tags present in the HTML code with their corresponding styling and formatting. For a more detailed tutorial on how CSS is used, please refer to thisΒ link.
  • The code for the static file main.css is given below, followed by an explanation.
  • As we can see, the above CSS file consists of appearance-based formatting and customization for most of the tags used in our HTMLΒ code.

Code Explanation

  • Lines 2 to 9β€Šβ€”β€Šconsist of styling and formatting aimed at the tagΒ header.
  • Lines 11 to 25β€Šβ€”β€Šconsist of formatting for tagΒ h1.
  • Lines 28 to 34β€Šβ€”β€Šhave the styling for body tag, along with a background image named image.gif
  • Lines 35 to 39β€Šβ€”β€Šformatting for container
  • Lines 41 to 47β€Šβ€”β€Šstyling for div tag, which specifies the division of someΒ content
  • Lines 49 to 64β€Šβ€”β€Šformatting for tags h2 andΒ h3
  • Lines 68 to 77β€Šβ€”β€Šconsist of formatting aimed at placing two text boxes side by side with the help of tags parent and child. Let's move on to creating the front end with our HTMLΒ code.

The HTML is built with the objective of providing a structured pleasant user interface, a space for users to provide input and a space for showing theΒ summary.

  • In the already created folder named templates, create a new file named index.html.
  • The code for index.html is given below, followed by the explanation.

Code Explanation

  • Lines 4 to 11β€” Header of theΒ HTML
  • Line 9β€Šβ€”β€ŠWe have added the source of the static file (main.css).
  • Line 10β€Šβ€”β€ŠWe have given a title for our webpage named Summarization Application.
  • Lines 13 to 42β€Šβ€”β€ŠBody of theΒ HTML
  • Lines 14 to 19β€Šβ€”β€ŠContains the headertag, which is used to provide the introductory part of a web page. In our case, we have given β€œMy Summarization App”
  • Lines 23 and 34β€Šβ€”β€ŠTwo text boxes are created and placed side byΒ side
  • Lines 25 to 31β€Šβ€”β€ŠThe form created for getting the input data and sending it to the back end. We have utilizedform action to specify that the submitted data has to be sent toget_summary the method written in the back end for processing.
  • Line 27β€Šβ€”β€ŠThe text box for input is created and is named with the variable nameinput_text which is used in the back end for processing inputΒ data.
  • Lines 29 and 30β€Šβ€”β€ŠButtons Submit and Clear areΒ created.
  • Line 38β€Šβ€”β€ŠA text box for displaying the output is created with the name result which is used in the back end to store the summarized text.
  • Having built the front end for the web app, let us move on to build the backΒ end.

3. Build the back end with Python Flask and include the summarization task.

The job of the back end isΒ to

  • get the input from the frontΒ end,
  • utilize the Hugging Face’s Inference API by sending the input data to the summarization model and gathering its output(summary)
  • send the summary to the frontΒ end

Let’s name our backend python file asapp.py, kindly refer to the code given below, followed by the explanations.

  • We have to remember that the front end and the back end should be connected with common variableΒ names.
  • The inputs to the summarization model are
    β€Šβ€”β€ŠText to be summarized (obtained from the front end)
    β€Šβ€”β€ŠMinimum length of the summary (initialized)
    β€Šβ€”β€ŠMaximum length of the summary (initialized)

Code Explanation

  • To begin with, let’s install Flask using the command pip installΒ flask
  • Line 6β€Šβ€”β€Šincludes the path for the staticΒ file
  • Lines 7 to 9β€Šβ€”β€Šindex method to render the index.html file
  • Lines 12 to 38β€Šβ€”β€Šget_summary method
  • Lines 15 and 16β€Šβ€”β€Šincludes the URL for the Inference API and the authentication tokens, respectively. Make sure to paste your authentication token in the BearerΒ part.
  • Line 25β€Šβ€”β€Šspecify the input from the front end using the variable input_text
  • Lines 26 and 27β€Šβ€”β€Šspecify the maximum and minimum lengths for the summary with variables min_len andΒ max_len
  • Lines 30 to 33β€Šβ€”β€Šsend the query to the model, including the input variables. The result is stored in output variable.
  • Line 36β€Šβ€”β€Šthe summary from the model stored inoutput[β€œsummary_text”] is sent to the front end using the result variable.
  • As we can see, the common variable names used for connecting the front end and back end are input_text, get_summary, result.
  • The full code for the project is given in this GitHubΒ link.

Summarization App

  • On running the app.py python code, the front end (created using main.css and index.html files) is rendered in the local host http://127.0.0.1:5000.
  • Opening the local host link in a browser will give us the web application.
  • A screenshot of the output is givenΒ below.
  • We can see Summarization Application as our page title, MY SUMMARIZATION APP as the header content, two text boxes side by side with titles Input Text and Summarized Text, two buttons Submit andΒ Clear.
  • If an input text is given in the left side box and Submit button is pressed, the get_summary method is invoked and the summarization model is utilized.
  • In the following screenshot, we can see that the input text is pasted and Submit button isΒ clicked.
  • As a result the get_summary method is invoked and the change in the address bar can be noted in the following screenshot (http://127.0.0.1:5000/get_summary).
  • When the get_summary method is invoked, the inference API of the Summarization model is utilized, and the output is shown in the Summarized TexttextΒ box.

Hurray!!! We have successfully created a Web application for the most popular Summarization task in Natural Language Processing.

Summary

We had an introduction on summarization, how we are going to build the web app for extracting the summary of a text, and a gist of all the elements to be used. We proceeded to understand the simple 3 step process of developing the web app, starting with selecting a model from Hugging Face for the task. Next, we understood the usage of HTML and CSS with a code example for building the front end. Followed by that, we inferred the usage of Flask with a code example for building the back end. Finally, we saw the result of our hard work, the rewarding Summarization Web Application. The same procedure can be extended to build any other kind of Natural Language Processing task using a model from Hugging Face. Proceed with your task and succeed!!!!


A Step-by-Step Approach To Building a Text Summarization Webapp in Python From Scratch was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Join thousands of data leaders on the AI newsletter. It’s free, we don’t spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓