nileshhanotia commited on
Commit
848ab8c
1 Parent(s): 320370e

Create intent_classifier.py

Browse files
Files changed (1) hide show
  1. intent_classifier.py +16 -0
intent_classifier.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
2
+ import torch
3
+
4
+ class IntentClassifier:
5
+ def __init__(self):
6
+ self.model_name = "distilbert-base-uncased-finetuned-sst-2-english"
7
+ self.model = AutoModelForSequenceClassification.from_pretrained(self.model_name, num_labels=2)
8
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
9
+ self.intents = {0: "database_query", 1: "product_description"}
10
+
11
+ def classify(self, query):
12
+ inputs = self.tokenizer(query, return_tensors="pt", truncation=True, padding=True)
13
+ outputs = self.model(**inputs)
14
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
15
+ predicted_class = torch.argmax(probabilities).item()
16
+ return self.intents[predicted_class], probabilities[0][predicted_class].item()