How to use text classification pipeline Hugging Face
Quick answer
Use the Hugging Face
transformers library's pipeline function with the task set to text-classification to classify text. Load a pretrained model like distilbert-base-uncased-finetuned-sst-2-english and pass your text to get classification labels and scores.PREREQUISITES
Python 3.8+pip install transformers>=4.0.0pip install torch (or tensorflow)Internet connection to download pretrained models
Setup
Install the transformers library and a backend like torch or tensorflow. Set up your Python environment with these commands:
pip install transformers torch Step by step
Use the pipeline API for text classification. This example uses the distilbert-base-uncased-finetuned-sst-2-english model for sentiment analysis:
from transformers import pipeline
# Initialize the text classification pipeline
classifier = pipeline('text-classification', model='distilbert-base-uncased-finetuned-sst-2-english')
# Input text to classify
texts = [
"I love using Hugging Face transformers!",
"This is the worst movie I have ever seen."
]
# Get classification results
results = classifier(texts)
for text, result in zip(texts, results):
print(f"Text: {text}\nLabel: {result['label']}, Score: {result['score']:.4f}\n") output
Text: I love using Hugging Face transformers! Label: POSITIVE, Score: 0.9998 Text: This is the worst movie I have ever seen. Label: NEGATIVE, Score: 0.9997
Common variations
- Use other pretrained models by changing the
modelparameter, e.g.,roberta-base-finetuned-sentiment. - Run asynchronously with
asyncioby wrapping calls in async functions. - Use the pipeline with TensorFlow backend by installing
tensorflowinstead oftorch.
from transformers import pipeline
# Using a different model
classifier = pipeline('text-classification', model='roberta-base-finetuned-sentiment')
result = classifier("I am happy with the service.")
print(result) output
[{'label': 'POSITIVE', 'score': 0.9991}] Troubleshooting
- If you get a
ModelNotFoundError, verify the model name is correct and you have internet access to download it. - For slow performance, ensure you have a compatible GPU or use smaller models.
- If you see
ImportError, confirmtransformersandtorchare installed properly.
Key Takeaways
- Use Hugging Face's
pipelinewith tasktext-classificationfor easy text classification. - Select pretrained models like
distilbert-base-uncased-finetuned-sst-2-englishfor sentiment analysis. - Install
transformersand a backend liketorchto run the pipeline locally. - You can switch models or run asynchronously for flexibility.
- Check model names and dependencies if you encounter errors.