-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
48 lines (35 loc) · 1.06 KB
/
chat.py
File metadata and controls
48 lines (35 loc) · 1.06 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
import random
import json
import torch
from model import NeuralNet
from nltk_utils import bagset, tokenize
device=torch.device('cpu')
with open('tagresp.json','r') as f:
tagresp=json.load(f)
FILE="data.pth"
data=torch.load(FILE)
input_size=data["input_size"]
hidden_size=data["hidden_size"]
output_size=data["output_size"]
wordset=data["wordset"]
tags=data["tags"]
model_state=data["model_state"]
model= NeuralNet(input_size,hidden_size,output_size).to(device)
model.load_state_dict(model_state)
model.eval()
botname="Annie"
def get_response(msg):
sentence=tokenize(msg)
X= bagset(sentence,wordset)
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 ele in tagresp["tagresp"]:
if tag==ele["tag"]:
return random.choice(ele["responses"])
return "I do not understand..."