-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathchat.py
49 lines (38 loc) · 1.58 KB
/
chat.py
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
import random
import json
import torch
from model import NeuralNet
from nltk_utils import bag_of_words, tokenize
class chatBot():
def __init__(self):
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open('intents.json', 'r') as json_data:
self.intents = json.load(json_data)
FILE = "data.pth"
self.data = torch.load(FILE)
self.input_size = self.data["input_size"]
self.hidden_size = self.data["hidden_size"]
self.output_size = self.data["output_size"]
self.all_words = self.data['all_words']
self.tags = self.data['tags']
self.model_state = self.data["model_state"]
self.model = NeuralNet(self.input_size, self.hidden_size, self.output_size).to(self.device)
self.model.load_state_dict(self.model_state)
self.model.eval()
def get_response(self, question):
# sentence = "do you use credit cards?"
sentence = tokenize(question)
X = bag_of_words(sentence, self.all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X).to(self.device)
output = self.model(X)
_, predicted = torch.max(output, dim=1)
tag = self.tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in self.intents['intents']:
if tag == intent["tag"]:
return random.choice(intent['responses'])
else:
return "I am sorry I do not understand the question! 🥺"