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 transformers. Gradio and Hugging Face 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 statement to do that: import # 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 function from Hugging Face transformers to do that. We need to specify the task as and optionally, we can provide other parameters, such as the , , and of the summaries: pipeline "summarization" max_length min_length do_sample # 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 pipeline that we created in the previous step to do that. We need to pass the text to the and get the first element of the returned list, which is a dictionary that contains the key. We can return the value of that key as the output of our function: summarizer summarizer "summary_text" # 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 function to do that. We need to pass our function as the argument and specify the input and output components as the Textbox arguments. We can use the component for the input text and the component for the output summary. gr.Interface fn gr.Textbox gr.Textbox We can also provide labels for the components as the argument: label # 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 method of the interface object to do that. launch # 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 and documentation. I hope you enjoyed this article and learned something new. Happy summarizing! Gradio Hugging Face Also published . here