-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
76 lines (63 loc) · 2.51 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#importing necessary libraries
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import load_model
import streamlit as st
from PIL import Image
#importing cover image
image = Image.open('NLP.png')
#loading the pre-trained weights and model architecture
model = tf.keras.models.load_model('textPredictorModel.h5')
#defining project title
st.title("Text Predictor!")
st.subheader("A Deep Learning Model that foretells text before you type it.")
st.image(image, caption='',use_column_width=True)
#dataset preprocessing
file = open("Goodwill.txt").read() #opeining the dataset and reading from it
tokenizer = Tokenizer() #tokenizing the dataset
data = file.lower().split("\n") #converting dataset to lowercase
#removing whitespaces from the dataset
corpus = []
for line in data:
a = line.strip()
corpus.append(a)
#generating tokens for each sentence in the data
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
print(tokenizer.word_index)
print(total_words)
#creating labels for each sentence in dataset
input_sequences = []
for line in corpus:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)
# pad sequences
max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre')
# create predictors and label
xs, labels = input_sequences[:,:-1],input_sequences[:,-1]
ys = tf.keras.utils.to_categorical(labels, num_classes=total_words)
num = st.slider("Number of text predictions?",0,10)
#generating next words given a seed
def next_word(seed):
seed_text = seed
next_words = num
for _ in range(next_words):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
predicted = model.predict_classes(token_list, verbose=0)
output_word = ""
for word, index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " " + output_word
st.subheader(seed_text)
#getting the output/predicted text
next_word(st.text_input('Enter seed sentence','I want to meet'))