|
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() |
|
|
|
return {'Positive': float(proba[0]), 'Negative': float(proba[1]), 'Neutral': float(proba[2])} |
|
|
|
|
|
classes = ['Positive', 'Negative', 'Neutral'] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_sentiment, |
|
inputs=gr.inputs.Textbox(label='Enter text here'), |
|
outputs=gr.outputs.Bar(label='Sentiment Distribution', |
|
colors=['#2a9d8f', '#e76f51', '#264653'], |
|
value=[0.3, 0.3, 0.3], |
|
min=0, |
|
max=1), |
|
title='SENTIMENT ANALYSIS' |
|
) |
|
|
|
|
|
iface.outputs[0].type = 'bar' |
|
iface.outputs[0].label = 'Sentiment Distribution' |
|
iface.outputs[0].determine_labels_from_data = False |
|
iface.outputs[0].labels = classes |
|
|
|
|
|
iface.launch() |
|
|