File size: 1,189 Bytes
2e71df7 2f9f9d9 2e71df7 52fe9f8 a7f2f89 2e71df7 a7f2f89 2f9f9d9 a7f2f89 2e71df7 a7f2f89 2e71df7 2f9f9d9 2e71df7 2f9f9d9 |
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 |
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()
|