|
import gradio as gr |
|
import tensorflow as tf |
|
from tensorflow import keras |
|
|
|
|
|
model = keras.models.load_model("sentimentality.h5") |
|
|
|
|
|
tokenizer = keras.preprocessing.text.tokenizer_from_json(open("tokenizer.json").read()) |
|
max_len = 100 |
|
|
|
def predict_sentiment(text): |
|
|
|
text = [text] |
|
text = tf.keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences(text), maxlen=max_len) |
|
|
|
|
|
prediction = model.predict(text)[0][0] |
|
|
|
|
|
positive_prob = round(prediction * 100, 2) |
|
negative_prob = round((1 - prediction) * 100, 2) |
|
neutral_prob = 100 - positive_prob - negative_prob |
|
|
|
return f"Positive: {positive_prob}%\nNegative: {negative_prob}%\nNeutral: {neutral_prob}%" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_sentiment, |
|
inputs=gr.inputs.Textbox(label="Enter text here:"), |
|
outputs=gr.outputs.Textbox(label="Sentiment probabilities:"), |
|
title="Sentiment Analysis", |
|
description="Enter some text and get the probabilities of the sentiment.", |
|
) |
|
|
|
|
|
iface.launch() |
|
|