-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (54 loc) · 2.52 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
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing.text import tokenizer_from_json
import tensorflow.keras.utils as ku
import streamlit as st
import tensorflow as tf
import json
# Load the tokenizer from the JSON file
with open("tokenizer.json", "r", encoding="utf-8") as f:
tokenizer_json = json.loads(f.read())
tokenizer = tokenizer_from_json(tokenizer_json)
# Function to generate text based on input sequence
def generate_text(seed_text, num_next_words, model, max_sequence_length):
for num in range(num_next_words):
# Tokenize the input sequence
token_list = tokenizer.texts_to_sequences([seed_text])[0]
# Pad the sequence
token_list = pad_sequences([token_list], maxlen=max_sequence_length, padding='pre')
# Feed the preprocessed input data into the model to generate the next word
predicted_probs = model.predict(token_list)[0]
# Find the index of the word with the highest probability
predicted_index = np.argmax(predicted_probs)
# Find the word corresponding to the predicted index
output_word = ''
for word, index in tokenizer.word_index.items():
if index == predicted_index:
output_word = word
break
# Update the seed text (input taken + generated word --> to predict next word)
seed_text += " " + output_word
return seed_text.title()
# Streamlit app
def main():
st.title("Text Generation App")
# load the model
model = tf.keras.models.load_model('model.h5')
# Input sequence from the user
input_sequence = st.text_input("Enter the input sequence:")
# Number of words to generate
num_next_words = st.slider("Number of next words to generate:", min_value=1, max_value=10, value=5)
# Specifying the max sequence length
max_seq_len = 17
# Button to generate text
if st.button("Generate Text"):
# Generate text based on the input sequence
generated_text = generate_text(input_sequence, num_next_words, model, max_seq_len)
# Display the generated text
st.write("Generated Text:", generated_text)
if __name__ == "__main__":
main()