import gradio as gr import numpy as np import pickle import tensorflow as tf from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.models import Sequential from tensorflow.keras.optimizers import Adam from tensorflow.keras.models import load_model model = load_model('./nextwords11.h5') tokenizer = pickle.load(open('./token11.pkl', 'rb')) def predict(text,nw): seed_text = text next_words = int(nw) for _ in range(next_words): token_list = tokenizer.texts_to_sequences([seed_text])[0] token_list = pad_sequences([token_list], maxlen=86, padding='pre') predict_x=model.predict(token_list, verbose=0) predicted=np.argmax(predict_x,axis=1) #predicted = model.predict_classes(token_list, verbose=0) output_word = "" for word, index in tokenizer.word_index.items(): if index == predicted: output_word = word break seed_text += " " + output_word return seed_text with gr.Blocks(css=".x {font-weight:bold}") as demo: text = gr.Textbox(label="Write Something :",elem_classes="x") nw = gr.inputs.Dropdown(choices=[1,2,3,4,5], label="Select no of words to be predicted") output = gr.Textbox(label="Output Box",elem_classes="x") predict_btn = gr.Button("Predict next Words !!",elem_classes="x") predict_btn.click(fn=predict, inputs=[text,nw], outputs=output) demo.launch()