create a simple python app to inference with an h5 model using its tokenizer.pickle and…
- User
create a simple python app to inference with an h5 model using its tokenizer.pickle and a text input, this is a Language model, the user should specify the sequence length and the app should output the predicted text by the model
- Assistant
Sure! Here's an example of a simple Python app that uses an H5 model and its tokenizer.pickle file to generate text based on a given input sequence length:
python - User
I get this: Traceback (most recent call last): File "c:\Users\Dell-PC\Desktop\Projets\Model-Creator\chat-A.py", line 37, in
generated_text = generate_text(model, tokenizer, seq_length, input_text) File "c:\Users\Dell-PC\Desktop\Projets\Model-Creator\chat-A.py", line 19, in generate_text predicted_word_index = model.predict_classes(input_sequence, verbose=0) AttributeError: 'Sequential' object has no attribute 'predict_classes'. Did you mean: 'predict_step'? - Assistant
Apologies for the confusion. The
predict_classes()method has been removed in recent versions of TensorFlow. You can modify the code to use thepredict()method instead and then find the index of the predicted word.Here's an updated version of the
generate_text()function that should work correctly:pythondef generate_text(model, tokenizer, seq_length, seed_text, max_words=100): generated_text = seed_text for _ in range(max_words): input_sequence = preprocess_text(generated_text, tokenizer, seq_length) predicted_word_probs = model.predict(input_sequence, verbose=0)[0] predicted_word_index = tf.argmax(predicted_word_probs, axis=-1).numpy() predicted_word = tokenizer.index_word[predicted_word_index] generated_text += ' ' + predicted_word if predicted_word == '.': break return generated_text