|
import gradio as gr |
|
import numpy as np |
|
from keras.models import load_model |
|
import re |
|
from keras.preprocessing.sequence import pad_sequences |
|
from keras.preprocessing.text import tokenizer_from_json |
|
import re |
|
import joblib |
|
import json |
|
|
|
|
|
with open('tok.json', 'r') as json_file: |
|
tokenizer_json = json.load(json_file) |
|
|
|
|
|
tokenizer = tokenizer_from_json(tokenizer_json) |
|
rnn_model = load_model("rnn_model.h5") |
|
|
|
|
|
lr_model = joblib.load("logistic_model.pkl") |
|
svm_model = joblib.load("svm_model.pkl") |
|
nn_model = load_model("dl_model.h5") |
|
mnb_model = joblib.load("mnb_model.pkl") |
|
|
|
|
|
tfidf_vectorizer = joblib.load("tfidf_vectorizer.pkl") |
|
|
|
TAG_RE = re.compile(r'<[^>]+>') |
|
|
|
def remove_tags(text): |
|
return TAG_RE.sub('', text) |
|
|
|
def preprocess_text(sen): |
|
|
|
sentence = remove_tags(sen) |
|
|
|
|
|
sentence = re.sub('[^a-zA-Z]', ' ', sentence) |
|
|
|
|
|
sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence) |
|
|
|
|
|
sentence = re.sub(r'\s+', ' ', sentence) |
|
|
|
return sentence |
|
|
|
def preprocess_text_for_rnn(text, tokenizer, maxlen): |
|
text = preprocess_text(text) |
|
sequence = tokenizer.texts_to_sequences([text]) |
|
padded_sequence = pad_sequences(sequence, padding='post', maxlen=maxlen) |
|
return padded_sequence |
|
|
|
def predict_lr(text): |
|
preprocessed_text = preprocess_text(text) |
|
vectorized_text = tfidf_vectorizer.transform([preprocessed_text]) |
|
dense_vectorized_text = vectorized_text.toarray() |
|
prediction = int(lr_model.predict(dense_vectorized_text)[0]) |
|
return prediction |
|
|
|
|
|
|
|
def predict_svm(text): |
|
preprocessed_text = preprocess_text(text) |
|
vectorized_text = tfidf_vectorizer.transform([preprocessed_text]) |
|
dense_vectorized_text = vectorized_text.toarray() |
|
prediction = int(svm_model.predict(dense_vectorized_text)[0]) |
|
return prediction |
|
|
|
|
|
def predict_nn(text): |
|
preprocessed_text = preprocess_text(text) |
|
vectorized_text = tfidf_vectorizer.transform([preprocessed_text]) |
|
dense_vectorized_text = vectorized_text.toarray() |
|
|
|
prediction_probs = nn_model.predict(dense_vectorized_text)[0] |
|
prediction = int(np.argmax(prediction_probs)) |
|
return prediction |
|
|
|
|
|
def predict_mnb(text): |
|
preprocessed_text = preprocess_text(text) |
|
vectorized_text = tfidf_vectorizer.transform([preprocessed_text]) |
|
dense_vectorized_text = vectorized_text.toarray() |
|
prediction = int(mnb_model.predict(dense_vectorized_text)[0]) |
|
return prediction |
|
|
|
def predict_rnn(text): |
|
processed_text = preprocess_text_for_rnn(text, tokenizer, maxlen=170) |
|
prediction_probs = rnn_model.predict(processed_text)[0] |
|
prediction = int(np.argmax(prediction_probs)) |
|
return prediction, prediction_probs |
|
|
|
def sentiment_prediction(text, model): |
|
prediction, percentages = 0, [] |
|
if model == "Logistic Regression": |
|
prediction = predict_lr(text) |
|
elif model == "SVM": |
|
prediction = predict_svm(text) |
|
elif model == "Neural Network": |
|
prediction = predict_nn(text) |
|
elif model == "Multinomial Naive Bayes": |
|
prediction = predict_mnb(text) |
|
elif model == "Recurrent Neural Network": |
|
prediction, percentages = predict_rnn(text) |
|
|
|
percentage_negative = percentages[0] |
|
percentage_positive = percentages[1] |
|
|
|
|
|
|
|
emoji_positive = "π" |
|
emoji_negative = "π’" |
|
emoji = emoji_positive if prediction == 1 else emoji_negative |
|
|
|
|
|
labels = ["Negative", "Positive"] |
|
|
|
|
|
prediction_label = labels[prediction] |
|
|
|
if model == "Recurrent Neural Network": |
|
return prediction_label, f" {labels[0]}: {percentage_negative:.2%}, Percentage {labels[1]}: {percentage_positive:.2%}", emoji |
|
else: |
|
|
|
return prediction_label, f"NOT AVAILABLE", emoji |
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=sentiment_prediction, |
|
inputs=[gr.Textbox(type="text", label="Enter Text"), gr.Dropdown(["Logistic Regression", "SVM", "Neural Network", "Multinomial Naive Bayes", "Recurrent Neural Network"], label="Select Model")], |
|
outputs=[gr.Label(), gr.Label(), gr.Label()], |
|
live=True, |
|
title="Sentiment Analysis with Model Selection", |
|
description="Enter a text and choose a model for sentiment prediction.", |
|
) |
|
|
|
|
|
iface.launch() |
|
|