Open In App

Text Classification using HuggingFace Model

Last Updated : 20 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Text classification is a pivotal task in natural language processing (NLP) that categorizes text into predefined categories. It is widely used in sentiment analysis, spam detection, topic labeling, and more. The development of transformer-based models, such as those provided by Hugging Face, has significantly enhanced the accuracy and efficiency of these tasks.

This article explores how to implement text classification using a Hugging Face transformer model, specifically leveraging a user-friendly Gradio interface to interact with the model.

Overview of Hugging Face Transformers

Hugging Face is at the forefront of modern NLP, providing a vast array of pre-trained models that are easily accessible through their transformers library. These models are trained on diverse datasets and are highly capable of understanding and generating human-like text. For text classification, models like BERT, DistilBERT, and RoBERTa are commonly used due to their robustness and versatility in handling various NLP tasks.

Choosing a Model

For our demonstration, we selected distilbert-base-uncased-finetuned-sst-2-english, a distilled version of the BERT model fine-tuned on the Stanford Sentiment Treebank (SST-2) dataset for sentiment analysis. This model offers a good balance between performance and computational efficiency, making it suitable for real-time applications.

Implementing Text Classification Model with Gradio

The goal is to create a web-based interface using Gradio that allows users to input text and receive sentiment classification results. Gradio is an open-source library that makes it easy to create customizable UI components for machine learning models.

Step 1: Load the Model

We use the pipeline API from Hugging Face's transformers library, which provides a high-level abstraction to apply pre-trained models directly to data.

from transformers import pipeline

def load_model():
return pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

Step 2: Define Text Classification Function

This function receives text from the user and uses the loaded model to perform sentiment analysis.

def classify_text(model, text):
return model(text)

Step 3: Set Up Gradio Interface

We create a simple interface with a textbox for input and configure it to display the model's output in JSON format.

import gradio as gr

def main():
model = load_model()
interface = gr.Interface(
fn=lambda text: classify_text(model, text),
inputs=gr.Textbox(lines=2, placeholder="Enter Text Here..."),
outputs="json",
title="Text Classification with HuggingFace",
description="This interface uses a HuggingFace model to classify text sentiments. Enter a sentence to see its classification."
)
interface.launch()

Complete Code and Output for Text Classification using Hugging Face Model

Python
import gradio as gr
from transformers import pipeline

def load_model():
    # Load a pre-trained HuggingFace pipeline for sentiment analysis
    model_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
    return model_pipeline

def classify_text(model, text):
    # Use the loaded model to classify text
    result = model(text)
    return result

def main():
    # Load the model
    model = load_model()

    # Define the Gradio interface
    interface = gr.Interface(
        fn=lambda text: classify_text(model, text),
        inputs=gr.Textbox(lines=2, placeholder="Enter Text Here..."),
        outputs="json",
        title="Text Classification with HuggingFace",
        description="This interface uses a HuggingFace model to classify text sentiments. Enter a sentence to see its classification."
    )

    # Launch the Gradio app
    interface.launch()

if __name__ == "__main__":
    main()

Output:

Capture


Conclusion

Integrating Hugging Face transformers with Gradio offers a powerful and efficient way to deploy NLP models with interactive web interfaces. This setup not only aids in rapid prototyping but also enhances accessibility, allowing end-users with no technical background to leverage state-of-the-art NLP technologies. By following this guide, developers can extend the application to various other NLP tasks, customizing the interface and model choice as per their specific needs.


Similar Reads