Spaces:
Runtime error
Runtime error
The-Adnan-Syed
commited on
Commit
•
3721ad4
1
Parent(s):
ed9a36e
Upload 5 files
Browse files- RNN_Model.h5 +3 -0
- app.py +102 -0
- nb_model.h5 +3 -0
- tokenizer.joblib +3 -0
- vectorizer.joblib +3 -0
RNN_Model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:703cd7966421a38707bdf6d0c7897560ba0ddb2e34bd540cee02f2e828fd1fb7
|
3 |
+
size 7877944
|
app.py
ADDED
@@ -0,0 +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 tensorflow.keras.preprocessing.sequence import pad_sequences
|
7 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
8 |
+
from tensorflow.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)
|
nb_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:11608d7e91fc08ed7950ed69df7c07b13acea757e1fa8d79a47f1c4cd320e915
|
3 |
+
size 33467
|
tokenizer.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:96c8b412828ee400cfe41dec0182ad9dc0178822aa26cdcfc364f77f0b1b32f7
|
3 |
+
size 28836
|
vectorizer.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e9d5e767bfa0d733bb435f8adbb088c62ed4344b012c4a8dcfdb79c2a874a655
|
3 |
+
size 16817
|