File size: 1,373 Bytes
2f9f9d9 1a2c81f 625452f 1a2c81f b976ff0 1a2c81f b976ff0 1a2c81f 52fe9f8 1a2c81f 60ae5e6 1a2c81f 9811bd2 1a2c81f 60ae5e6 1a2c81f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import imdb
word_to_index = imdb.get_word_index()
# Load the pre-trained model from file
loaded_model = tf.keras.models.load_model('sentimentality.h5')
# Define a function to make predictions
def predict_sentiment(user_input, number_of_words, words_per_view):
# Encode the input text
words = user_input.split()
encoded_word = np.zeros(words_per_view).astype(int)
encoded_word[words_per_view - len(words) - 1] = 1
for i, word in enumerate(words):
index = words_per_view - len(words) + i
encoded_word[index] = word_to_index.get(word, 0) + 3
encoded_word = np.expand_dims(encoded_word, axis=0)
# Make the prediction
prediction = loaded_model.predict(encoded_word)[0][0]
# Return the sentiment label
if prediction > 0.5:
return "Positive"
else:
return "Negative"
# Create the Gradio interface
iface = gr.Interface(
fn=predict_sentiment,
inputs=["textbox", gr.inputs.NumberRange(minimum=1, maximum=10000, default=3000, label="Number of words"), gr.inputs.NumberRange(minimum=1, maximum=100, default=30, label="Words per view")],
outputs="text",
title="Sentiment Analysis",
description="Enter a text and get the sentiment prediction"
)
# Launch the interface
iface.launch()
|