Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	
		DHRUV SHEKHAWAT
		
	commited on
		
		
					Commit 
							
							·
						
						8541da5
	
1
								Parent(s):
							
							f1215a7
								
Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -41,7 +41,6 @@ class TransformerChatbot(Model): | |
| 41 | 
             
                def create_padding_mask(self, seq):
         | 
| 42 | 
             
                    mask = tf.cast(tf.math.equal(seq, 0), tf.float32)
         | 
| 43 | 
             
                    return mask[:, tf.newaxis, tf.newaxis, :]
         | 
| 44 | 
            -
             | 
| 45 | 
             
            st.title("UniGLM TEXT completion Model")
         | 
| 46 | 
             
            st.subheader("Next Word Prediction AI Model by Webraft-AI")
         | 
| 47 | 
             
            #Picking what NLP task you want to do
         | 
| @@ -126,66 +125,7 @@ elif option=="26M_OLD": | |
| 126 | 
             
                    len2 = 1
         | 
| 127 | 
             
                else:
         | 
| 128 | 
             
                    len2 = 13
         | 
| 129 | 
            -
                vocab_size = 100000
         | 
| 130 | 
            -
                max_len = 1
         | 
| 131 | 
            -
                d_model = 128  # 64 , 1024
         | 
| 132 | 
            -
                n_head = 4  # 8 , 16
         | 
| 133 | 
            -
                ff_dim = 256  # 256 , 2048
         | 
| 134 | 
            -
                dropout_rate = 0.1  # 0.5 , 0.2
         | 
| 135 | 
            -
                weights = "predict1"
         | 
| 136 | 
            -
                datafile = "data2.txt"
         | 
| 137 | 
            -
                dict = "dict_predict1.bin.npz"
         | 
| 138 | 
            -
             | 
| 139 | 
            -
                with open(datafile,"r") as f:
         | 
| 140 | 
            -
                    text = f.read()
         | 
| 141 | 
            -
                text = text.lower()
         | 
| 142 | 
            -
                words = text.split()
         | 
| 143 | 
            -
                loaded_dict = np.load(dict, allow_pickle=True)
         | 
| 144 | 
            -
                word_to_num = loaded_dict["word_to_num"].item()
         | 
| 145 | 
            -
                num_to_word = loaded_dict["num_to_word"].item()
         | 
| 146 | 
            -
                X = []
         | 
| 147 | 
            -
                Y = []
         | 
| 148 | 
            -
                for i in range(len(words)-1):
         | 
| 149 | 
            -
                    word = words[i]
         | 
| 150 | 
            -
                    next_word = words[i+1]
         | 
| 151 | 
            -
                    X.append(word_to_num[word])
         | 
| 152 | 
            -
                    Y.append(word_to_num[next_word])
         | 
| 153 | 
            -
                Y.append(0)
         | 
| 154 | 
            -
             | 
| 155 | 
            -
                X.append(word_to_num[words[-1]])
         | 
| 156 | 
            -
                X_train = pad_sequences([X])
         | 
| 157 | 
            -
                y_train = pad_sequences([Y])
         | 
| 158 |  | 
| 159 | 
            -
             | 
| 160 | 
            -
             | 
| 161 | 
            -
                chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)
         | 
| 162 | 
            -
                chatbot.load_weights(weights)
         | 
| 163 | 
            -
                chatbot.build(input_shape=(None, max_len)) # Build the model
         | 
| 164 | 
            -
                chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
         | 
| 165 | 
            -
                chatbot.fit(X_train, y_train, epochs=1, batch_size=64)
         | 
| 166 | 
            -
                for i in range(1):
         | 
| 167 | 
            -
                    other_text2 = text2
         | 
| 168 | 
            -
                    other_text2 = other_text2.lower()
         | 
| 169 | 
            -
                    other_words2 = other_text2.split()
         | 
| 170 | 
            -
                    other_num2 = [word_to_num[word] for word in other_words2]
         | 
| 171 | 
            -
                    given_X2 = other_num2
         | 
| 172 | 
            -
                    input_sequence2 = pad_sequences([given_X2], maxlen=max_len, padding='post')
         | 
| 173 | 
            -
                    output_sentence = other_text2 + ""
         | 
| 174 | 
            -
                    for _ in range(len2):
         | 
| 175 | 
            -
                        predicted_token = np.argmax(chatbot.predict(input_sequence2), axis=-1)
         | 
| 176 | 
            -
                        predicted_token = predicted_token.item()
         | 
| 177 | 
            -
                        out = num_to_word[predicted_token]
         | 
| 178 | 
            -
                        # if out == ".":
         | 
| 179 | 
            -
                            # break
         | 
| 180 | 
            -
             | 
| 181 | 
            -
                        output_sentence += " " + out
         | 
| 182 | 
            -
                        given_X2 = given_X2[1:]
         | 
| 183 | 
            -
                        given_X2.append(predicted_token)
         | 
| 184 | 
            -
                        input_sequence2 = pad_sequences([given_X2], maxlen=max_len, padding='post')
         | 
| 185 | 
            -
             | 
| 186 | 
            -
                    out2 = output_sentence  
         | 
| 187 | 
            -
                st.write("Predicted Text: ")
         | 
| 188 | 
            -
                st.write(out2)    
         | 
| 189 | 
             
            else:
         | 
| 190 | 
             
                out2 = "Error: Wrong Model Selected"
         | 
| 191 |  | 
|  | |
| 41 | 
             
                def create_padding_mask(self, seq):
         | 
| 42 | 
             
                    mask = tf.cast(tf.math.equal(seq, 0), tf.float32)
         | 
| 43 | 
             
                    return mask[:, tf.newaxis, tf.newaxis, :]
         | 
|  | |
| 44 | 
             
            st.title("UniGLM TEXT completion Model")
         | 
| 45 | 
             
            st.subheader("Next Word Prediction AI Model by Webraft-AI")
         | 
| 46 | 
             
            #Picking what NLP task you want to do
         | 
|  | |
| 125 | 
             
                    len2 = 1
         | 
| 126 | 
             
                else:
         | 
| 127 | 
             
                    len2 = 13
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 128 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 129 | 
             
            else:
         | 
| 130 | 
             
                out2 = "Error: Wrong Model Selected"
         | 
| 131 |  | 
