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()