Build Text Summarizer with Gradio
Tags: datascience, ai, machinelearning, tutorial
Published: 2024-01-17 12:41:51

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](https://datapro.blog/machine-learning/build-text-summarizer-with-gradio/) 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](https://datapro.blog/) with Gradio and Hugging Face transformers.

## Table of Contents

- [Gradio](https://datapro.blog/)
- [Hugging Face](https://datapro.blog/machine-learning/random-search-in-machine-learning/)
- [Code for Text Summarizer](https://datapro.blog/data-science/what-is-bias-vs-variance-tradeoff/)
  - [Import the libraries](https://datapro.blog/data-science/data-science-vs-machine-learning/)
  - [Create a Summarization Pipeline](https://datapro.blog/deep-learning/what-is-deep-learning/)
  - [Define a function that takes a text and returns a summary](https://datapro.blog/machine-learning/logistic-regression/)
  - [Create a Gradio Interface](https://datapro.blog/machine-learning/linear-regression/)
  - [Launch the Interface](https://datapro.blog/prompt-engineering/)
- [Conclusion](https://datapro.blog/chat-gpt/what-is-chatgpt/)

## Gradio
[Gradio](https://datapro.blog/chat-gpt/chat-gpt-login/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcs8ybsdhmtg7zz0yhoz.png)


## Hugging Face

Hugging Face transformers is another [Python library](https://datapro.blog/deep-learning/deep-learning-vs-machine-learning/) 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](https://datapro.blog/pytorch/pytorch-installation/), [TensorFlow](https://datapro.blog/tensorflow/tensorflow-tutorial/), or [JAX](https://datapro.blog/tensorflow/tensorflow-vs-pytorch/).

![Hugging Face](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwq6kqh90q6kqo2ra2z6.png)

## 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 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)
```
### 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 out yourself by running the code or visiting the link provided by [Gradio](https://datapro.blog/ai/ilya-sutskever/). 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](https://datapro.blog/ai/rise-of-mira-murati/). I hope you enjoyed this article and learned something new. Happy summarizing!