File size: 1,239 Bytes
bbb8444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
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(t):
    example = tokenizer.texts_to_sequences([t])
    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()