File size: 1,232 Bytes
ee27e80 5e08480 1814585 ee27e80 b570369 1814585 5e08480 2d0fc9b ee27e80 928098e ee27e80 928098e ee27e80 |
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 |
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()
# Створення інтерфейсу Gradio
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) |