DHRUV SHEKHAWAT commited on
Commit
9e17522
·
1 Parent(s): 93c51a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -2
app.py CHANGED
@@ -117,7 +117,55 @@ if option == '13M_OLD':
117
  weights = "predict3"
118
  datafile = "data2.txt"
119
  dict = "dict_predict3.bin.npz"
120
- out2 = completion_model(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate,weights,datafile,dict,len2,text2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  st.write("Predicted Text: ")
122
  st.write(out2)
123
 
@@ -137,7 +185,63 @@ elif option=="26M_OLD":
137
  weights = "predict1"
138
  datafile = "data2.txt"
139
  dict = "dict_predict1.bin.npz"
140
- out2 = completion_model(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate,weights,datafile,dict,len2,text2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  st.write("Predicted Text: ")
142
  st.write(out2)
143
  else:
 
117
  weights = "predict3"
118
  datafile = "data2.txt"
119
  dict = "dict_predict3.bin.npz"
120
+ with open(datafile,"r") as f:
121
+ text = f.read()
122
+ text = text.lower()
123
+ words = text.split()
124
+ loaded_dict = np.load(dict, allow_pickle=True)
125
+ word_to_num = loaded_dict["word_to_num"].item()
126
+ num_to_word = loaded_dict["num_to_word"].item()
127
+ X = []
128
+ Y = []
129
+ for i in range(len(words)-1):
130
+ word = words[i]
131
+ next_word = words[i+1]
132
+ X.append(word_to_num[word])
133
+ Y.append(word_to_num[next_word])
134
+ Y.append(0)
135
+
136
+ X.append(word_to_num[words[-1]])
137
+ X_train = pad_sequences([X])
138
+ y_train = pad_sequences([Y])
139
+
140
+
141
+
142
+ chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)
143
+ chatbot.load_weights(weights)
144
+ chatbot.build(input_shape=(None, max_len)) # Build the model
145
+ chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
146
+
147
+ for i in range(1):
148
+ other_text2 = text2
149
+ other_text2 = other_text2.lower()
150
+ other_words2 = other_text2.split()
151
+ other_num2 = [word_to_num[word] for word in other_words2]
152
+ given_X2 = other_num2
153
+ input_sequence2 = pad_sequences([given_X2], maxlen=max_len, padding='post')
154
+ output_sentence = other_text2 + ""
155
+ for _ in range(len2):
156
+ predicted_token = np.argmax(chatbot.predict(input_sequence2), axis=-1)
157
+ predicted_token = predicted_token.item()
158
+ out = num_to_word[predicted_token]
159
+ # if out == ".":
160
+ # break
161
+
162
+ output_sentence += " " + out
163
+ given_X2 = given_X2[1:]
164
+ given_X2.append(predicted_token)
165
+ input_sequence2 = pad_sequences([given_X2], maxlen=max_len, padding='post')
166
+
167
+ out2 = output_sentence
168
+
169
  st.write("Predicted Text: ")
170
  st.write(out2)
171
 
 
185
  weights = "predict1"
186
  datafile = "data2.txt"
187
  dict = "dict_predict1.bin.npz"
188
+ vocab_size = 100000
189
+ max_len = 1
190
+ d_model = 64 # 64 , 1024
191
+ n_head = 4 # 8 , 16
192
+ ff_dim = 256 # 256 , 2048
193
+ dropout_rate = 0.1 # 0.5 , 0.2
194
+ weights = "predict3"
195
+ datafile = "data2.txt"
196
+ dict = "dict_predict3.bin.npz"
197
+ with open(datafile,"r") as f:
198
+ text = f.read()
199
+ text = text.lower()
200
+ words = text.split()
201
+ loaded_dict = np.load(dict, allow_pickle=True)
202
+ word_to_num = loaded_dict["word_to_num"].item()
203
+ num_to_word = loaded_dict["num_to_word"].item()
204
+ X = []
205
+ Y = []
206
+ for i in range(len(words)-1):
207
+ word = words[i]
208
+ next_word = words[i+1]
209
+ X.append(word_to_num[word])
210
+ Y.append(word_to_num[next_word])
211
+ Y.append(0)
212
+
213
+ X.append(word_to_num[words[-1]])
214
+ X_train = pad_sequences([X])
215
+ y_train = pad_sequences([Y])
216
+
217
+
218
+
219
+ chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)
220
+ chatbot.load_weights(weights)
221
+ chatbot.build(input_shape=(None, max_len)) # Build the model
222
+ chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
223
+
224
+ for i in range(1):
225
+ other_text2 = text2
226
+ other_text2 = other_text2.lower()
227
+ other_words2 = other_text2.split()
228
+ other_num2 = [word_to_num[word] for word in other_words2]
229
+ given_X2 = other_num2
230
+ input_sequence2 = pad_sequences([given_X2], maxlen=max_len, padding='post')
231
+ output_sentence = other_text2 + ""
232
+ for _ in range(len2):
233
+ predicted_token = np.argmax(chatbot.predict(input_sequence2), axis=-1)
234
+ predicted_token = predicted_token.item()
235
+ out = num_to_word[predicted_token]
236
+ # if out == ".":
237
+ # break
238
+
239
+ output_sentence += " " + out
240
+ given_X2 = given_X2[1:]
241
+ given_X2.append(predicted_token)
242
+ input_sequence2 = pad_sequences([given_X2], maxlen=max_len, padding='post')
243
+
244
+ out2 = output_sentence
245
  st.write("Predicted Text: ")
246
  st.write(out2)
247
  else: