DEV Community

Cover image for Build Text Summarizer with Gradio
Rajesh Kumar
Rajesh Kumar

Posted on • Originally published at datapro.blog

Build Text Summarizer with Gradio

Text summarization is the task of creating a shorter version of a document or an article that captures the main information and meaning. Text Summarizer 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

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.

Gradio

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.

Hugging Face

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
Enter fullscreen mode Exit fullscreen mode

Create a Summarization Pipeline

We need to create a summarization pipeline that will use 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)
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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
)
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is how you can build a text summarizer with Gradio and Hugging Face transformers. You can try it out yourself 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!

Top comments (0)