python 18 lines · 1 tab

Using Hugging Face transformers for modern NLP inference

1 tab
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline

model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

classifier = pipeline(
    task='text-classification',
    model=model,
    tokenizer=tokenizer,
    truncation=True,
)

texts = [
    'The checkout flow is fast and intuitive.',
    'Customer support never replied to my ticket.',
]
print(classifier(texts))
1 file · python Explain with highlit

I use transformers when the text task justifies contextual modeling and the serving budget can handle it. The fastest path to value is usually starting with pretrained checkpoints, measuring latency, and then deciding whether quantization, distillation, or simpler baselines are sufficient. Fancy models still need boring operational discipline.

Share this code

Here's the card — post it anywhere.

Using Hugging Face transformers for modern NLP inference — share card
Link copied