Spaces:
Build error
Build error
Upload 3 files
Browse files- dictionary.pkl +3 -0
- main.py +224 -0
- transformer_model.h5 +3 -0
dictionary.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1f9217dba8bf807ec5ca5202565b16d048cfe8ff43fe3deac31d57782120f3ad
|
| 3 |
+
size 616116
|
main.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import keras
|
| 2 |
+
from keras import layers
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import numpy as np
|
| 5 |
+
from nltk.tokenize import word_tokenize
|
| 6 |
+
from keras.models import load_model
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
def number_to_words(predictions, dictionary):
|
| 10 |
+
# Invert the dictionary
|
| 11 |
+
inverted_dictionary = {v: k for k, v in dictionary.items()}
|
| 12 |
+
|
| 13 |
+
predicted_sentences = []
|
| 14 |
+
|
| 15 |
+
for prediction_row in predictions:
|
| 16 |
+
words_row = []
|
| 17 |
+
for index in prediction_row:
|
| 18 |
+
# Check if the index exists in the inverted dictionary
|
| 19 |
+
word = inverted_dictionary.get(index)
|
| 20 |
+
if word is not None:
|
| 21 |
+
words_row.append(word)
|
| 22 |
+
predicted_sentence = ' '.join(words_row)
|
| 23 |
+
predicted_sentences.append(predicted_sentence)
|
| 24 |
+
|
| 25 |
+
return predicted_sentences
|
| 26 |
+
def return_order(dict_, content: str):
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
tokens = word_tokenize(content)
|
| 30 |
+
|
| 31 |
+
order = [dict_[token] for token in tokens if token in dict_]
|
| 32 |
+
return order
|
| 33 |
+
class MultiHeadSelfAttention(layers.Layer):
|
| 34 |
+
def __init__(self, embed_dim, num_heads):
|
| 35 |
+
super(MultiHeadSelfAttention, self).__init__()
|
| 36 |
+
self.embed_dim = embed_dim
|
| 37 |
+
self.num_heads = num_heads
|
| 38 |
+
if embed_dim % num_heads != 0:
|
| 39 |
+
raise ValueError(
|
| 40 |
+
f"embedding dimension = {embed_dim} should be divisible by number of heads = {num_heads}"
|
| 41 |
+
)
|
| 42 |
+
self.projection_dim = embed_dim // num_heads
|
| 43 |
+
self.query_dense = layers.Dense(embed_dim)
|
| 44 |
+
self.key_dense = layers.Dense(embed_dim)
|
| 45 |
+
self.value_dense = layers.Dense(embed_dim)
|
| 46 |
+
self.combine_heads = layers.Dense(embed_dim)
|
| 47 |
+
|
| 48 |
+
def attention(self, query, key, value):
|
| 49 |
+
score = tf.matmul(query, key, transpose_b=True)
|
| 50 |
+
dim_key = tf.cast(tf.shape(key)[-1], tf.float32)
|
| 51 |
+
scaled_score = score / tf.math.sqrt(dim_key)
|
| 52 |
+
|
| 53 |
+
# Apply mask to prevent attending to future tokens during decoding
|
| 54 |
+
mask = tf.linalg.band_part(tf.ones_like(scaled_score), -1, 0)
|
| 55 |
+
scaled_score -= 1e9 * (1 - mask)
|
| 56 |
+
|
| 57 |
+
weights = tf.nn.softmax(scaled_score, axis=-1)
|
| 58 |
+
output = tf.matmul(weights, value)
|
| 59 |
+
return output, weights
|
| 60 |
+
def separate_heads(self, x, batch_size):
|
| 61 |
+
x = tf.reshape(x, (batch_size, -1, self.num_heads, self.projection_dim))
|
| 62 |
+
return tf.transpose(x, perm=[0, 2, 1, 3])
|
| 63 |
+
|
| 64 |
+
def call(self, inputs, mask=None, training=None):
|
| 65 |
+
batch_size = tf.shape(inputs)[0]
|
| 66 |
+
query = self.query_dense(inputs)
|
| 67 |
+
key = self.key_dense(inputs)
|
| 68 |
+
value = self.value_dense(inputs)
|
| 69 |
+
query = self.separate_heads(query, batch_size)
|
| 70 |
+
key = self.separate_heads(key, batch_size)
|
| 71 |
+
value = self.separate_heads(value, batch_size)
|
| 72 |
+
attention, weights = self.attention(query, key, value)
|
| 73 |
+
attention = tf.transpose(attention, perm=[0, 2, 1, 3])
|
| 74 |
+
concat_attention = tf.reshape(attention, (batch_size, -1, self.embed_dim))
|
| 75 |
+
output = self.combine_heads(concat_attention)
|
| 76 |
+
|
| 77 |
+
return output
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class TransformerBlock(layers.Layer):
|
| 81 |
+
def __init__(self, embed_dim, num_heads, ff_dim, rate=0.1, **kwargs):
|
| 82 |
+
super(TransformerBlock, self).__init__(**kwargs)
|
| 83 |
+
self.att = layers.MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim)
|
| 84 |
+
self.ffn = keras.Sequential(
|
| 85 |
+
[layers.Dense(ff_dim, activation="relu"), layers.Dense(embed_dim)]
|
| 86 |
+
)
|
| 87 |
+
self.layernorm1 = layers.LayerNormalization(epsilon=1e-6)
|
| 88 |
+
self.layernorm2 = layers.LayerNormalization(epsilon=1e-6)
|
| 89 |
+
self.dropout1 = layers.Dropout(rate)
|
| 90 |
+
self.dropout2 = layers.Dropout(rate)
|
| 91 |
+
self.embed_dim = embed_dim
|
| 92 |
+
self.num_heads = num_heads
|
| 93 |
+
self.ff_dim = ff_dim
|
| 94 |
+
self.rate = rate
|
| 95 |
+
|
| 96 |
+
def call(self, inputs):
|
| 97 |
+
attn_output = self.att(inputs, inputs)
|
| 98 |
+
attn_output = self.dropout1(attn_output, training=True)
|
| 99 |
+
out1 = self.layernorm1(inputs + attn_output)
|
| 100 |
+
ffn_output = self.ffn(out1)
|
| 101 |
+
ffn_output = self.dropout2(ffn_output, training=True)
|
| 102 |
+
return self.layernorm2(out1 + ffn_output)
|
| 103 |
+
|
| 104 |
+
def get_config(self):
|
| 105 |
+
config = super().get_config()
|
| 106 |
+
config.update({
|
| 107 |
+
'embed_dim': self.embed_dim,
|
| 108 |
+
'num_heads': self.num_heads,
|
| 109 |
+
'ff_dim': self.ff_dim,
|
| 110 |
+
'rate': self.rate,
|
| 111 |
+
})
|
| 112 |
+
return config
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class TokenAndPositionEmbedding(keras.layers.Layer):
|
| 117 |
+
def __init__(self, maxlen, vocab_size, embed_dim, **kwargs):
|
| 118 |
+
super(TokenAndPositionEmbedding, self).__init__(**kwargs)
|
| 119 |
+
self.maxlen = maxlen
|
| 120 |
+
self.vocab_size = vocab_size
|
| 121 |
+
self.embed_dim = embed_dim
|
| 122 |
+
|
| 123 |
+
def build(self, input_shape):
|
| 124 |
+
self.token_emb = keras.layers.Embedding(input_dim=self.vocab_size, output_dim=self.embed_dim)
|
| 125 |
+
self.pos_emb = keras.layers.Embedding(input_dim=self.maxlen, output_dim=self.embed_dim)
|
| 126 |
+
super().build(input_shape)
|
| 127 |
+
|
| 128 |
+
def call(self, x):
|
| 129 |
+
maxlen = tf.shape(x)[-1]
|
| 130 |
+
positions = tf.range(start=0, limit=maxlen, delta=1)
|
| 131 |
+
positions = self.pos_emb(positions)
|
| 132 |
+
x = self.token_emb(x)
|
| 133 |
+
return x + positions
|
| 134 |
+
|
| 135 |
+
def compute_output_shape(self, input_shape):
|
| 136 |
+
return input_shape[0], input_shape[1], self.embed_dim
|
| 137 |
+
|
| 138 |
+
def get_config(self):
|
| 139 |
+
config = super().get_config()
|
| 140 |
+
config.update({
|
| 141 |
+
'maxlen': self.maxlen,
|
| 142 |
+
'vocab_size': self.vocab_size,
|
| 143 |
+
'embed_dim': self.embed_dim,
|
| 144 |
+
})
|
| 145 |
+
return config
|
| 146 |
+
def build_transformer_model(maxlen, vocab_size, embed_dim, num_heads, ff_dim, num_blocks, dropout_rate, num_encoders, num_decoders):
|
| 147 |
+
inputs = layers.Input(shape=(maxlen,))
|
| 148 |
+
embedding_layer = TokenAndPositionEmbedding(maxlen, vocab_size, embed_dim)(inputs)
|
| 149 |
+
encoders_outputs = []
|
| 150 |
+
x = embedding_layer
|
| 151 |
+
for _ in range(int(num_encoders)):
|
| 152 |
+
encoder_output = TransformerBlock(embed_dim, num_heads, ff_dim, dropout_rate)(x)
|
| 153 |
+
encoders_outputs.append(encoder_output)
|
| 154 |
+
|
| 155 |
+
decoder_inputs = layers.Input(shape=(maxlen,))
|
| 156 |
+
y = TokenAndPositionEmbedding(maxlen, vocab_size, embed_dim)(decoder_inputs)
|
| 157 |
+
for _ in range(int(num_decoders)):
|
| 158 |
+
for encoder_output in encoders_outputs:
|
| 159 |
+
y = TransformerBlock(embed_dim, num_heads, ff_dim, dropout_rate)(y)
|
| 160 |
+
outputs = layers.Dense(vocab_size, activation="softmax")(y)
|
| 161 |
+
|
| 162 |
+
model = keras.Model(inputs=[inputs, decoder_inputs], outputs=outputs)
|
| 163 |
+
return model
|
| 164 |
+
|
| 165 |
+
def query_gen_sentences(query, model, dictionary, maxlen):
|
| 166 |
+
# Convert the query to the order of words based on the provided dictionary
|
| 167 |
+
query_order = return_order(dict_=dictionary, content=query)
|
| 168 |
+
u_order = np.array(query_order)
|
| 169 |
+
|
| 170 |
+
# Pad the order to match the maximum length
|
| 171 |
+
padding_length = max(0, maxlen - len(u_order))
|
| 172 |
+
padded_u_order = np.pad(u_order, (0, padding_length), mode='constant', constant_values=0)
|
| 173 |
+
padded_u_order = np.reshape(padded_u_order, (1, -1))
|
| 174 |
+
|
| 175 |
+
# Generate predictions using the model
|
| 176 |
+
# Assuming x_data_1 and x_data_2 are your input data tensors
|
| 177 |
+
predictions = model.predict([padded_u_order, padded_u_order])
|
| 178 |
+
predicted_classes = np.argmax(predictions, axis=-1)
|
| 179 |
+
|
| 180 |
+
# Convert predicted classes to words using the provided dictionary
|
| 181 |
+
words = number_to_words(predictions=predicted_classes, dictionary=dictionary)
|
| 182 |
+
|
| 183 |
+
return words
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
custom_objects = {
|
| 187 |
+
'MultiHeadSelfAttention': MultiHeadSelfAttention,
|
| 188 |
+
'TransformerBlock': TransformerBlock,
|
| 189 |
+
'TokenAndPositionEmbedding': TokenAndPositionEmbedding
|
| 190 |
+
}
|
| 191 |
+
loaded_model = load_model('transformer_model.h5', custom_objects=custom_objects)
|
| 192 |
+
with open('dictionary.pkl', 'rb') as f:
|
| 193 |
+
loaded_dictionary = pickle.load(f)
|
| 194 |
+
|
| 195 |
+
maxlen=15
|
| 196 |
+
def generate_text(s1,tokens:int):
|
| 197 |
+
respose=''
|
| 198 |
+
words = query_gen_sentences(query=s1,
|
| 199 |
+
model=loaded_model, dictionary=loaded_dictionary, maxlen=maxlen)
|
| 200 |
+
w_=words[0].split(' ')
|
| 201 |
+
respose+=' '+w_[-1]+' '
|
| 202 |
+
for i in range(tokens):
|
| 203 |
+
w1 = query_gen_sentences(query=words[-1]
|
| 204 |
+
,
|
| 205 |
+
model=loaded_model, dictionary=loaded_dictionary, maxlen=maxlen)
|
| 206 |
+
words.append(w1[0])
|
| 207 |
+
w_ = w1[0].split(' ')
|
| 208 |
+
respose += ' '+w_[-1]+' '
|
| 209 |
+
|
| 210 |
+
return respose
|
| 211 |
+
|
| 212 |
+
def gen(input,num_of_token):
|
| 213 |
+
o=generate_text(s1=input,tokens=num_of_token)
|
| 214 |
+
return o
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
# while True:
|
| 218 |
+
# i=input("Enter : ")
|
| 219 |
+
# # remember the number of words in ur prompt should be less that 15
|
| 220 |
+
# o=generate_text(s1=i,tokens=10)
|
| 221 |
+
# print(o)
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
# Sample prompt- Harry walked through the dark corridors of Hogwarts, his wand lit with a faint
|
transformer_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:287ef916edfa240a118c9ae62169b2e7fe283b9ceb344dfa181826a8edb714de
|
| 3 |
+
size 213188256
|