-
Notifications
You must be signed in to change notification settings - Fork 1
/
model2.py
120 lines (95 loc) · 4.03 KB
/
model2.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import json
# Opening JSON file
file = open('D:\coding_pranav\inheritance\chatbot\mentalhealth - Copy.json','r')
data = json.load(file)
df = pd.DataFrame(data['intents'])
df
dic = {"tag":[], "patterns":[], "responses":[]}
for i in range(len(df)):
ptrns = df[df.index == i]['patterns'].values[0]
rspns = df[df.index == i]['responses'].values[0]
tag = df[df.index == i]['tag'].values[0]
for j in range(len(ptrns)):
dic['tag'].append(tag)
dic['patterns'].append(ptrns[j])
dic['responses'].append(rspns)
df = pd.DataFrame.from_dict(dic)
df
df['tag'].unique()
from tensorflow.keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(lower=True, split=' ')
tokenizer.fit_on_texts(df['patterns'])
tokenizer.get_config()
vacab_size = len(tokenizer.word_index)
print('number of unique words = ', vacab_size)
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.preprocessing import LabelEncoder
ptrn2seq = tokenizer.texts_to_sequences(df['patterns'])
X = pad_sequences(ptrn2seq, padding='post')
print('X shape = ', X.shape)
lbl_enc = LabelEncoder()
y = lbl_enc.fit_transform(df['tag'])
print('y shape = ', y.shape)
print('num of classes = ', len(np.unique(y)))
import tensorflow
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Embedding, LSTM, LayerNormalization, Dense, Dropout
from tensorflow.keras.utils import plot_model
model = Sequential()
model.add(Input(shape=(X.shape[1])))
model.add(Embedding(input_dim=vacab_size+1, output_dim=100, mask_zero=True))
model.add(LSTM(32, return_sequences=True))
model.add(LayerNormalization())
model.add(LSTM(32, return_sequences=True))
model.add(LayerNormalization())
model.add(LSTM(32))
model.add(LayerNormalization())
model.add(Dense(128, activation="relu"))
model.add(LayerNormalization())
model.add(Dropout(0.2))
model.add(Dense(128, activation="relu"))
model.add(LayerNormalization())
model.add(Dropout(0.2))
model.add(Dense(len(np.unique(y)), activation="softmax"))
model.compile(optimizer='adam', loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.summary()
#plot_model(model, show_shapes=True)
model_history = model.fit(x=X,
y=y,
batch_size=10,
callbacks=[tensorflow.keras.callbacks.EarlyStopping(monitor='accuracy', patience=3)],
epochs=50)
import re
import random
def generate_answer(pattern):
text = []
txt = re.sub('[^a-zA-Z\']', ' ', pattern)
txt = txt.lower()
txt = txt.split()
txt = " ".join(txt)
text.append(txt)
x_test = tokenizer.texts_to_sequences(text)
x_test = np.array(x_test).squeeze()
x_test = pad_sequences([x_test], padding='post', maxlen=X.shape[1])
y_pred = model.predict(x_test)
y_pred = y_pred.argmax()
tag = lbl_enc.inverse_transform([y_pred])[0]
responses = df[df['tag'] == tag]['responses'].values[0]
#print("you: {}".format(pattern))
print("{}".format(random.choice(responses)))
generate_answer('Well... I\'m feeling sad!')
generate_answer('Hi! How are you?')