-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChatbot.py
More file actions
93 lines (81 loc) · 4.44 KB
/
Chatbot.py
File metadata and controls
93 lines (81 loc) · 4.44 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from keras.models import Model, model_from_json
from keras.layers import Input, LSTM, Dense, Embedding
from keras.preprocessing.sequence import pad_sequences
import numpy as np
import nltk
HIDDEN_UNITS = 256
class ChatBot():
"""
This is ChatBot class it takes weights for the Neural Network, compliling model
and returns prediction in responce to input text
"""
def __init__(self):
"""
define all required parameters, rebuild model and load weights
"""
self.input_word2idx = np.load('model/word-input-word2idx.npy').item()
self.input_idx2word = np.load('model/word-input-idx2word.npy').item()
self.target_word2idx = np.load('model/word-target-word2idx.npy').item()
self.target_idx2word = np.load('model/word-target-idx2word.npy').item()
context = np.load('model/word-context.npy').item()
self.max_encoder_seq_length = context['encoder_max_seq_length']
self.max_decoder_seq_length = context['decoder_max_seq_length']
self.num_encoder_tokens = context['num_encoder_tokens']
self.num_decoder_tokens = context['num_decoder_tokens']
self.ultimate_question = 'Answer to the Ultimate Question of Life, the Universe, and Everything'
encoder_inputs = Input(shape=(None, ), name='encoder_inputs')
encoder_embedding = Embedding(input_dim=self.num_encoder_tokens, output_dim=HIDDEN_UNITS,
input_length=self.max_encoder_seq_length, name='encoder_embedding')
encoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, name="encoder_lstm")
encoder_outputs, encoder_state_h, encoder_state_c = encoder_lstm(encoder_embedding(encoder_inputs))
encoder_states = [encoder_state_h, encoder_state_c]
decoder_inputs = Input(shape=(None, self.num_decoder_tokens), name='decoder_inputs')
decoder_lstm = LSTM(units=HIDDEN_UNITS, return_sequences=True, return_state=True, name='decoder_lstm')
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(self.num_decoder_tokens, activation='softmax', name='decoder_dense')
decoder_outputs = decoder_dense(decoder_outputs)
self.model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
self.model.load_weights('model/word-weights.h5')
self.model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
self.encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_inputs = [Input(shape=(HIDDEN_UNITS,)), Input(shape=(HIDDEN_UNITS,))]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_state_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
self.decoder_model = Model([decoder_inputs] + decoder_state_inputs, [decoder_outputs] + decoder_states)
def reply(self, input_text):
"""
Takes input_text and return predicted responce
:param input_text: string
:return: predicted_text: string
"""
if input_text == self.ultimate_question:
return '42'
input_seq = []
input_wids = []
for word in nltk.word_tokenize(input_text.lower()):
idx = 1
if word in self.input_word2idx:
idx = self.input_word2idx[word]
input_wids.append(idx)
input_seq.append(input_wids)
input_seq = pad_sequences(input_seq, self.max_encoder_seq_length)
states_value = self.encoder_model.predict(input_seq)
target_seq = np.zeros((1, 1, self.num_decoder_tokens))
target_seq[0, 0, self.target_word2idx['START']] = 1
target_text = ''
target_text_len = 0
terminated = False
while not terminated:
output_tokens, h, c = self.decoder_model.predict([target_seq] + states_value)
sample_token_idx = np.argmax(output_tokens[0, -1, :])
sample_word = self.target_idx2word[sample_token_idx]
target_text_len += 1
if sample_word != 'START' and sample_word != 'END':
target_text += ' ' + sample_word
if sample_word == 'END' or target_text_len >= self.max_decoder_seq_length:
terminated = True
target_seq = np.zeros((1, 1, self.num_decoder_tokens))
target_seq[0, 0, sample_token_idx] = 1
states_value = [h, c]
return target_text.strip().replace('UNK', '')