<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rajesh Kumar</title>
    <description>The latest articles on DEV Community by Rajesh Kumar (@rajesh1729).</description>
    <link>https://dev.to/rajesh1729</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1258944%2F25a64585-7be9-49a6-a93d-2efb030c108b.png</url>
      <title>DEV Community: Rajesh Kumar</title>
      <link>https://dev.to/rajesh1729</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajesh1729"/>
    <language>en</language>
    <item>
      <title>Build Text Summarizer with Gradio</title>
      <dc:creator>Rajesh Kumar</dc:creator>
      <pubDate>Wed, 17 Jan 2024 12:41:51 +0000</pubDate>
      <link>https://dev.to/rajesh1729/build-text-summarizer-with-gradio-2me2</link>
      <guid>https://dev.to/rajesh1729/build-text-summarizer-with-gradio-2me2</guid>
      <description>&lt;p&gt;Text summarization is the task of creating a shorter version of a document or an article that captures the main information and meaning. &lt;a href="https://datapro.blog/machine-learning/build-text-summarizer-with-gradio/" rel="noopener noreferrer"&gt;Text Summarizer&lt;/a&gt; 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 &lt;a href="https://datapro.blog/" rel="noopener noreferrer"&gt;text summarizer&lt;/a&gt; with Gradio and Hugging Face transformers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://datapro.blog/" rel="noopener noreferrer"&gt;Gradio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datapro.blog/machine-learning/random-search-in-machine-learning/" rel="noopener noreferrer"&gt;Hugging Face&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://datapro.blog/data-science/what-is-bias-vs-variance-tradeoff/" rel="noopener noreferrer"&gt;Code for Text Summarizer&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://datapro.blog/data-science/data-science-vs-machine-learning/" rel="noopener noreferrer"&gt;Import the libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datapro.blog/deep-learning/what-is-deep-learning/" rel="noopener noreferrer"&gt;Create a Summarization Pipeline&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datapro.blog/machine-learning/logistic-regression/" rel="noopener noreferrer"&gt;Define a function that takes a text and returns a summary&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datapro.blog/machine-learning/linear-regression/" rel="noopener noreferrer"&gt;Create a Gradio Interface&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datapro.blog/prompt-engineering/" rel="noopener noreferrer"&gt;Launch the Interface&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;a href="https://datapro.blog/chat-gpt/what-is-chatgpt/" rel="noopener noreferrer"&gt;Conclusion&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Gradio
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://datapro.blog/chat-gpt/chat-gpt-login/" rel="noopener noreferrer"&gt;Gradio&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffcs8ybsdhmtg7zz0yhoz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffcs8ybsdhmtg7zz0yhoz.png" alt="Gradio" width="720" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hugging Face
&lt;/h2&gt;

&lt;p&gt;Hugging Face transformers is another &lt;a href="https://datapro.blog/deep-learning/deep-learning-vs-machine-learning/" rel="noopener noreferrer"&gt;Python library&lt;/a&gt; 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 &lt;a href="https://datapro.blog/pytorch/pytorch-installation/" rel="noopener noreferrer"&gt;PyTorch&lt;/a&gt;, &lt;a href="https://datapro.blog/tensorflow/tensorflow-tutorial/" rel="noopener noreferrer"&gt;TensorFlow&lt;/a&gt;, or &lt;a href="https://datapro.blog/tensorflow/tensorflow-vs-pytorch/" rel="noopener noreferrer"&gt;JAX&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpwq6kqh90q6kqo2ra2z6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpwq6kqh90q6kqo2ra2z6.png" alt="Hugging Face" width="496" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code for Text Summarizer
&lt;/h2&gt;

&lt;p&gt;To build a text summarizer with Gradio and Hugging Face transformers, we need to follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Import the libraries&lt;/li&gt;
&lt;li&gt;Create a summarization pipeline&lt;/li&gt;
&lt;li&gt;Define a function that takes a text and returns a summary&lt;/li&gt;
&lt;li&gt;Create a Gradio interface&lt;/li&gt;
&lt;li&gt;Launch the interface
Let’s see the code for each step:&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Import the libraries
&lt;/h3&gt;

&lt;p&gt;We need to import Gradio and Hugging Face transformers in our code. We can use the import statement to do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Import libraries
import gradio as gr
from transformers import pipeline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Summarization Pipeline
&lt;/h3&gt;

&lt;p&gt;We need to create a summarization pipeline that will use a pre-trained model to generate summaries. We can use the &lt;code&gt;pipeline&lt;/code&gt; 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 &lt;code&gt;max_length&lt;/code&gt;, &lt;code&gt;min_length&lt;/code&gt;, and &lt;code&gt;do_sample&lt;/code&gt; of the summaries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a summarization pipeline
summarizer = pipeline("summarization", max_length=150, min_length=40, do_sample=False)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Define a function that takes a text and returns a summary
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Define a function that takes a text and returns a summary
def summarize(text):
summary = summarizer(text)[0]
return summary["summary_text"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Gradio Interface
&lt;/h3&gt;

&lt;p&gt;We need to create a Gradio interface that will wrap up our function and provide a web interface for it. We can use the &lt;code&gt;gr.Interface&lt;/code&gt; function to do that. We need to pass our function as the &lt;code&gt;fn&lt;/code&gt; argument and specify the input and output components as the Textbox arguments. We can use the &lt;code&gt;gr.Textbox&lt;/code&gt; component for the input text and the &lt;code&gt;gr.Textbox&lt;/code&gt; component for the output summary. We can also provide labels for the components as the &lt;code&gt;label&lt;/code&gt; argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Launch the Interface
&lt;/h3&gt;

&lt;p&gt;We need to launch the interface so that we can use it online. We can use the &lt;code&gt;launch&lt;/code&gt; method of the interface object to do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Launch the interface
interface.launch()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the entire code in one block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://datapro.blog/ai/ilya-sutskever/" rel="noopener noreferrer"&gt;Gradio&lt;/a&gt;. 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 &lt;a href="https://datapro.blog/ai/rise-of-mira-murati/" rel="noopener noreferrer"&gt;Hugging Face documentation&lt;/a&gt;. I hope you enjoyed this article and learned something new. Happy summarizing!&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>ai</category>
      <category>machinelearning</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
