|
import gradio as gr |
|
import tensorflow as tf |
|
|
|
|
|
model = tf.keras.models.load_model('sentimentality.h5') |
|
|
|
|
|
def predict_sentiment(text): |
|
|
|
tokenizer = tf.keras.preprocessing.text.Tokenizer() |
|
tokenizer.fit_on_texts([text]) |
|
text = tokenizer.texts_to_sequences([text]) |
|
text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=500, padding='post', truncating='post') |
|
|
|
proba = model.predict(text)[0] |
|
|
|
proba /= proba.sum() |
|
|
|
return {"Positive": float(proba[0]), "Negative": float(proba[1]), "Neutral": float(proba[2])} |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_sentiment, |
|
inputs=gr.inputs.Textbox(label="Enter text here", lines=5, placeholder="Type here to analyze sentiment..."), |
|
outputs=gr.outputs.Label(label="Sentiment", default="Neutral", font_size=30) |
|
) |
|
|
|
|
|
classes = ["Positive", "Negative", "Neutral"] |
|
iface.outputs[0].choices = classes |
|
|
|
|
|
iface.launch() |
|
|