larrysim commited on
Commit
92d79f6
·
verified ·
1 Parent(s): aa160cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -19
app.py CHANGED
@@ -1,28 +1,44 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import torch
 
4
 
 
5
  # Load model & tokenizer
 
6
  @st.cache_resource
7
- def load_model():
8
- tokenizer = AutoTokenizer.from_pretrained(".")
9
- model = AutoModelForCausalLM.from_pretrained(".")
10
- return tokenizer, model
 
11
 
12
- tokenizer, model = load_model()
13
 
14
- st.title("📝 Next Word Prediction App")
15
- st.write("Type a sentence and let the model suggest the next word!")
 
 
 
16
 
17
- # User input
18
- text = st.text_input("Enter your sentence:", "")
19
 
20
- if st.button("Predict Next Word") and text:
21
- inputs = tokenizer(text, return_tensors="pt")
22
- with torch.no_grad():
23
- outputs = model.generate(**inputs, max_new_tokens=1)
24
- prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
25
 
26
- # Extract only the new part
27
- predicted_next = prediction[len(text):].strip()
28
- st.success(f"**Predicted next word:** {predicted_next}")
 
 
 
 
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.")