-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChatBot.py
75 lines (56 loc) · 1.92 KB
/
ChatBot.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import random
import json
import torch
from model import NuralNetworks
from nltk_utils import tokenize , bag_of_words
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open("./data/symptoms.json")as file:
intents = json.load(file)
FILE = 'data.pth'
data = torch.load(FILE)
input_size = data['input_size']
hidden_size = data['hidden_size']
num_classes =data['num_classes']
all_words = data['all_words']
tags = data['tags']
model_state = data['model_state']
model = NuralNetworks(input_size, hidden_size, num_classes).to(device)
model.load_state_dict(model_state)
model.eval()
CHAT_BOT_NAME = 'Guru'
# print('Hey My name is Guru How May i Help You? ')
def get_response(query):
query = tokenize(query)
x = bag_of_words(query , all_words)
x = x.reshape(1 , x.shape[0])
x = torch.from_numpy(x)
output = model(x)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
return (f"{CHAT_BOT_NAME}: {random.choice(intent['responses'])}")
else:
return (f"{CHAT_BOT_NAME}: I Dont UnderStand!")
# while True:
# query = input("You: ")
# if query == 'exit':
# break
# query = tokenize(query)
# x = bag_of_words(query , all_words)
# x = x.reshape(1 , x.shape[0])
# x = torch.from_numpy(x)
# output = model(x)
# _, predicted = torch.max(output, dim=1)
# tag = tags[predicted.item()]
# probs = torch.softmax(output, dim=1)
# prob = probs[0][predicted.item()]
# if prob.item() > 0.75:
# for intent in intents['intents']:
# if tag == intent["tag"]:
# print(f"{CHAT_BOT_NAME}: {random.choice(intent['responses'])}")
# else:
# print(f"{CHAT_BOT_NAME}: I Dont UnderStand!")