Skip to content

Instantly share code, notes, and snippets.

@nithyadurai87
Last active March 8, 2025 19:22
Show Gist options
  • Save nithyadurai87/9bbbc6cec404bd6f9a4b089ede332817 to your computer and use it in GitHub Desktop.
Save nithyadurai87/9bbbc6cec404bd6f9a4b089ede332817 to your computer and use it in GitHub Desktop.
07_Ilayaraja_book_prediction.py
from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle
tokens = pickle.load(open(r'/content/Ilayaraja_book_tokens.pkl', 'rb'))
model_file = pickle.load(open(r'/content/Ilayaraja_book_model.pkl', 'rb'))
model = model_from_json(model_file['model_json'])
model.set_weights(model_file['model_weights'])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
input_text = "சுருக்கமாகச்"
predict_next_words= 3
for _ in range(predict_next_words):
input_token = tokens.texts_to_sequences([input_text])[0]
input_x = pad_sequences([input_token], maxlen=max_line_len-1, padding='pre')
predicted = np.argmax(model.predict(input_x), axis=-1)
output_word = ""
for word, index in tokens.word_index.items():
if index == predicted:
output_word = word
break
input_text += " " + output_word
print(input_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment