-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
34 lines (29 loc) · 1.38 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
import streamlit as st
import numpy as np
import pickle
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
#Load the LSTM Model
model=load_model('next_word_gru.h5')
#3 Laod the tokenizer
with open('tokenizer.pickle','rb') as handle:
tokenizer=pickle.load(handle)
# Function to predict the next word
def predict_next_word(model, tokenizer, text, max_sequence_len):
token_list = tokenizer.texts_to_sequences([text])[0]
if len(token_list) >= max_sequence_len:
token_list = token_list[-(max_sequence_len):] # Ensure the sequence length matches max_sequence_len
token_list = pad_sequences([token_list], maxlen=max_sequence_len, padding='pre')
predicted = model.predict(token_list, verbose=0)
predicted_word_index = np.argmax(predicted, axis=1)
for word, index in tokenizer.word_index.items():
if index == predicted_word_index:
return word
return None
# streamlit app
st.title("Next Word Prediction With GRU RNN")
input_text=st.text_input("Enter the sequence of Words","To be or not to")
if st.button("Predict Next Word"):
max_sequence_len = model.input_shape[1] # Retrieve the max sequence length from the model input shape
next_word = predict_next_word(model, tokenizer, input_text, max_sequence_len)
st.write(f'Next word: {next_word}')