Spaces:
Sleeping
Sleeping
File size: 1,289 Bytes
93f8a8a 7556992 124ab99 e9c8039 7556992 124ab99 7556992 124ab99 7556992 1af56d0 |
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 37 |
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() |