-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunmodel.py
More file actions
61 lines (42 loc) · 1.91 KB
/
Copy pathrunmodel.py
File metadata and controls
61 lines (42 loc) · 1.91 KB
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
from tensorflow import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import os
import numpy as np
import pickle
absolute_path = os.path.dirname(__file__)
relative_path_model = "textsyn_model_new.h5"
full_path_model = os.path.join(absolute_path, relative_path_model)
relative_path_tokenizer= "mysimpletokenizer"
full_path_tokenizer = os.path.join(absolute_path, relative_path_tokenizer)
def generate_text(model,tokenizer):
seed_text=str(input("Enter the first few words [ max = 10 ] : "))
n=5
seed_text = seed_text.split(' ')
tokenizer.fit_on_texts(seed_text)
sequences=tokenizer.texts_to_sequences(seed_text)
sequences = [s for s in sequences if len(s) > 0] # remove empty sequences
feature=pad_sequences([sequences], padding='post', maxlen=10)
feature=feature.reshape(-1,)
generated_text=np.array([])
for i in range(n):
x_pred=np.reshape(feature,(1,10))
preds=model.predict(x_pred,verbose=0)[0]
next_word_index=np.argmax(preds)
next_word=tokenizer.index_word[next_word_index]
generated_text=np.concatenate([generated_text,[next_word]])
feature=np.concatenate([feature[1:],[next_word_index]],axis=0)
print('Next few words: ',' '.join(generated_text))
mymodel = keras.models.load_model(full_path_model)
mytokenizer = pickle.load(open(full_path_tokenizer, 'rb'))
while True:
print("------------------------------------------------------------------------------------------------------------------------ ")
ans=str(input("Make a prediction? Y/N : "))
if ans.lower()=='y':
generate_text(model=mymodel,tokenizer=mytokenizer)
elif ans.lower()=='n':
break
else:
print("Please enter either Y or N!")
continue
print("------------------------------------------------------------------------------------------------------------------------ ")