Spaces:
Runtime error
Runtime error
import gradio as gr | |
import numpy as np | |
from tensorflow import keras | |
import tensorflow as tf | |
import pickle | |
from tensorflow.keras.models import load_model | |
from tensorflow.keras.preprocessing.sequence import pad_sequences | |
pickle_open = open("tokenizer.pkl","rb") | |
tokenizer = pickle.load(pickle_open) | |
model = load_model("model.h5") | |
pickle_open.close() | |
def predict(text): | |
example = tokenizer.texts_to_sequences([text]) | |
example = pad_sequences(example, maxlen=2) | |
prediction = model.predict(np.array(example)) | |
predictions = [] | |
sorted_ = np.sort(prediction[0])[::-1] | |
for i in sorted_[:5]: | |
predictions.append(np.where(prediction[0] == i)[0]) | |
predicted_words = [] | |
reverse_word_map = dict(map(reversed, tokenizer.word_index.items())) | |
for i in predictions: | |
predicted_words.append(reverse_word_map[i[0]]) | |
return predicted_words | |
input_text = gr.inputs.Textbox(lines=1, placeholder="Enter sentence or word here...") | |
output_text = gr.outputs.Textbox() | |
title = "Next Word Prediction" | |
description = "Enter some text or sentence or word and get the next 5 possible autocompletion words" | |
gr.Interface(fn=predict, inputs=input_text, outputs=output_text, title=title, description=description).launch() | |