WebraftAI/Text-Completion
0
1import streamlit as st2import tensorflow as tf3from keras.layers import Input, Dense, Embedding, MultiHeadAttention4from keras.layers import Dropout, LayerNormalization5from keras.models import Model6from keras.utils import pad_sequences7from keras import layers8import numpy as np9import logging10logging.getLogger('tensorflow').setLevel(logging.ERROR)11class TransformerChatbot(Model):12 def __init__(self, vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate):13 super(TransformerChatbot, self).__init__()14 self.embedding = Embedding(vocab_size, d_model)15 self.attention = MultiHeadAttention(num_heads=n_head, key_dim=d_model)16 self.norm1 = LayerNormalization(epsilon=1e-6)17 self.dropout1 = Dropout(dropout_rate)18 self.dense1 = Dense(ff_dim, activation="relu")19 self.dense2 = Dense(d_model)20 self.norm2 = LayerNormalization(epsilon=1e-6)21 self.dropout2 = Dropout(dropout_rate)22 self.flatten = tf.keras.layers.Flatten()23 self.fc = Dense(vocab_size, activation="softmax")24 self.max_len = max_len25 26 def call(self, inputs):27 x = self.embedding(inputs)28 # Masking29 mask = self.create_padding_mask(inputs)30 attn_output = self.attention(x, x, x, attention_mask=mask)31 x = x + attn_output32 x = self.norm1(x)33 x = self.dropout1(x)34 x = self.dense1(x)35 x = self.dense2(x)36 x = self.norm2(x)37 x = self.dropout2(x)38 x = self.fc(x)39 return x40 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, :]44st.title("UniGLM TEXT completion Model")45st.subheader("Next Word Prediction AI Model by Webraft-AI")46#Picking what NLP task you want to do47option = st.selectbox('Model',('13M','26M')) #option is stored in this variable48#Textbox for text user is entering49st.subheader("Enter a word from which a sentence / word would be predicted")50text2 = st.text_input('Enter word: ') #text is stored in this variable51 52if option == '13M':53 with open("data2.txt","r") as f:54 text = f.read()55 text = text.lower()56 words = text.split()57 loaded_dict = np.load("dict_predict3.bin.npz", allow_pickle=True)58 word_to_num = loaded_dict["word_to_num"].item()59 num_to_word = loaded_dict["num_to_word"].item()60 X = []61 Y = []62 63 for i in range(len(words)-1):64 word = words[i]65 next_word = words[i+1]66 X.append(word_to_num[word])67 Y.append(word_to_num[next_word])68 Y.append(0)69 70 X.append(word_to_num[words[-1]])71 72 X_train = pad_sequences([X])73 y_train = pad_sequences([Y])74 vocab_size = 10000075 max_len = 176 d_model = 64 # 64 , 102477 n_head = 4 # 8 , 1678 ff_dim = 256 # 256 , 204879 dropout_rate = 0.1 # 0.5 , 0.280 81 82 chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)83 chatbot.load_weights("predict3")84 chatbot.build(input_shape=(None, max_len)) # Build the model85 chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")86 87 for i in range(1):88 other_text1 = text289 other_text1 = other_text1.lower()90 other_words1 = other_text1.split()91 other_num1 = [word_to_num[word] for word in other_words1]92 given_X1 = other_num193 input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')94 output_sentence = other_text1+""95 for _ in range(13):96 predicted_token = np.argmax(chatbot.predict(input_sequence1), axis=-1)97 predicted_token = predicted_token.item()98 out = num_to_word[predicted_token]99 100 101 output_sentence += " " + out102 if out == ".":103 break104 given_X1 = given_X1[1:]105 given_X1.append(predicted_token)106 input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')107 108 out2 = output_sentence109 110 111else:112 with open("data2.txt","r") as f:113 text = f.read()114 text = text.lower()115 words = text.split()116 loaded_dict = np.load("dict_predict1.bin.npz", allow_pickle=True)117 word_to_num = loaded_dict["word_to_num"].item()118 num_to_word = loaded_dict["num_to_word"].item()119 X = []120 Y = []121 for i in range(len(words)-1):122 word = words[i]123 next_word = words[i+1]124 X.append(word_to_num[word])125 Y.append(word_to_num[next_word])126 Y.append(0)127 128 X.append(word_to_num[words[-1]])129 X_train = pad_sequences([X])130 y_train = pad_sequences([Y])131 vocab_size = 100000132 max_len = 1133 d_model = 128 # 64 , 1024134 n_head = 4 # 8 , 16135 ff_dim = 256 # 256 , 2048136 dropout_rate = 0.1 # 0.5 , 0.2137 138 139 chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)140 chatbot.load_weights("predict1")141 chatbot.build(input_shape=(None, max_len)) # Build the model142 chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")143 144 for i in range(1):145 other_text1 = text2146 other_text1 = other_text1.lower()147 other_words1 = other_text1.split()148 other_num1 = [word_to_num[word] for word in other_words1]149 given_X1 = other_num1150 input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')151 output_sentence = other_text1+""152 for _ in range(10):153 predicted_token = np.argmax(chatbot.predict(input_sequence1), axis=-1)154 predicted_token = predicted_token.item()155 out = num_to_word[predicted_token]156 157 158 output_sentence += " " + out159 if out == ".":160 break161 given_X1 = given_X1[1:]162 given_X1.append(predicted_token)163 input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')164 165 out2 = output_sentence166 167 168 169 170 171 172st.write("Predicted Text: ")173st.write(out2)