-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintent_classifier.py
More file actions
67 lines (55 loc) · 2.89 KB
/
intent_classifier.py
File metadata and controls
67 lines (55 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
import torch
import torch.nn.functional as F
import face_detector
import text_speech
from nltk.stem import SnowballStemmer
detected_intent = ""
tags = ['Greetings', 'NameInquiry', 'AddressInquiry',
'LastmeetingInquiry', 'RelationshipInquiry', 'GoodBye']
model = DistilBertForSequenceClassification.from_pretrained(
'intent_cf_model')
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
ignore_words = ['?', '!', '.', ',','is','are','am','was','were','do','does','did','can','could','may','might','must','shall','should','will','would','have','has','had','a','an','the','of','in','on','at','to','for','from','by','with','and','or','but','if','then','else','all','any','both','each','few','more','most','other','some','such','no','nor','not','only','own','same','so','than','too','very','s','t','can','will','just','don','should','now']
stemmer = SnowballStemmer('english')
model.eval()
def intent_classify(text):
with torch.no_grad():
if text == "exit":
face_detector.releaseCam()
words = text.lower().split()
filtered_words = [word for word in words if word not in ignore_words]
stemmed_words = [stemmer.stem(word) for word in filtered_words]
text = ' '.join(stemmed_words)
inputs = tokenizer(text, padding=True, truncation=True,
max_length=512, return_tensors="pt")
# Pass the tokenized text to the model to get the predictions
outputs = model(**inputs)
predictions = F.softmax(outputs['logits'], dim=1)
print(predictions)
max_score, max_index = torch.max(predictions[0], dim=0)
if max_score > 0.90:
detected_intent = tags[max_index]
print(tags[max_index])
if detected_intent == "NameInquiry":
face_detector.take_photo('name')
elif detected_intent == "AddressInquiry":
face_detector.take_photo('address')
elif detected_intent == "LastmeetingInquiry":
face_detector.take_photo('last_meet')
elif detected_intent == "RelationshipInquiry":
face_detector.take_photo('relationship')
elif detected_intent == "GoodBye":
return detected_intent
elif detected_intent == "Greetings":
text_speech.speak("Hello, I am here to help you.")
else:
print("I am not sure what you are asking.")
text_speech.speak("I am not sure what you are asking.")
def text_mode():
while True:
text = input("Enter your query: ")
intent = intent_classify(text)
if intent == "GoodBye":
text_speech.speak("Goodbye")
break