File size: 9,793 Bytes
6b4dab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python

"""The chatbot model training app is the "Train the chatbot model page" in the main page"""

# the code for the page
def run_app():
    """
    Application code for the Chatbot Training app
    """
    ############################
    # :: IMPORTS AND CONSTANTS #
    ############################

    # machine learning modules
    import numpy as np
    import random

    # Deep Learning modules
    from keras.models import Sequential
    from keras.layers import Dense, Dropout
    from keras.optimizers import SGD

    import streamlit as st

    # suppress warnings
    import warnings
    warnings.filterwarnings('ignore')

    # for file processing
    import json
    import pickle

    # natural language processiong modules
    import nltk
    from nltk.stem import WordNetLemmatizer
    lemmatizer = WordNetLemmatizer()

    # Load NLTK dependencies
    nltk.download('punkt')
    nltk.download('wordnet')

    ######################################
    # :: HELPER FUNCTIONS FOR PROCESSING #
    ######################################

    def read_json_file(uploaded_file):
        """
        Reads a JSON file and returns its contents.

        Args:
            uploaded_file (_UploadedFile): The uploaded JSON file.

        Returns:
            dict: The contents of the JSON file.
        """
        data = json.loads(uploaded_file.read().decode("utf-8"))
        return data

    def preprocess_data(data):
        """
        Preprocesses the JSON data.

        Args:
            data (dict): The JSON data.

        Returns:
            tuple: The pre-processed data (words, classes, documents).
        """

        # creating lists for NLP
        words=[]
        classes = []
        documents = []
        ignore_words = ['?', '!']

        lemmatizer = WordNetLemmatizer()
        for command in data['intents']:
            for pattern in command['patterns']:
                # Tokenize each word
                w = nltk.word_tokenize(pattern)
                words.extend(w)
                # Add documents to the corpus
                documents.append((w, command['tag']))
                # Add to classes list
                if command['tag'] not in classes:
                    classes.append(command['tag'])

        # Lemmatize, convert to lowercase, and remove duplicates
        words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
        words = sorted(list(set(words)))
        classes = sorted(list(set(classes)))

        return words, classes, documents

    def load_pickle_data():
        """
        Loads the pickle data.

        Returns:
            tuple: The loaded pickle data (words, classes, documents).
        """
        with open('words.pkl', 'rb') as f:
            words = pickle.load(f)
        with open('classes.pkl', 'rb') as f:
            classes = pickle.load(f)
        with open('documents.pkl', 'rb') as f:
            documents = pickle.load(f)
        return words, classes, documents

    def create_training_data(words, classes, documents):
        """
        Create the training data for the chatbot model.

        Args:
            words (list): List of words in the vocabulary.
            classes (list): List of classes/intents.
            documents (list): List of (pattern, intent) pairs.

        Returns:
            tuple: Tuple containing the training data as NumPy arrays (train_x, train_y).
        """
        training = []
        output_empty = np.zeros(len(classes), dtype=int)

        for doc in documents:
            bag = np.zeros(len(words), dtype=int)
            pattern_words = doc[0]
            pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words]

            for i, w in enumerate(words):
                if w in pattern_words:
                    bag[i] = 1

            output_row = np.copy(output_empty)
            output_row[classes.index(doc[1])] = 1

            training.append([bag, output_row])

        random.shuffle(training)
        training = np.array(training)

        train_x = list(training[:, 0])
        train_y = list(training[:, 1])

        return np.array(train_x), np.array(train_y)

    ###############
    # :: MAIN APP #
    ###############

    st.markdown("<h1 style='text-align: left;'>Train the chatbot model ⚙️</h1>", unsafe_allow_html=True)
    st.subheader(
    """
    Let's train the chatbot model by following the sequence of steps provided below:
    """
    )
    # Summary of steps
    st.markdown(
        """
        **Summary of Steps:**
        - Upload the `commands.json` file for processing. The file should contain the commands and their corresponding tags.
        - Load the preprocessed data from pickle files (optional if you have already processed the data previously).
        - Create the training data by converting the commands into numerical vectors.
        - Build the model by specifying the number of layers, epochs, batch size, and activation function.

        Once the model is built, the training loss and accuracy will be displayed.
        """
    )
    st.write("---")

    if st.checkbox("Upload the commands.json file for processing"):
        st.subheader("JSON File Uploader")
        uploaded_file = st.file_uploader("Upload JSON file", type="json")

        if uploaded_file is not None:
            try:
                data = read_json_file(uploaded_file)

                st.json(data)

                # Preprocess the data
                words, classes, documents = preprocess_data(data)

                # Save the preprocessed data as pickle files
                with open('words.pkl', 'wb') as f:
                    pickle.dump(words, f)
                with open('classes.pkl', 'wb') as f:
                    pickle.dump(classes, f)
                with open('documents.pkl', 'wb') as f:
                    pickle.dump(documents, f)

                # Display the processed data
                st.write("Preprocessing Results:")
                st.write(len(documents), "documents")
                st.write(len(classes), "classes", classes)
                st.write(len(words), "unique lemmatized words", words)

            except json.JSONDecodeError:
                st.error("Invalid JSON file.")
    
    if st.checkbox("Load pickle data"):
        # Initialize the progress bar
        progress_bar = st.progress(0) 
        
        with st.spinner("Creating training data ..."):

            words, classes, documents = load_pickle_data()
            
        # Update the progress bar
        progress_bar.progress(100)

        st.write("Words:")
        st.write(words)

        st.write("Classes:")
        st.write(classes)

        st.write("Documents:")
        st.write(documents)

    if st.checkbox("Create training data"):
        try:
            # Initialize the progress bar
            progress_bar = st.progress(0)  

            with st.spinner("Creating training data ..."):
                train_x, train_y = create_training_data(words, classes, documents)

            # Update the progress bar
            progress_bar.progress(100)

            st.success("Training data created")

            st.write(f"Training data (train_x): {len(train_x)} samples")
            st.write(f"Training data (train_y): {len(train_y)} samples")
    
        except Exception as e:
            st.error("An error occurred during training data creation.")
            st.error(str(e))

    if st.checkbox("Build the model"):
        # Get user inputs
        num_layers = st.number_input("Number of layers", min_value=1, max_value=10, value=3)
        epochs = st.number_input("Number of epochs", min_value=1, max_value=1000, value=200)
        batch_size = st.number_input("Batch size", min_value=1, max_value=100, value=5)
        activation_functions = ['relu', 'sigmoid', 'softmax']
        activation_function = st.selectbox("Activation function", options=activation_functions)

        try:
            # Initialize the progress bar
            progress_bar = st.progress(0)  

            with st.spinner("Building the model ..."):
                # Create model
                model = Sequential()

                # Add layers to the model based on user input
                for i in range(num_layers):
                    if i == 0:
                        # Input layer
                        model.add(Dense(128, input_shape=(len(train_x[0]),), activation=activation_function))
                    else:
                        # Hidden layers
                        model.add(Dense(64, activation=activation_function))
                    model.add(Dropout(0.5))

                # Output layer
                model.add(Dense(len(train_y[0]), activation='softmax'))

                # Compile model
                sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
                model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

                # Fit the model
                hist = model.fit(np.array(train_x), np.array(train_y), epochs=epochs, batch_size=batch_size, verbose=1)

                # Save the model
                model.save('chatbot_model.h5', hist)

            # Update the progress bar
            progress_bar.progress(100)

            st.success("The chatbot model is created")

            # Display training loss and accuracy summary
            st.subheader("Training Summary")
            st.write("Training Loss:", hist.history['loss'][-1])
            st.write("Training Accuracy:", hist.history['accuracy'][-1])
    
        except Exception as e:
            st.error("An error occurred during model building.")
            st.error(str(e))

# End of app
if __name__ == "__main__":

    run_app()