|
import tensorflow as tf |
|
import gradio as gr |
|
import numpy as np |
|
|
|
|
|
model = tf.keras.models.load_model("sentiment.h5") |
|
|
|
|
|
def preprocess_text(text): |
|
|
|
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000) |
|
tokenizer.fit_on_texts(text) |
|
text = tokenizer.texts_to_sequences(text) |
|
|
|
max_len = 30 |
|
text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=max_len) |
|
return text |
|
|
|
|
|
def get_sentiment_scores(text): |
|
|
|
text = preprocess_text(text) |
|
|
|
scores = model.predict(text) |
|
|
|
label = "Positive" if np.round(scores) == 1 else "Negative" |
|
return label |
|
|
|
|
|
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() |
|
|