DHRUV SHEKHAWAT commited on
Commit
130d634
·
1 Parent(s): 9211632

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -75
app.py CHANGED
@@ -5,7 +5,8 @@ from keras.layers import Dropout, LayerNormalization
5
  from keras.models import Model
6
  from keras.utils import pad_sequences
7
  import numpy as np
8
-
 
9
  class TransformerChatbot(Model):
10
  def __init__(self, vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate):
11
  super(TransformerChatbot, self).__init__()
@@ -39,29 +40,8 @@ class TransformerChatbot(Model):
39
  def create_padding_mask(self, seq):
40
  mask = tf.cast(tf.math.equal(seq, 0), tf.float32)
41
  return mask[:, tf.newaxis, tf.newaxis, :]
 
42
 
43
- st.title("UniGLM TEXT completion Model")
44
- st.subheader("Next Word Prediction AI Model by Webraft-AI")
45
- #Picking what NLP task you want to do
46
- option = st.selectbox('Model',('13M','26M')) #option is stored in this variable
47
- #Textbox for text user is entering
48
- st.subheader("Enter a word from which a sentence / word would be predicted")
49
-
50
- text2 = st.text_input('Enter word: ') #text is stored in this variable
51
-
52
-
53
- if option == '13M':
54
- vocab_size = 100000
55
- max_len = 1
56
- d_model = 64 # 64 , 1024
57
- n_head = 4 # 8 , 16
58
- ff_dim = 256 # 256 , 2048
59
- dropout_rate = 0.1 # 0.5 , 0.2
60
- weights = "predict3"
61
- datafile = "data2.txt"
62
- dict = "dict_predict3.bin.npz"
63
- len = 15
64
- text2 = text2
65
  with open(datafile,"r") as f:
66
  text = f.read()
67
  text = text.lower()
@@ -110,9 +90,43 @@ if option == '13M':
110
  input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
111
 
112
  out2 = output_sentence
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
 
115
  elif option=="26M":
 
 
 
 
 
116
  vocab_size = 100000
117
  max_len = 1
118
  d_model = 128 # 64 , 1024
@@ -122,57 +136,10 @@ elif option=="26M":
122
  weights = "predict5"
123
  datafile = "data2.txt"
124
  dict = "dict_predict3.bin.npz"
125
- len = 15
126
- text2 = text2
127
- with open(datafile,"r") as f:
128
- text = f.read()
129
- text = text.lower()
130
- words = text.split()
131
- loaded_dict = np.load(dict, allow_pickle=True)
132
- word_to_num = loaded_dict["word_to_num"].item()
133
- num_to_word = loaded_dict["num_to_word"].item()
134
- X = []
135
- Y = []
136
- for i in range(len(words)-1):
137
- word = words[i]
138
- next_word = words[i+1]
139
- X.append(word_to_num[word])
140
- Y.append(word_to_num[next_word])
141
- Y.append(0)
142
-
143
- X.append(word_to_num[words[-1]])
144
- X_train = pad_sequences([X])
145
- y_train = pad_sequences([Y])
146
-
147
-
148
-
149
- chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)
150
- chatbot.load_weights(weights)
151
- chatbot.build(input_shape=(None, max_len)) # Build the model
152
- chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
153
-
154
- for i in range(1):
155
- other_text1 = text2
156
- other_text1 = other_text1.lower()
157
- other_words1 = other_text1.split()
158
- other_num1 = [word_to_num[word] for word in other_words1]
159
- given_X1 = other_num1
160
- input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
161
- output_sentence = ""
162
- for _ in range(len):
163
- predicted_token = np.argmax(chatbot.predict(input_sequence1), axis=-1)
164
- predicted_token = predicted_token.item()
165
- out = num_to_word[predicted_token]
166
-
167
-
168
- output_sentence = out
169
-
170
- given_X1 = given_X1[1:]
171
- given_X1.append(predicted_token)
172
- input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
173
-
174
- out2 = output_sentence
175
  else:
176
  out2 = "Error: Wrong Model Selected"
177
- st.write("Predicted Text: ")
178
- st.write(out2)
 
5
  from keras.models import Model
6
  from keras.utils import pad_sequences
7
  import numpy as np
8
+ import logging
9
+ logging.getLogger('tensorflow').setLevel(logging.ERROR)
10
  class TransformerChatbot(Model):
11
  def __init__(self, vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate):
12
  super(TransformerChatbot, self).__init__()
 
40
  def create_padding_mask(self, seq):
41
  mask = tf.cast(tf.math.equal(seq, 0), tf.float32)
42
  return mask[:, tf.newaxis, tf.newaxis, :]
43
+ def completion_model(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate,weights,datafile,dict,len,text2):
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  with open(datafile,"r") as f:
46
  text = f.read()
47
  text = text.lower()
 
90
  input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
91
 
92
  out2 = output_sentence
93
+ return out2
94
+ st.title("UniGLM TEXT completion Model")
95
+ st.subheader("Next Word Prediction AI Model by Webraft-AI")
96
+ #Picking what NLP task you want to do
97
+ option = st.selectbox('Model',('13M','26M')) #option is stored in this variable
98
+ #Textbox for text user is entering
99
+ st.subheader("Enter a word from which a sentence / word would be predicted")
100
+
101
+ text2 = st.text_input('Enter word: ') #text is stored in this variable
102
+
103
+
104
+ if option == '13M':
105
+ option2 = st.selectbox('Type',('word','sentence'))
106
+ if option2 == 'word':
107
+ len = 1
108
+ else:
109
+ len = 13
110
+ vocab_size = 100000
111
+ max_len = 1
112
+ d_model = 64 # 64 , 1024
113
+ n_head = 4 # 8 , 16
114
+ ff_dim = 256 # 256 , 2048
115
+ dropout_rate = 0.1 # 0.5 , 0.2
116
+ weights = "predict3"
117
+ datafile = "data2.txt"
118
+ dict = "dict_predict3.bin.npz"
119
+ out2 = completion_model(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate,weights,datafile,dict,len,text2)
120
+ st.write("Predicted Text: ")
121
+ st.write(out2)
122
 
123
 
124
  elif option=="26M":
125
+ option2 = st.selectbox('Type',('word','sentence'))
126
+ if option2 == 'word':
127
+ len = 1
128
+ else:
129
+ len = 13
130
  vocab_size = 100000
131
  max_len = 1
132
  d_model = 128 # 64 , 1024
 
136
  weights = "predict5"
137
  datafile = "data2.txt"
138
  dict = "dict_predict3.bin.npz"
139
+ out2 = completion_model(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate,weights,datafile,dict,len,text2)
140
+ st.write("Predicted Text: ")
141
+ st.write(out2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  else:
143
  out2 = "Error: Wrong Model Selected"
144
+
145
+ st.write(out2)