-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_bot.py
More file actions
69 lines (49 loc) · 1.73 KB
/
predict_bot.py
File metadata and controls
69 lines (49 loc) · 1.73 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
68
#Text Data Preprocessing Lib
import nltk
# add the below code
nltk.download('punkt')
import json
import pickle
import numpy as np
import random
ignore_words = ['?', '!',',','.', "'s", "'m"]
# Model Load Lib
import tensorflow
from data_preprocessing import get_stem_words
model = tensorflow.keras.models.load_model('chatbot_model.h5')
# Load data files
intents = json.loads(open('intents.json').read())
words = pickle.load(open('words.pkl','rb'))
classes = pickle.load(open('classes.pkl','rb'))
def preprocess_user_input(user_input):
input_word_token_1 = nltk.word_tokenize(user_input)
input_word_token_2 = get_stem_words(input_word_token_1, ignore_words)
input_word_token_2 = sorted(list(set(input_word_token_2)))
bag=[]
bag_of_words = []
# Input data encoding
for word in words:
if word in input_word_token_2:
bag_of_words.append(1)
else:
bag_of_words.append(0)
bag.append(bag_of_words)
return np.array(bag)
def bot_class_prediction(user_input):
inp = preprocess_user_input(user_input)
prediction = model.predict(inp)
predicted_class_label = np.argmax(prediction[0])
return predicted_class_label
def bot_response(user_input):
predicted_class_label = bot_class_prediction(user_input)
predicted_class = classes[predicted_class_label]
for intent in intents['intents']:
if intent['tag']==predicted_class:
bot_response = random.choice(intent['responses'])
return bot_response
print("Hi I am Stella, How Can I help you?")
while True:
user_input = input("Type your message here:")
print("User Input: ", user_input)
response = bot_response(user_input)
print("Bot Response: ", response)