import tensorflow as tf import gradio as gr import numpy as np # Load the saved model model = load_model('sentimentality.h5') # Define the function to preprocess the text def preprocess(text): # Tokenize the text tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000) tokenizer.fit_on_texts(text) sequences = tokenizer.texts_to_sequences(text) # Pad the sequences to a fixed length of 30 padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=30, padding='post') return np.array(padded_sequences) # Define the function to make predictions on the text def sentiment_analysis(text): preprocessed_text = preprocess([text]) prediction = model.predict(preprocessed_text)[0][0] if prediction >= 0.5: return "positive" else: return "negative" # Define the Gradio interface interface = gr.Interface( fn=get_sentiment_scores, inputs=gr.inputs.Textbox(placeholder="Enter a positive or negative sentence here..."), outputs=gr.outputs.Textbox(label="Sentiment Label"), examples=[["This is wonderful!"], ["I hate this product."]] ) # Launch the interface interface.launch()