-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
296 lines (226 loc) · 8.58 KB
/
app.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# import streamlit as st
# import pandas as pd
# import plotly.graph_objects as go
# import joblib
# import nltk
# nltk.download('vader_lexicon')
# from nltk.sentiment.vader import SentimentIntensityAnalyzer
# from lime.lime_text import LimeTextExplainer
# # Add hero image
# st.image('images/hero.jpg', use_column_width=True)
# # Load time series data
# df_resampled = pd.read_csv('data/reviews_processed.csv')
# df_resampled.set_index('date', inplace=True)
# # Add app title
# st.title('Time Series Analysis Customer Reviews for Sandbar')
# # User input for the time frame selection and sentiment analysis
# st.subheader('Select a Time Frame')
# time_frame = st.slider('Time Frame (3-Month Moving Average)',
# min_value=-1,
# max_value=(len(df_resampled)),
# step=3)
# # Resample the data according to user-selected time frame
# resampled_data = df_resampled['stars'].rolling(window=time_frame).mean()
# # Plot the time series data
# fig = go.Figure()
# fig.add_trace(go.Scatter(
# x=df_resampled.index,
# y=df_resampled['stars'],
# mode='lines',
# name='Monthly Average'
# ))
# # Add 3-Month Moving Average
# fig.add_trace(go.Scatter(
# x=df_resampled.index,
# y=resampled_data,
# mode='lines',
# name=f'{time_frame}-Month Moving Average'
# ))
# # Add title and labels
# fig.update_layout(
# title=f'Average Star Rating Over Time with {time_frame}-Monthly Moving Average',
# xaxis_title='Time',
# yaxis_title='Average Star Rating',
# )
# # Display plot
# st.plotly_chart(fig, use_container_width=True)
# # Sentiment Analysis
# # Load Naive Bayes model and TF-IDF Vectorizer
# naiveBayesModel = joblib.load('models/naive_bayes_model.pkl')
# vectorizerTFIDF = joblib.load('models/vectorizer.pkl')
# # Instantiate VADER
# vader = SentimentIntensityAnalyzer()
# # Instantiate the LIME text explainer
# lime_explainer = LimeTextExplainer(class_names=['Positive', 'Neutral', 'Negative'])
# # Function to get the predictions from Naive Bayes and VADER
# def get_model_prediction(text):
# # VADER prediction
# vader_scores = vader.polarity_scores(text)
# vader_sentiment = max(vader_scores, key=vader_scores.get)
# # Naive Bayes
# naiveBayesVectorizer = vectorizerTFIDF.transform([text])
# naiveBayesPrediction = naiveBayesModel.predict(naiveBayesVectorizer)[0]
# return {
# 'VADER': vader_sentiment,
# 'Naive Bayes': naiveBayesPrediction
# }, vader_scores
# # Function to predict probabilities and explain using LIME
# def predict_proba(texts):
# return naiveBayesModel.predict_proba(vectorizerTFIDF.transform(texts))
# # Sentiment Analysis
# st.header('Sentiment Analysis')
# # User text input
# user_input = st.text_area('Enter text for sentiment analysis')
# # Predict sentiment
# if st.button('Analyze'):
# if user_input:
# # Get predictions
# predictions, vaderScores = get_model_prediction(text=user_input)
# # Display predictions
# st.write(f'VADER Sentiment: {predictions["VADER"]}')
# st.write(f'Naive Bayes Sentiment: {predictions["Naive Bayes"]}')
# # Visualize model confidence
# fig = go.Figure()
# # Add VADER confidence
# fig.add_trace(go.Bar(
# x=list(vaderScores.keys()),
# y=list(vaderScores.value()),
# name='VADER Scores'
# ))
# # Add Naive Bayes confidence
# fig.add_trace(go.Bar(
# x=['Naive Bayes'],
# y=[1 if predictions['Naive Bayes'] == 'Positive' else 0],
# name='Naive Bayes Scores'
# ))
# fig.update_layout(
# title = 'Model Sentiment Comparison',
# xaxis_title = 'Models',
# yaxis_title = 'Confidence Levels',
# )
# st.plotly_chart(fig, use_container_width=True)
# # LIME
# st.subheader('LIME Explanation fo Naive Bayes')
# explainer = lime_explainer.explain_instance(
# user_input,
# predict_proba,
# num_features=10
# )
# explainer_html = explainer.as_html()
# # Display LIME explanation
# st.components/v1.html(explainer_html)
# else:
# st.write('Please provide text to analyze')
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
import joblib
import nltk
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from lime.lime_text import LimeTextExplainer
# Add a hero image
st.image('images/hero.jpg', use_column_width=True)
# Load time series data
df_resampled = pd.read_csv('data/reviews_processed.csv')
df_resampled.set_index('date', inplace=True)
# Add app title
st.title('Time Series Analysis Customer Review for Sandbar')
# User input for the time frame selection and sentiment analysis (positive/negative)
st.subheader('Select a Time Frame')
time_frame = st.slider('Time Frame (Months)',
min_value=1,
max_value=(len(df_resampled)),
step=1)
# Resample data according to the user-selected time frame
resampled_data = df_resampled['stars'].rolling(window=time_frame).mean()
# Plot the time series data
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df_resampled.index,
y=df_resampled['stars'],
mode='lines',
name='Monthly Average'
))
# Add moving averages to the plot as lines on top
fig.add_trace(go.Scatter(
x=df_resampled.index,
y=resampled_data,
mode='lines',
name=f'{time_frame}-Month Moving Average'
))
# Add title and labels to the plot
fig.update_layout(
title = f'Average Star Rating Over Time with {time_frame}-Monthly Moving Average',
xaxis_title = 'Time',
yaxis_title = 'Average Star Rating',
)
# Show the plot
st.plotly_chart(fig, use_container_width=True)
# Load the Naive Bayes model and TF-IDF vectors for sentiment analysis
naiveBayesModel = joblib.load('models/naive_bayes_model.pkl')
vectorizerTFIdf = joblib.load('models/vectorizer.pkl') # Make sure the filename matches
print(f"IDF attribute present: {'idf_' in dir(vectorizerTFIdf)}") # Should print True
# Instantiate VADER
vader = SentimentIntensityAnalyzer()
# Initialize LIME text explainer
lime_explainer = LimeTextExplainer(class_names=['Positive', 'Neutral', 'Negative'])
# Function to get the predictions from VADER and Naive Bayes
def get_model_prediction(text):
# VADER prediction
vader_scores = vader.polarity_scores(text)
vader_sentiment = max(vader_scores, key=vader_scores.get)
# Naive Bayes prediction
naiveBayesVectorizer = vectorizerTFIdf.transform([text])
naiveBayesPrediction = naiveBayesModel.predict(naiveBayesVectorizer)[0]
return {
'VADER': vader_sentiment,
'Naive Bayes': naiveBayesPrediction
}, vader_scores
# Function to predict probabilities and explain them with LIME
def predict_proba(texts):
return naiveBayesModel.predict_proba(vectorizerTFIdf.transform(texts))
# Sentiment analysis with LIME
st.header('Sentiment Analysis')
# User text input
user_input = st.text_area('Enter text for sentiment analysis:')
# Predict sentiment
if st.button('Analyze'):
if user_input:
# Get predictions
predictions, vaderScores = get_model_prediction(user_input)
# Display the predictions
st.write(f'VADER Sentiment: {predictions["VADER"]}')
st.write(f'Naive Bayes Sentiment: {predictions["Naive Bayes"]}')
# Visualize model confidence
fig = go.Figure()
# Add VADER confidence
fig.add_trace(go.Bar(
x=list(vaderScores.keys()),
y=list(vaderScores.values()),
name='VADER Scores'
))
# Add Naive Bayes confidence
fig.add_trace(go.Bar(
x=['Naive Bayes'],
y=[1 if predictions['Naive Bayes'] == 'Positive' else 0],
name='Naive Bayes Score'
))
fig.update_layout(
title = 'Model Sentiment Comparison',
xaxis_title = 'Models',
yaxis_title = 'Confidence Levels',
)
st.plotly_chart(fig, use_container_width=True)
# LIME Explainer
st.subheader('LIME Explanation for Naive Bayes')
exp = lime_explainer.explain_instance(
user_input,
predict_proba,
num_features=10
)
exp_html = exp.as_html()
# Display LIME Explanation
st.components.v1.html(exp_html)
else:
st.write('Please provide text for sentiment analysis')