Sambhavnoobcoder's picture
(old code) X (new code)
72cc2a3
raw
history blame
1.25 kB
import gradio as gr
import tensorflow as tf
# Load the saved model
model = tf.keras.models.load_model("sentimentality.h5")
def preprocess(text):
# Tokenize the text into a list of words
words = text.strip().lower().split()
# Load the vocabulary
with open('vocabulary.txt', 'r') as f:
vocab = f.read().splitlines()
# Convert the words to indices in the vocabulary
word_indices = [vocab.index(word) if word in vocab else 0 for word in words]
# Pad the sequence with zeros to a fixed length of 500
padded_indices = np.zeros(500, dtype=np.int32)
padded_indices[:len(word_indices)] = word_indices
# Convert the sequence to a tensor
tensor = np.expand_dims(padded_indices, axis=0)
return tensor
def predict_sentiment(text):
# preprocess input text
processed_text = preprocess(text)
# predict sentiment
prediction = model.predict([processed_text])[0][0]
sentiment = 'positive' if prediction >= 0.5 else 'negative'
return sentiment
iface = gr.Interface(fn=predict_sentiment,
inputs=gr.inputs.Textbox(label='Input Text'),
outputs=gr.outputs.Label(label='Sentiment Prediction'))
iface.launch()