Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,44 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
| 4 |
|
|
|
|
| 5 |
# Load model & tokenizer
|
|
|
|
| 6 |
@st.cache_resource
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
text = st.text_input("Enter your sentence:"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import pickle
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# ----------------------
|
| 7 |
# Load model & tokenizer
|
| 8 |
+
# ----------------------
|
| 9 |
@st.cache_resource
|
| 10 |
+
def load_assets():
|
| 11 |
+
model = tf.keras.models.load_model("nextword_model.h5") # your saved model
|
| 12 |
+
with open("tokenizer.pkl", "rb") as f: # your saved tokenizer
|
| 13 |
+
tokenizer = pickle.load(f)
|
| 14 |
+
return model, tokenizer
|
| 15 |
|
| 16 |
+
model, tokenizer = load_assets()
|
| 17 |
|
| 18 |
+
# ----------------------
|
| 19 |
+
# Streamlit UI
|
| 20 |
+
# ----------------------
|
| 21 |
+
st.title("📝 LSTM Next Word Prediction")
|
| 22 |
+
st.write("Type a sentence and let the LSTM suggest the next word!")
|
| 23 |
|
| 24 |
+
# Input text
|
| 25 |
+
text = st.text_input("Enter your sentence:")
|
| 26 |
|
| 27 |
+
def predict_next_word(model, tokenizer, text, max_len=20):
|
| 28 |
+
"""Predict next word from input text using trained LSTM model."""
|
| 29 |
+
seq = tokenizer.texts_to_sequences([text])[0]
|
| 30 |
+
seq = tf.keras.preprocessing.sequence.pad_sequences([seq], maxlen=max_len-1, padding='pre')
|
| 31 |
+
|
| 32 |
+
preds = model.predict(seq, verbose=0)[0]
|
| 33 |
+
next_index = np.argmax(preds)
|
| 34 |
+
for word, index in tokenizer.word_index.items():
|
| 35 |
+
if index == next_index:
|
| 36 |
+
return word
|
| 37 |
+
return None
|
| 38 |
|
| 39 |
+
if st.button("Predict Next Word") and text:
|
| 40 |
+
predicted_word = predict_next_word(model, tokenizer, text)
|
| 41 |
+
if predicted_word:
|
| 42 |
+
st.success(f"**Predicted next word:** {predicted_word}")
|
| 43 |
+
else:
|
| 44 |
+
st.warning("Could not predict a word. Try another input.")
|