-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprediction.py
35 lines (26 loc) · 1.07 KB
/
prediction.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
from pathlib import Path
from pickle import load
import numpy as np
from keras.models import load_model
from keras.preprocessing.sequence import pad_sequences
def generate_sequence(model, tokenizer, seq_length, seed_text, n_words):
result = list()
in_text = seed_text
for _ in range(n_words):
encoded = tokenizer.texts_to_sequences([in_text])[0]
encoded = pad_sequences([encoded], maxlen=seq_length, truncating='pre')
predict_x = model.predict(encoded)
yhat = np.argmax(predict_x, axis=1)
out_word = ''
for word, index in tokenizer.word_index.items():
if index == yhat:
out_word = word
break
in_text += ' ' + out_word
result.append(out_word)
return ' '.join(result)
def predict(text, sequence_length, model_path: Path):
model = load_model(model_path / 'model.h5')
tokenizer = load(open(model_path / 'tokenizer.pkl', 'rb'))
generated = generate_sequence(model, tokenizer, sequence_length, text, 50)
return {'text': text, 'generated': generated}