Ridaren's picture
Update app.py
99ff7e4
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras.datasets import imdb
import numpy as np
import gradio as gr
number_of_words = 3000
words_per_view = 200
loaded_model = tf.keras.models.load_model('sentiment_analysis.h5')
word_to_index = imdb.get_word_index()
def get_predict(userInputString, model):
words = userInputString.split()
#print(len(words))
encoded_word = np.zeros(words_per_view).astype(int)
encoded_word[words_per_view -len(words) - 1] = 1
for i, word in enumerate(words):
index = words_per_view - len(words) + i
encoded_word[index] = word_to_index.get(word, 0) + 3
encoded_word = np.expand_dims(encoded_word, axis=0)
prediction = model.predict(encoded_word)
return prediction
def analyze_sentiment(userInputString):
result = get_predict(userInputString, loaded_model)[0][0]
if result > 0.5:
answer = 'positive review'
else: answer = 'negative review'
return answer
UserInputPage = gr.Interface(
fn=analyze_sentiment,
inputs = ["text"],
outputs=["text"]
)
tabbed_Interface = gr.TabbedInterface([UserInputPage], ["Check user input"])
tabbed_Interface.launch()