Prafuld3 commited on
Commit
40b367b
·
verified ·
1 Parent(s): afd97d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -44
app.py CHANGED
@@ -1,44 +1,44 @@
1
- import streamlit as st
2
- from keras.models import load_model
3
- from tensorflow.keras.preprocessing.sequence import pad_sequences
4
- import pickle
5
- import numpy as np
6
-
7
- max_sequence_len = 40
8
-
9
- st.title("Next Word Prediction using LSTM")
10
- seed_text = st.text_input('Enter initial text to start generating next words')
11
- next_words_count = st.slider('How many words to generate', 1, 8, 4)
12
- model = load_model('text_generator.h5')
13
-
14
- with open("tokenizer.pickle", "rb") as handle:
15
- tokenizer = pickle.read(handle)
16
-
17
- def generate_text(seed_text, next_words, model, max_sequence_len):
18
-
19
- for _ in range(next_words):
20
- token_list = tokenizer.texts_to_sequences([seed_text])[0]
21
-
22
- token_list = pad_sequences([token_list], maxlen = max_sequence_len - 1, padding='pre')
23
-
24
- predicted = model.predict(token_list, verbose=0)
25
-
26
- classes_x = np.argmax(predicted,axis=1)
27
-
28
- output_word = ""
29
- for word,index in tokenizer.word_index.items():
30
- if index == classes_x:
31
- output_word = word
32
- break
33
- seed_text += " "+output_word
34
- return seed_text.title()
35
-
36
- if st.button("Submit", type="primary"):
37
- output = generate_text(seed_text, next_words_count,model,max_sequence_len)
38
- st.write(output)
39
-
40
-
41
-
42
-
43
-
44
-
 
1
+ import streamlit as st
2
+ from keras.models import load_model
3
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
4
+ import pickle
5
+ import numpy as np
6
+
7
+ max_sequence_len = 40
8
+
9
+ st.title("Next Word Prediction using LSTM")
10
+ seed_text = st.text_input('Enter initial text to start generating next words')
11
+ next_words_count = st.slider('How many words to generate', 1, 8, 4)
12
+ model = load_model('text_generator.h5')
13
+
14
+ with open("tokenizer.pickle", "rb") as handle:
15
+ tokenizer = pickle.load(handle)
16
+
17
+ def generate_text(seed_text, next_words, model, max_sequence_len):
18
+
19
+ for _ in range(next_words):
20
+ token_list = tokenizer.texts_to_sequences([seed_text])[0]
21
+
22
+ token_list = pad_sequences([token_list], maxlen = max_sequence_len - 1, padding='pre')
23
+
24
+ predicted = model.predict(token_list, verbose=0)
25
+
26
+ classes_x = np.argmax(predicted,axis=1)
27
+
28
+ output_word = ""
29
+ for word,index in tokenizer.word_index.items():
30
+ if index == classes_x:
31
+ output_word = word
32
+ break
33
+ seed_text += " "+output_word
34
+ return seed_text.title()
35
+
36
+ if st.button("Submit", type="primary"):
37
+ output = generate_text(seed_text, next_words_count,model,max_sequence_len)
38
+ st.write(output)
39
+
40
+
41
+
42
+
43
+
44
+