Sambhavnoobcoder's picture
fn9
2e71df7
raw
history blame
1.15 kB
import gradio as gr
import tensorflow as tf
import numpy as np
# Load the pre-trained model
model = tf.keras.models.load_model('sentimentality.h5')
# Define a function to preprocess the text input
def preprocess(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')
return text
# Define a function to make a prediction on the input text
def predict_sentiment(text):
# Preprocess the text
text = preprocess(text)
# Make a prediction using the loaded model
proba = model.predict(text)[0]
# Normalize the probabilities
proba /= proba.sum()
# Return the probability distribution
return {"Positive": float(proba[0]), "Negative": float(proba[1]), "Neutral": float(proba[2])}
# Create a Gradio interface
iface = gr.Interface(
fn=predict_sentiment,
inputs=gr.inputs.Textbox(label="Enter text here"),
outputs=gr.outputs.Label(label="Sentiment", default="Neutral")
)
# Launch the interface
iface.launch()