Spaces:
Runtime error
Runtime error
Commit
•
20e6177
1
Parent(s):
1d7d146
Update app.py
Browse files
app.py
CHANGED
@@ -1,102 +1,102 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import re
|
4 |
-
import joblib
|
5 |
-
from sklearn.feature_extraction.text import CountVectorizer
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from sklearn.metrics import accuracy_score
|
10 |
-
|
11 |
-
# Function to clean text
|
12 |
-
def clean_text(text):
|
13 |
-
text = re.sub(r'<.*?>', '', text) # Remove HTML tags
|
14 |
-
text = re.sub(r'[^a-zA-Z\s]', '', text) # Remove special characters and digits
|
15 |
-
text = text.lower() # Convert to lowercase
|
16 |
-
text = re.sub(r'\s+', ' ', text).strip() # Remove extra spaces
|
17 |
-
return text
|
18 |
-
|
19 |
-
# Load the pre-trained Naive Bayes model and CountVectorizer
|
20 |
-
nb_model = joblib.load('nb_model.h5')
|
21 |
-
count_vectorizer = joblib.load('vectorizer.joblib')
|
22 |
-
|
23 |
-
# Load the pre-trained RNN model and Tokenizer
|
24 |
-
rnn_model = load_model('RNN_Model.h5')
|
25 |
-
tokenizer = joblib.load('tokenizer.joblib')
|
26 |
-
|
27 |
-
# Define max length for padding
|
28 |
-
max_length = 15
|
29 |
-
|
30 |
-
# Streamlit UI
|
31 |
-
st.title(":green[Sentiment Analysis of Reviews]")
|
32 |
-
st.write("""
|
33 |
-
This app predicts the sentiment of product reviews using two machine learning models:
|
34 |
-
- Naive Bayes
|
35 |
-
- Recurrent Neural Network (RNN)
|
36 |
-
""")
|
37 |
-
|
38 |
-
# Text input
|
39 |
-
review_text = st.text_area("Enter a review text:", "")
|
40 |
-
|
41 |
-
if st.button("Predict"):
|
42 |
-
if review_text:
|
43 |
-
cleaned_text = clean_text(review_text)
|
44 |
-
|
45 |
-
# Naive Bayes Prediction
|
46 |
-
review_cv = count_vectorizer.transform([cleaned_text])
|
47 |
-
nb_prediction = nb_model.predict(review_cv)
|
48 |
-
|
49 |
-
# RNN Prediction
|
50 |
-
review_seq = tokenizer.texts_to_sequences([cleaned_text])
|
51 |
-
review_pad = pad_sequences(review_seq, maxlen=max_length)
|
52 |
-
rnn_prediction_prob = rnn_model.predict(review_pad)
|
53 |
-
rnn_prediction = rnn_prediction_prob.argmax(axis=-1)[0]
|
54 |
-
|
55 |
-
sentiment_mapping = {0: 'Negative Review', 1: 'Neutral Review', 2: 'Positive Review'}
|
56 |
-
|
57 |
-
st.write("### Predictions")
|
58 |
-
if nb_prediction[0] =="negative":
|
59 |
-
st.success(f"**Naive Bayes Prediction: Negative Review With an Accuracy of 0.95**")
|
60 |
-
elif nb_prediction[0] =="positive":
|
61 |
-
st.success(f"**Naive Bayes Prediction: Positive Review With an Accuracy of 0.95**")
|
62 |
-
else:
|
63 |
-
st.success(f"**Naive Bayes Prediction: Neutral Review With an Accuracy of 0.95**")
|
64 |
-
|
65 |
-
|
66 |
-
st.success(f"**RNN Prediction: {sentiment_mapping[rnn_prediction]} With an Accuracy of {round(rnn_prediction_prob[0][rnn_prediction],2)}**")
|
67 |
-
|
68 |
-
# Display probabilities for RNN
|
69 |
-
# st.write(f"**RNN Prediction Probabilities:**")
|
70 |
-
# st.write(f"Negative: {rnn_prediction_prob[0][0]:.2f}")
|
71 |
-
# st.write(f"Neutral: {rnn_prediction_prob[0][1]:.2f}")
|
72 |
-
# st.write(f"Positive: {rnn_prediction_prob[0][2]:.2f}")
|
73 |
-
|
74 |
-
else:
|
75 |
-
st.write("Please enter a review text to get predictions.")
|
76 |
-
|
77 |
-
# Add some style to the UI
|
78 |
-
st.markdown("""
|
79 |
-
<style>
|
80 |
-
.reportview-container {
|
81 |
-
background: #f0f2f6;
|
82 |
-
}
|
83 |
-
.sidebar .sidebar-content {
|
84 |
-
background: #f0f2f6;
|
85 |
-
}
|
86 |
-
.stButton>button {
|
87 |
-
color: #ffffff;
|
88 |
-
background-color: #4CAF50;
|
89 |
-
border-radius: 8px;
|
90 |
-
padding: 10px;
|
91 |
-
border: none;
|
92 |
-
cursor: pointer;
|
93 |
-
}
|
94 |
-
.stButton>button:hover {
|
95 |
-
background-color: #red;
|
96 |
-
}
|
97 |
-
.stTextArea>label {
|
98 |
-
font-size: 20px;
|
99 |
-
color: #4CAF50;
|
100 |
-
}
|
101 |
-
</style>
|
102 |
-
""", unsafe_allow_html=True)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
import joblib
|
5 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
6 |
+
from keras.preprocessing.sequence import pad_sequences
|
7 |
+
from keras.preprocessing.text import Tokenizer
|
8 |
+
from keras.models import load_model
|
9 |
+
from sklearn.metrics import accuracy_score
|
10 |
+
|
11 |
+
# Function to clean text
|
12 |
+
def clean_text(text):
|
13 |
+
text = re.sub(r'<.*?>', '', text) # Remove HTML tags
|
14 |
+
text = re.sub(r'[^a-zA-Z\s]', '', text) # Remove special characters and digits
|
15 |
+
text = text.lower() # Convert to lowercase
|
16 |
+
text = re.sub(r'\s+', ' ', text).strip() # Remove extra spaces
|
17 |
+
return text
|
18 |
+
|
19 |
+
# Load the pre-trained Naive Bayes model and CountVectorizer
|
20 |
+
nb_model = joblib.load('nb_model.h5')
|
21 |
+
count_vectorizer = joblib.load('vectorizer.joblib')
|
22 |
+
|
23 |
+
# Load the pre-trained RNN model and Tokenizer
|
24 |
+
rnn_model = load_model('RNN_Model.h5')
|
25 |
+
tokenizer = joblib.load('tokenizer.joblib')
|
26 |
+
|
27 |
+
# Define max length for padding
|
28 |
+
max_length = 15
|
29 |
+
|
30 |
+
# Streamlit UI
|
31 |
+
st.title(":green[Sentiment Analysis of Reviews]")
|
32 |
+
st.write("""
|
33 |
+
This app predicts the sentiment of product reviews using two machine learning models:
|
34 |
+
- Naive Bayes
|
35 |
+
- Recurrent Neural Network (RNN)
|
36 |
+
""")
|
37 |
+
|
38 |
+
# Text input
|
39 |
+
review_text = st.text_area("Enter a review text:", "")
|
40 |
+
|
41 |
+
if st.button("Predict"):
|
42 |
+
if review_text:
|
43 |
+
cleaned_text = clean_text(review_text)
|
44 |
+
|
45 |
+
# Naive Bayes Prediction
|
46 |
+
review_cv = count_vectorizer.transform([cleaned_text])
|
47 |
+
nb_prediction = nb_model.predict(review_cv)
|
48 |
+
|
49 |
+
# RNN Prediction
|
50 |
+
review_seq = tokenizer.texts_to_sequences([cleaned_text])
|
51 |
+
review_pad = pad_sequences(review_seq, maxlen=max_length)
|
52 |
+
rnn_prediction_prob = rnn_model.predict(review_pad)
|
53 |
+
rnn_prediction = rnn_prediction_prob.argmax(axis=-1)[0]
|
54 |
+
|
55 |
+
sentiment_mapping = {0: 'Negative Review', 1: 'Neutral Review', 2: 'Positive Review'}
|
56 |
+
|
57 |
+
st.write("### Predictions")
|
58 |
+
if nb_prediction[0] =="negative":
|
59 |
+
st.success(f"**Naive Bayes Prediction: Negative Review With an Accuracy of 0.95**")
|
60 |
+
elif nb_prediction[0] =="positive":
|
61 |
+
st.success(f"**Naive Bayes Prediction: Positive Review With an Accuracy of 0.95**")
|
62 |
+
else:
|
63 |
+
st.success(f"**Naive Bayes Prediction: Neutral Review With an Accuracy of 0.95**")
|
64 |
+
|
65 |
+
|
66 |
+
st.success(f"**RNN Prediction: {sentiment_mapping[rnn_prediction]} With an Accuracy of {round(rnn_prediction_prob[0][rnn_prediction],2)}**")
|
67 |
+
|
68 |
+
# Display probabilities for RNN
|
69 |
+
# st.write(f"**RNN Prediction Probabilities:**")
|
70 |
+
# st.write(f"Negative: {rnn_prediction_prob[0][0]:.2f}")
|
71 |
+
# st.write(f"Neutral: {rnn_prediction_prob[0][1]:.2f}")
|
72 |
+
# st.write(f"Positive: {rnn_prediction_prob[0][2]:.2f}")
|
73 |
+
|
74 |
+
else:
|
75 |
+
st.write("Please enter a review text to get predictions.")
|
76 |
+
|
77 |
+
# Add some style to the UI
|
78 |
+
st.markdown("""
|
79 |
+
<style>
|
80 |
+
.reportview-container {
|
81 |
+
background: #f0f2f6;
|
82 |
+
}
|
83 |
+
.sidebar .sidebar-content {
|
84 |
+
background: #f0f2f6;
|
85 |
+
}
|
86 |
+
.stButton>button {
|
87 |
+
color: #ffffff;
|
88 |
+
background-color: #4CAF50;
|
89 |
+
border-radius: 8px;
|
90 |
+
padding: 10px;
|
91 |
+
border: none;
|
92 |
+
cursor: pointer;
|
93 |
+
}
|
94 |
+
.stButton>button:hover {
|
95 |
+
background-color: #red;
|
96 |
+
}
|
97 |
+
.stTextArea>label {
|
98 |
+
font-size: 20px;
|
99 |
+
color: #4CAF50;
|
100 |
+
}
|
101 |
+
</style>
|
102 |
+
""", unsafe_allow_html=True)
|