|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
|
|
|
|
def preprocess(text): |
|
tokenizer = tf.keras.preprocessing.text.Tokenizer() |
|
tokenizer.fit_on_texts([text]) |
|
text = tokenizer.texts_to_sequences([text]) |
|
text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=500, padding='post', truncating='post') |
|
return text |
|
|
|
|
|
model = tf.keras.models.load_model('sentimentality.h5') |
|
|
|
|
|
def predict_sentiment(text): |
|
|
|
text = preprocess(text) |
|
|
|
proba = model.predict(text)[0] |
|
|
|
proba /= proba.sum() |
|
|
|
sentiment_label = ['Positive', 'Negative', 'Neutral'][np.argmax(proba)] |
|
|
|
if sentiment_label == 'Positive': |
|
color = '#2a9d8f' |
|
elif sentiment_label == 'Negative': |
|
color = '#e76f51' |
|
else: |
|
color = '#264653' |
|
|
|
return {'label': sentiment_label, 'color': color} |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_sentiment, |
|
inputs=gr.inputs.Textbox(label='Enter text here'), |
|
outputs=gr.outputs.Label(label='Sentiment', value='Neutral', |
|
font_size=30, font_family='Arial', |
|
background_color='#f8f8f8', |
|
color='black'), |
|
title='SENTIMENT ANALYSIS' |
|
) |
|
|
|
|
|
iface.launch() |
|
|