Zero-Shot Text Classification using HuggingFace Model
Last Updated :
06 Jun, 2024
Zero-shot text classification is a groundbreaking technique that allows for categorizing text into predefined labels without any prior training on those specific labels. This method is particularly useful when labeled data is scarce or unavailable. Leveraging the HuggingFace Transformers library, we can easily implement zero-shot classification using pre-trained models. In this article, we'll explore how to use the HuggingFace pipeline
for zero-shot classification and create an interactive web interface using Gradio.
Understanding Zero-Shot Classification
Zero-shot classification relies on pre-trained language models that understand language context deeply. These models can be prompted with new tasks, such as classification, by providing text and candidate labels. The model evaluates the text against the labels and assigns probabilities to each label based on its understanding.
HuggingFace Transformers
The HuggingFace Transformers library provides an easy-to-use interface for various natural language processing tasks, including zero-shot classification. One of the most popular models for this task is facebook/bart-large-mnli
, which is based on the BART model and fine-tuned on the Multi-Genre Natural Language Inference (MNLI) dataset.
Implementing Zero-Shot Classification
Step 1: Install HuggingFace Transformers
First, ensure that you have the HuggingFace Transformers library installed:
pip install transformers
Step 2: Initialize the Zero-Shot Classification Pipeline
Next, we initialize the zero-shot classification pipeline using the facebook/bart-large-mnli
model:
from transformers import pipeline
# Initialize the zero-shot classification pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
Step 3: Perform Classification
We can now classify a sample text into predefined labels. Here’s an example:
text = "The company's quarterly earnings increased by 20%, exceeding market expectations."
candidate_labels = ["finance", "sports", "politics", "technology"]
result = classifier(text, candidate_labels)
print(result)
Code of Zero-Shot Classification
Python
from transformers import pipeline
# Initialize the zero-shot classification pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
text = "The company's quarterly earnings increased by 20%, exceeding market expectations."
candidate_labels = ["finance", "sports", "politics", "technology"]
result = classifier(text, candidate_labels)
print(result)
Output:
{'sequence': "The company's quarterly earnings increased by 20%, exceeding market expectations.", 'labels': ['finance', 'technology', 'sports', 'politics'], 'scores': [0.6282334327697754, 0.22457945346832275, 0.08779555559158325, 0.05939162150025368]}
Evaluating Zero-Shot Classification
To evaluate the performance, you can compare the predicted labels with true labels using metrics like precision, recall, and F1-score. Here’s an example using a small dataset:
Python
from sklearn.metrics import classification_report
texts = ["The stock market is up today.", "The new movie is a great thriller.", "The football match was exciting."]
true_labels = ["finance", "entertainment", "sports"]
predicted_labels = []
for text in texts:
result = classifier(text, candidate_labels=["finance", "entertainment", "sports"])
predicted_labels.append(result['labels'][0])
print(classification_report(true_labels, predicted_labels))
Output:
precision recall f1-score support
entertainment 0.50 1.00 0.67 1
finance 1.00 1.00 1.00 1
sports 0.00 0.00 0.00 1
accuracy 0.67 3
macro avg 0.50 0.67 0.56 3
weighted avg 0.50 0.67 0.56 3
Creating an Interactive Interface with Gradio
Gradio provides an easy way to create web interfaces for machine learning models. We can use Gradio to build an interactive interface for zero-shot classification.
Step 1: Install Gradio
First, install Gradio:
pip install gradio
Step 2: Define the Classification Function
Create a function that takes text and labels as inputs and returns the classification results:
import gradio as gr
from transformers import pipeline
# Initialize the zero-shot classification pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
# Define the classification function
def classify_text(text, labels):
labels = labels.split(",")
result = classifier(text, candidate_labels=labels)
return {label: score for label, score in zip(result["labels"], result["scores"])}
Step 3: Create the Gradio Interface
Set up the Gradio interface with text inputs for the sentence and labels, and a label output:
# Create the Gradio interface
interface = gr.Interface(
fn=classify_text,
inputs=[
gr.inputs.Textbox(lines=2, placeholder="Enter text here..."),
gr.inputs.Textbox(lines=1, placeholder="Enter comma-separated labels here...")
],
outputs=gr.outputs.Label(num_top_classes=3),
title="Zero-Shot Text Classification",
description="Classify text into labels without training data.",
)
# Launch the interface
interface.launch()
Complete Code for Creating an Interactive Interface with Gradio
Python
import gradio as gr
from transformers import pipeline
# Initialize the zero-shot classification pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
# Define the classification function
def classify_text(text, labels):
labels = labels.split(",")
result = classifier(text, candidate_labels=labels)
return {label: score for label, score in zip(result["labels"], result["scores"])}
# Create the Gradio interface
interface = gr.Interface(
fn=classify_text,
inputs=[
gr.Textbox(lines=2, placeholder="Enter text here..."),
gr.Textbox(lines=1, placeholder="Enter comma-separated labels here...")
],
outputs=gr.Label(num_top_classes=3),
title="Zero-Shot Text Classification",
description="Classify text into labels without training data.",
)
# Launch the interface
interface.launch()
Output:
Gradio Interface for Interactive Zero Shot Classification Conclusion
Zero-shot text classification using the HuggingFace Transformers library offers a flexible and powerful way to categorize text without the need for labeled training data. By leveraging models like facebook/bart-large-mnli, we can achieve high accuracy in various classification tasks. Additionally, integrating this functionality with Gradio allows for easy deployment of interactive web interfaces, making it accessible to a wider audience. This approach opens up numerous possibilities for real-world applications where labeled data is not readily available.
Similar Reads
Text Classification using HuggingFace Model
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 sig
3 min read
Text Feature Extraction using HuggingFace Model
Text feature extraction converts text data into a numerical format that machine learning algorithms can understand. This preprocessing step is important for efficient, accurate, and interpretable models in natural language processing (NLP). We will discuss more about text feature extraction in this
4 min read
Text-to-Image using Stable Diffusion HuggingFace Model
Models available through HuggingFace utilize advanced machine-learning techniques for a variety of applications, from natural language processing to computer vision. Recently, they have expanded to include the ability to generate images directly from text descriptions, prominently featuring models l
3 min read
Text-to-Video Synthesis using HuggingFace Model
The emergence of deep learning has brought forward numerous innovations, particularly in natural language processing and computer vision. Recently, the synthesis of video content from textual descriptions has emerged as an exciting frontier. Hugging Face, a leader in artificial intelligence (AI) res
6 min read
Text Classification using Logistic Regression
Text classification is a fundamental task in Natural Language Processing (NLP) that involves assigning predefined categories or labels to textual data. It has a wide range of applications, including spam detection, sentiment analysis, topic categorization, and language identification. Logistic Regre
4 min read
Text classification using CNN
Text classification is a widely used NLP task in different business problems, and using Convolution Neural Networks (CNNs) has become the most popular choice. In this article, you will learn about the basics of Convolutional neural networks and the implementation of text classification using CNNs, a
5 min read
Sentiment Analysis using HuggingFace's RoBERTa Model
Sentiment analysis determines the sentiment or emotion behind a piece of text. It's widely used to analyze customer reviews, social media posts, and other forms of textual data to understand public opinion and trends. In this article, we are going to implement sentiment analysis using RoBERTa model.
4 min read
Text Summarizations using HuggingFace Model
Text summarization is a crucial task in natural language processing (NLP) that involves generating concise and coherent summaries from longer text documents. This task has numerous applications, such as creating summaries for news articles, research papers, and long-form content, making it easier fo
5 min read
Image classification using Support Vector Machine (SVM) in Python
Support Vector Machines (SVMs) are a type of supervised machine learning algorithm that can be used for classification and regression tasks. In this article, we will focus on using SVMs for image classification. When a computer processes an image, it perceives it as a two-dimensional array of pixels
9 min read
Image Classification using Google's Teachable Machine
Machine learning is a scientific field that allows computers to learn without being programmed directly. When many learners, students, engineers, and data scientists use machine learning to create diverse projects and goods, the application of machine learning is trendy. However, the development of
2 min read