DHRUV SHEKHAWAT commited on
Commit
6d8ed0e
·
1 Parent(s): 35294b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -7
app.py CHANGED
@@ -42,9 +42,9 @@ class TransformerChatbot(Model):
42
  st.title("UniGLM TEXT completion Model")
43
  st.subheader("Next Word Prediction AI Model by Webraft-AI")
44
  #Picking what NLP task you want to do
45
- option = st.selectbox('Model',('1')) #option is stored in this variable
46
  #Textbox for text user is entering
47
- st.subheader("Enter a word from which a sentence would be predicted")
48
  text2 = st.text_input('Enter word: ') #text is stored in this variable
49
 
50
  if option == '1':
@@ -87,16 +87,15 @@ if option == '1':
87
  other_num1 = [word_to_num[word] for word in other_words1]
88
  given_X1 = other_num1
89
  input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
90
- output_sentence = other_text1+""
91
  for _ in range(1):
92
  predicted_token = np.argmax(chatbot.predict(input_sequence1), axis=-1)
93
  predicted_token = predicted_token.item()
94
  out = num_to_word[predicted_token]
95
 
96
 
97
- output_sentence += " " + out
98
- if out == ".":
99
- break
100
  given_X1 = given_X1[1:]
101
  given_X1.append(predicted_token)
102
  input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
@@ -105,7 +104,60 @@ if option == '1':
105
 
106
 
107
  else:
108
- out2 = "Wrong Model"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  st.write("Predicted Text: ")
111
  st.write(out2)
 
42
  st.title("UniGLM TEXT completion Model")
43
  st.subheader("Next Word Prediction AI Model by Webraft-AI")
44
  #Picking what NLP task you want to do
45
+ option = st.selectbox('Model',('1','2')) #option is stored in this variable
46
  #Textbox for text user is entering
47
+ st.subheader("Enter a word from which a sentence / word would be predicted")
48
  text2 = st.text_input('Enter word: ') #text is stored in this variable
49
 
50
  if option == '1':
 
87
  other_num1 = [word_to_num[word] for word in other_words1]
88
  given_X1 = other_num1
89
  input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
90
+ output_sentence = ""
91
  for _ in range(1):
92
  predicted_token = np.argmax(chatbot.predict(input_sequence1), axis=-1)
93
  predicted_token = predicted_token.item()
94
  out = num_to_word[predicted_token]
95
 
96
 
97
+ output_sentence = out
98
+
 
99
  given_X1 = given_X1[1:]
100
  given_X1.append(predicted_token)
101
  input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
 
104
 
105
 
106
  else:
107
+ with open("data2.txt","r") as f:
108
+ text = f.read()
109
+ text = text.lower()
110
+ words = text.split()
111
+ loaded_dict = np.load("dict_predict3.bin.npz", allow_pickle=True)
112
+ word_to_num = loaded_dict["word_to_num"].item()
113
+ num_to_word = loaded_dict["num_to_word"].item()
114
+ X = []
115
+ Y = []
116
+ for i in range(len(words)-1):
117
+ word = words[i]
118
+ next_word = words[i+1]
119
+ X.append(word_to_num[word])
120
+ Y.append(word_to_num[next_word])
121
+ Y.append(0)
122
+
123
+ X.append(word_to_num[words[-1]])
124
+ X_train = pad_sequences([X])
125
+ y_train = pad_sequences([Y])
126
+ vocab_size = 100000
127
+ max_len = 1
128
+ d_model = 64 # 64 , 1024
129
+ n_head = 4 # 8 , 16
130
+ ff_dim = 256 # 256 , 2048
131
+ dropout_rate = 0.1 # 0.5 , 0.2
132
+
133
+
134
+ chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)
135
+ chatbot.load_weights("predict3")
136
+ chatbot.build(input_shape=(None, max_len)) # Build the model
137
+ chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
138
+
139
+ for i in range(1):
140
+ other_text1 = text2
141
+ other_text1 = other_text1.lower()
142
+ other_words1 = other_text1.split()
143
+ other_num1 = [word_to_num[word] for word in other_words1]
144
+ given_X1 = other_num1
145
+ input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
146
+ output_sentence = other_text1+""
147
+ for _ in range(10):
148
+ predicted_token = np.argmax(chatbot.predict(input_sequence1), axis=-1)
149
+ predicted_token = predicted_token.item()
150
+ out = num_to_word[predicted_token]
151
+
152
+
153
+ output_sentence += " " + out
154
+ if out == ".":
155
+ break
156
+ given_X1 = given_X1[1:]
157
+ given_X1.append(predicted_token)
158
+ input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
159
+
160
+ out2 = output_sentence
161
 
162
  st.write("Predicted Text: ")
163
  st.write(out2)