oleksiikondus's picture
Update app.py
1af56d0
import tensorflow
from tensorflow.keras.datasets import imdb
from tensorflow import keras
import gradio as gr
import numpy as np
rnn = keras.models.load_model('model.h5')
words_per_review = 200
word_to_index = imdb.get_word_index()
# Створення функції для оцінки коментаря
def predict_comment_score(comment):
class_names = ["Negative", "Positive"]
words = comment.split()
print(len(words))
indexes = np.zeros(words_per_review).astype(int)
indexes[words_per_review -len(words) - 1] = 1
for i, word in enumerate(words):
indexes[words_per_review -len(words) + i] = word_to_index.get(word, 0) + 3
indexes = np.expand_dims(indexes, axis=0)
predictions = rnn.predict(indexes)
prediction = { }
prediction["Negative"] = float(np.round(1 - predictions[0], 3))
prediction["Positive"] = float(np.round(predictions[0], 3))
return prediction
demo = gr.Blocks()
# Створення інтерфейсу Gradio
with demo:
with gr.Tab("Predict comment score"):
image_input = gr.TextArea(label="Enter a comment")
output = gr.Label(label="Comment score")
image_button = gr.Button("Predict")
image_button.click(predict_comment_score, inputs=image_input, outputs=output)
demo.launch()