Spaces:
Paused
Paused
DHRUV SHEKHAWAT
commited on
Commit
·
5ba996d
1
Parent(s):
496f3b5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from keras.layers import Input, Dense, Embedding, MultiHeadAttention
|
4 |
+
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__()
|
12 |
+
self.embedding = Embedding(vocab_size, d_model)
|
13 |
+
self.attention = MultiHeadAttention(num_heads=n_head, key_dim=d_model)
|
14 |
+
self.norm1 = LayerNormalization(epsilon=1e-6)
|
15 |
+
self.dropout1 = Dropout(dropout_rate)
|
16 |
+
self.dense1 = Dense(ff_dim, activation="relu")
|
17 |
+
self.dense2 = Dense(d_model)
|
18 |
+
self.norm2 = LayerNormalization(epsilon=1e-6)
|
19 |
+
self.dropout2 = Dropout(dropout_rate)
|
20 |
+
self.flatten = tf.keras.layers.Flatten()
|
21 |
+
self.fc = Dense(vocab_size, activation="softmax")
|
22 |
+
self.max_len = max_len
|
23 |
+
|
24 |
+
def call(self, inputs):
|
25 |
+
x = self.embedding(inputs)
|
26 |
+
# Masking
|
27 |
+
mask = self.create_padding_mask(inputs)
|
28 |
+
attn_output = self.attention(x, x, x, attention_mask=mask)
|
29 |
+
x = x + attn_output
|
30 |
+
x = self.norm1(x)
|
31 |
+
x = self.dropout1(x)
|
32 |
+
x = self.dense1(x)
|
33 |
+
x = self.dense2(x)
|
34 |
+
x = self.norm2(x)
|
35 |
+
x = self.dropout2(x)
|
36 |
+
x = self.fc(x)
|
37 |
+
return x
|
38 |
+
|
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 |
+
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',('12M Param')) #option is stored in this variable
|
46 |
+
#Textbox for text user is entering
|
47 |
+
st.subheader("Enter the text you'd like to analyze.")
|
48 |
+
text = st.text_input('Enter word: ') #text is stored in this variable
|
49 |
+
|
50 |
+
if option == '12M Param':
|
51 |
+
loaded_dict = np.load("dict_predict3.bin.npz", allow_pickle=True)
|
52 |
+
word_to_num = loaded_dict["word_to_num"].item()
|
53 |
+
num_to_word = loaded_dict["num_to_word"].item()
|
54 |
+
X = loaded_dict["X"].item()
|
55 |
+
Y = loaded_dict["Y"].item()
|
56 |
+
X_train = pad_sequences([X])
|
57 |
+
y_train = pad_sequences([Y])
|
58 |
+
vocab_size = 100000
|
59 |
+
max_len = 1
|
60 |
+
d_model = 64 # 64 , 1024
|
61 |
+
n_head = 4 # 8 , 16
|
62 |
+
ff_dim = 256 # 256 , 2048
|
63 |
+
dropout_rate = 0.1 # 0.5 , 0.2
|
64 |
+
|
65 |
+
|
66 |
+
chatbot = TransformerChatbot(vocab_size, max_len, d_model, n_head, ff_dim, dropout_rate)
|
67 |
+
chatbot.load_weights("predict3")
|
68 |
+
chatbot.build(input_shape=(None, max_len)) # Build the model
|
69 |
+
chatbot.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
|
70 |
+
other_text1 = text
|
71 |
+
for i in range(1):
|
72 |
+
|
73 |
+
other_text1 = other_text1.lower()
|
74 |
+
other_words1 = other_text1.split()
|
75 |
+
if len(other_words1) > 1:
|
76 |
+
st.write("Error: Found more than 1 word . There should not be more than one word in the prompt ")
|
77 |
+
for word in other_words1:
|
78 |
+
if word not in word_to_num:
|
79 |
+
st.write("Error: The word ` ",word," ` doesn't exist in the vocabulary and hence the model wasn't train on that. ")
|
80 |
+
else:
|
81 |
+
other_num1 = word_to_num[word]
|
82 |
+
|
83 |
+
given_X1 = other_num1
|
84 |
+
input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
|
85 |
+
output_sentence = other_text1 + ""
|
86 |
+
for _ in range(16):
|
87 |
+
predicted_token = np.argmax(chatbot.predict(input_sequence1), axis=-1)
|
88 |
+
predicted_token = predicted_token.item()
|
89 |
+
out = num_to_word[predicted_token]
|
90 |
+
|
91 |
+
|
92 |
+
output_sentence += " " + out
|
93 |
+
if out == ".":
|
94 |
+
break
|
95 |
+
given_X1 = given_X1[1:]
|
96 |
+
given_X1.append(predicted_token)
|
97 |
+
input_sequence1 = pad_sequences([given_X1], maxlen=max_len, padding='post')
|
98 |
+
out = output_sentence
|
99 |
+
|
100 |
+
|
101 |
+
else:
|
102 |
+
out = "Wrong Model"
|
103 |
+
|
104 |
+
st.write("Predicted Text: ")
|
105 |
+
st.write(out)
|