File size: 3,412 Bytes
20e6177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import streamlit as st
import pandas as pd
import re
import joblib
from sklearn.feature_extraction.text import CountVectorizer
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
from keras.models import load_model
from sklearn.metrics import accuracy_score

# Function to clean text
def clean_text(text):
    text = re.sub(r'<.*?>', '', text)  # Remove HTML tags
    text = re.sub(r'[^a-zA-Z\s]', '', text)  # Remove special characters and digits
    text = text.lower()  # Convert to lowercase
    text = re.sub(r'\s+', ' ', text).strip()  # Remove extra spaces
    return text

# Load the pre-trained Naive Bayes model and CountVectorizer
nb_model = joblib.load('nb_model.h5')
count_vectorizer = joblib.load('vectorizer.joblib')

# Load the pre-trained RNN model and Tokenizer
rnn_model = load_model('RNN_Model.h5')
tokenizer = joblib.load('tokenizer.joblib')

# Define max length for padding
max_length = 15

# Streamlit UI
st.title(":green[Sentiment Analysis of Reviews]")
st.write("""
This app predicts the sentiment of product reviews using two machine learning models:
- Naive Bayes
- Recurrent Neural Network (RNN)
""")

# Text input
review_text = st.text_area("Enter a review text:", "")

if st.button("Predict"):
    if review_text:
        cleaned_text = clean_text(review_text)
        
        # Naive Bayes Prediction
        review_cv = count_vectorizer.transform([cleaned_text])
        nb_prediction = nb_model.predict(review_cv)
        
        # RNN Prediction
        review_seq = tokenizer.texts_to_sequences([cleaned_text])
        review_pad = pad_sequences(review_seq, maxlen=max_length)
        rnn_prediction_prob = rnn_model.predict(review_pad)
        rnn_prediction = rnn_prediction_prob.argmax(axis=-1)[0]
        
        sentiment_mapping = {0: 'Negative Review', 1: 'Neutral Review', 2: 'Positive Review'}
        
        st.write("### Predictions")
        if nb_prediction[0] =="negative":
            st.success(f"**Naive Bayes Prediction: Negative Review With an Accuracy of 0.95**")
        elif nb_prediction[0] =="positive":
            st.success(f"**Naive Bayes Prediction: Positive Review With an Accuracy of 0.95**")
        else:
            st.success(f"**Naive Bayes Prediction: Neutral Review With an Accuracy of 0.95**")


        st.success(f"**RNN Prediction: {sentiment_mapping[rnn_prediction]} With an Accuracy of {round(rnn_prediction_prob[0][rnn_prediction],2)}**")
        
        # Display probabilities for RNN
        # st.write(f"**RNN Prediction Probabilities:**")
        # st.write(f"Negative: {rnn_prediction_prob[0][0]:.2f}")
        # st.write(f"Neutral: {rnn_prediction_prob[0][1]:.2f}")
        # st.write(f"Positive: {rnn_prediction_prob[0][2]:.2f}")
        
    else:
        st.write("Please enter a review text to get predictions.")

# Add some style to the UI
st.markdown("""
<style>
    .reportview-container {
        background: #f0f2f6;
    }
    .sidebar .sidebar-content {
        background: #f0f2f6;
    }
    .stButton>button {
        color: #ffffff;
        background-color: #4CAF50;
        border-radius: 8px;
        padding: 10px;
        border: none;
        cursor: pointer;
    }
    .stButton>button:hover {
        background-color: #red;
    }
    .stTextArea>label {
        font-size: 20px;
        color: #4CAF50;
    }
</style>
""", unsafe_allow_html=True)