|
import numpy as np |
|
import tensorflow as tf |
|
import gradio as gr |
|
from tensorflow import keras |
|
from tensorflow.keras.preprocessing.sequence import pad_sequences |
|
import json |
|
from tensorflow.keras.preprocessing.text import tokenizer_from_json |
|
|
|
model_cnn = keras.models.load_model('model_cnn.h5') |
|
|
|
with open('tokenizer_cnn.json') as f: |
|
data = json.load(f) |
|
tokenizer = tokenizer_from_json(json.dumps(data)) |
|
|
|
words_per_review = 200 |
|
def predict_comment_score(comment): |
|
sequence = tokenizer.texts_to_sequences([comment]) |
|
padded_sequence = pad_sequences(sequence, padding='post', maxlen=words_per_review) |
|
prediction = model_cnn.predict(padded_sequence) |
|
predictions = { } |
|
predictions["Negative"] = float(np.round(1 - prediction[0], 3)) |
|
predictions["Positive"] = float(np.round(prediction[0], 3)) |
|
return predictions |
|
|
|
demo = gr.Blocks() |
|
|
|
|
|
with demo: |
|
with gr.Tab("Predict comment Sentiment"): |
|
comment_input = gr.TextArea(label="Enter a comment") |
|
output = gr.Label(label="Comment sentiment") |
|
predict_button = gr.Button("Predict") |
|
predict_button.click(predict_comment_score, inputs=comment_input, outputs=output) |
|
|
|
demo.launch(debug=True) |