|
import tensorflow as tf |
|
import gradio as gr |
|
import numpy as np |
|
|
|
|
|
model = load_model('sentimentality.h5') |
|
|
|
|
|
def preprocess(text): |
|
|
|
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000) |
|
tokenizer.fit_on_texts(text) |
|
sequences = tokenizer.texts_to_sequences(text) |
|
|
|
|
|
padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=30, padding='post') |
|
|
|
return np.array(padded_sequences) |
|
|
|
|
|
def sentiment_analysis(text): |
|
preprocessed_text = preprocess([text]) |
|
prediction = model.predict(preprocessed_text)[0][0] |
|
|
|
if prediction >= 0.5: |
|
return "positive" |
|
else: |
|
return "negative" |
|
|
|
|
|
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."]] |
|
) |
|
|
|
|
|
interface.launch() |
|
|