Sambhavnoobcoder's picture
created app.py
5328dbc
raw
history blame
1.24 kB
import gradio as gr
import tensorflow as tf
from tensorflow import keras
# Load the sentiment analysis model
model = keras.models.load_model("sentimentality.h5")
# Load the tokenizer and max length used during training
tokenizer = keras.preprocessing.text.tokenizer_from_json(open("tokenizer.json").read())
max_len = 100
def predict_sentiment(text):
# Preprocess the text
text = [text]
text = tf.keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences(text), maxlen=max_len)
# Make a prediction
prediction = model.predict(text)[0][0]
# Return the probabilities of each sentiment
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}%"
# Define the interface of the Gradio app
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.",
)
# Run the Gradio app
iface.launch()