Skip to content

Instantly share code, notes, and snippets.

@theleoborges
Last active January 6, 2025 06:14
Show Gist options
  • Save theleoborges/ef16e716c407d08e0b105f48e53224e4 to your computer and use it in GitHub Desktop.
Save theleoborges/ef16e716c407d08e0b105f48e53224e4 to your computer and use it in GitHub Desktop.
Intent classification with BERT
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_name = 'Falconsai/intent_classification'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
input_text = "My order hasn't been delivered."
inputs = tokenizer(input_text, return_tensors='pt', padding=True, truncation=True, max_length=64)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_id = torch.argmax(logits, dim=1).item()
predicted_label = model.config.id2label[predicted_class_id]
print(f"Predicted intent: {predicted_label}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment