pythonbeginner
Zero-Shot Text Classification
Classify text into custom categories using zero-shot NLI models without training data.
pythonPress ⌘/Ctrl + Shift + C to copy
from transformers import pipeline
classifier = pipeline('zero-shot-classification', model='facebook/bart-large-mnli')
texts = [
'The stock market crashed 10% today.',
'Scientists discover new species in Amazon.',
'New iPhone model launches next month.',
]
labels = ['finance', 'science', 'technology', 'sports', 'politics']
for text in texts:
result = classifier(text, candidate_labels=labels)
top = result['labels'][0]
score = result['scores'][0]
print(f'{text[:50]!r}')
print(f' -> {top} ({score:.2%})\n')Use Cases
- content categorization
- intent detection
- topic classification
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Text Classification with Hugging Face
Fine-tune or use pre-trained Hugging Face models for text classification.
Best for: Sentiment analysis
#ai#huggingface
typescriptintermediate
AI Text Classification with Prompts
Classify text into categories using structured prompting with confidence scores and explanations.
Best for: Automated support ticket routing
#classification#prompt-engineering
pythonbeginner
Hugging Face Transformers Pipeline
Run text classification, NER, summarisation, and translation tasks with the HF Pipelines API.
Best for: NLP tasks
#huggingface#transformers
pythonintermediate
Hugging Face Inference API
Run ML models via the Hugging Face Inference API for text generation, classification, and embeddings.
Best for: Text classification
#huggingface#inference