|
import gradio as gr |
|
import tensorflow as tf |
|
|
|
|
|
model = tf.keras.models.load_model("sentimentality.h5") |
|
|
|
def preprocess(text): |
|
|
|
words = text.strip().lower().split() |
|
|
|
with open('vocabulary.txt', 'r') as f: |
|
vocab = f.read().splitlines() |
|
|
|
word_indices = [vocab.index(word) if word in vocab else 0 for word in words] |
|
|
|
padded_indices = np.zeros(500, dtype=np.int32) |
|
padded_indices[:len(word_indices)] = word_indices |
|
|
|
tensor = np.expand_dims(padded_indices, axis=0) |
|
return tensor |
|
|
|
def predict_sentiment(text): |
|
|
|
processed_text = preprocess(text) |
|
|
|
|
|
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() |