paint-brush
How to Build a Text Summarizer with Gradio and Hugging Face Transformers by@rajeshai1729
387 reads
387 reads

How to Build a Text Summarizer with Gradio and Hugging Face Transformers

by Rajesh KumarJanuary 21st, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Text summarization is the task of creating a shorter version of a document or an article. It is useful for quickly understanding the content of long texts, such as research papers, or blog posts. In this article, we will show you how to build a text summarizer with Gradio and Hugging Face transformers.

People Mentioned

Mention Thumbnail
featured image - How to Build a Text Summarizer with Gradio and Hugging Face Transformers
Rajesh Kumar HackerNoon profile picture


Text summarization is the task of creating a shorter version of a document or an article that captures the main information and meaning. It is useful for quickly understanding the content of long texts, such as news articles, research papers, or blog posts. In this article, we will show you how to build a text summarizer with Gradio and Hugging Face transformers.

Table of contents

  • Gradio
  • Hugging Face
  • Code for Text Summarizer
    • Import the libraries
    • Create a Summarization Pipeline
    • Define a function that takes a text and returns a summary
    • Create a Gradio Interface
    • Launch the Interface
  • Conclusion

Gradio

Gradio is a Python library that allows you to quickly build a web interface for your machine-learning models. You can use Gradio to create interactive demos, tutorials, or experiments for your models, and share them with anyone online. Gradio supports various input and output components, such as text boxes, images, audio, sliders, buttons, and more.

Hugging Face

Hugging Face transformers is another Python library that provides you with many pre-trained models for various natural language processing tasks, including text summarization. You can use the transformers library to easily load and use these models or fine-tune them on your data. The transformers library also supports different frameworks, such as PyTorch, TensorFlow, or JAX.


Also read: Linear Regression: A Comprehensive Guide

Code for Text Summarizer

To build a text summarizer with Gradio and Hugging Face transformers, we need to follow these steps:

  • Import the libraries
  • Create a summarization pipeline
  • Define a function that takes a text and returns a summary
  • Create a Gradio interface
  • Launch the interface

Let’s see the code for each step:

Import the libraries

We need to import Gradio and Hugging Face transformers in our code. We can use the import statement to do that:

# Import libraries
import gradio as gr
from transformers import pipeline

Create a Summarization Pipeline

We need to create a summarization pipeline using a pre-trained model to generate summaries. We can use the pipeline function from Hugging Face transformers to do that. We need to specify the task as "summarization" and optionally, we can provide other parameters, such as the max_length, min_length, and do_sample of the summaries:

# Create a summarization pipeline
summarizer = pipeline("summarization", max_length=150, min_length=40, do_sample=False)


Define a function that takes a text and returns a summary

We need to define a function that will take a text as input and return a summary as output. We can use the summarizer pipeline that we created in the previous step to do that. We need to pass the text to the summarizer and get the first element of the returned list, which is a dictionary that contains the "summary_text" key. We can return the value of that key as the output of our function:

# Define a function that takes a text and returns a summary
def summarize(text):
  summary = summarizer(text)[0]
  return summary["summary_text"]


Create a Gradio Interface

We need to create a Gradio interface that will wrap up our function and provide a web interface for it. We can use the gr.Interface function to do that. We need to pass our function as the fn argument and specify the input and output components as the Textbox arguments. We can use the gr.Textbox component for the input text and the gr.Textbox component for the output summary.


We can also provide labels for the components as the label argument:

# Create a Gradio interface
interface = gr.Interface(
  fn=summarize, # the function to wrap
  inputs=gr.inputs.Textbox(lines=10, label="Input Text"), # the input component
  outputs=gr.outputs.Textbox(label="Summary") # the output component
)


Launch the Interface

We need to launch the interface so that we can use it online. We can use the launch method of the interface object to do that.

# Launch the interface
interface.launch()


Here is the entire code in one block:

# Import libraries
import gradio as gr
from transformers import pipeline

# Create a summarization pipeline
summarizer = pipeline("summarization")

# Define a function that takes a text and returns a summary
def summarize(text):
  summary = summarizer(text, max_length=150, min_length=40, do_sample=False)[0]
  return summary["summary_text"]

# Create a Gradio interface
interface = gr.Interface(
  fn=summarize, # the function to wrap
  inputs=gr.Textbox(lines=10, label="Input Text"), # the input component
  outputs=gr.Textbox(label="Summary") # the output component
)

# Launch the interface
interface.launch()


Conclusion

This is how you can build a text summarizer with Gradio and Hugging Face transformers. You can try it by running the code or visiting the link provided by Gradio. You can also experiment with different models and parameters to improve the quality of the summaries. You can also customize the interface with different components and styles. For more information, you can check out the Gradio and Hugging Face documentation. I hope you enjoyed this article and learned something new. Happy summarizing!


Also published here.