Soundaryasos commited on
Commit
1bc7958
·
verified ·
1 Parent(s): 8979b15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -57,12 +57,23 @@ def display_sentiment_analysis(vader_score, bert_result):
57
  sentiment_data = {'Positive': max(0, vader_score), 'Negative': min(0, vader_score), 'Neutral': 1 - abs(vader_score)}
58
  sentiment_df = pd.DataFrame(list(sentiment_data.items()), columns=["Sentiment", "Score"])
59
 
60
- st.bar_chart(sentiment_df.set_index("Sentiment"))
 
 
 
61
 
62
  # Generate and display the word cloud
63
  wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'
64
  st.image(wordcloud_img, use_column_width=True)
65
 
 
 
 
 
 
 
 
 
66
  # Analyze sentiment on button click
67
  if st.sidebar.button('Analyze Sentiment'):
68
  if user_input:
@@ -81,22 +92,26 @@ if st.sidebar.button('Analyze Sentiment'):
81
  # Section for past sentiment trends
82
  st.subheader("Past Sentiment Trends (Last 14 Days)")
83
  fig1 = px.line(df, x='Date', y='Sentiment Score', title='Past Sentiment Trends', markers=True, line_shape='spline')
 
84
  st.plotly_chart(fig1)
85
 
86
  # Section for future sentiment predictions
87
  st.subheader("Sentiment Prediction for Next 7 Days")
88
  fig2 = px.line(future_df, x='Date', y='Predicted Sentiment', title='Sentiment Prediction for Next 7 Days', markers=True, line_shape='spline')
 
89
  st.plotly_chart(fig2)
90
 
91
  # Sentiment Distribution Pie Chart
92
  st.subheader("Sentiment Distribution")
93
  fig3 = px.pie(values=[sum(df['Sentiment Score'] > 0), sum(df['Sentiment Score'] <= 0)],
94
  names=['Positive', 'Negative'], title='Sentiment Distribution', hole=0.3)
 
95
  st.plotly_chart(fig3)
96
 
97
  # Histogram of Sentiment Scores (Distribution)
98
  st.subheader("Sentiment Score Distribution (Past 14 Days)")
99
  fig4 = px.histogram(df, x='Sentiment Score', nbins=20, title="Sentiment Score Distribution")
 
100
  st.plotly_chart(fig4)
101
 
102
  # Time-Series Heatmap for Sentiment Over Time
@@ -105,19 +120,34 @@ df['Day'] = df['Date'].dt.dayofweek
105
  df['Hour'] = df['Date'].dt.hour
106
  heatmap_data = df.pivot('Day', 'Hour', 'Sentiment Score')
107
  fig5 = px.imshow(heatmap_data, title="Heatmap of Sentiment Over Time", labels={'x': 'Hour of Day', 'y': 'Day of Week'})
 
108
  st.plotly_chart(fig5)
109
 
110
  # Scatter plot of Sentiment over Time
111
  st.subheader("Sentiment Scatter Plot (Past 14 Days)")
112
  fig6 = px.scatter(df, x='Date', y='Sentiment Score', title='Sentiment Over Time')
 
113
  st.plotly_chart(fig6)
114
 
115
  # Rolling average of Sentiment (7-day window)
116
  st.subheader("Rolling Average of Sentiment (7-Day Window)")
117
  df['Rolling Avg Sentiment'] = df['Sentiment Score'].rolling(window=7).mean()
118
  fig7 = px.line(df, x='Date', y='Rolling Avg Sentiment', title="Rolling Average of Sentiment (7-Day Window)")
 
119
  st.plotly_chart(fig7)
120
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  # Reset button to clear inputs and visualizations
122
  if st.sidebar.button('Reset Analysis'):
123
  st.experimental_rerun()
 
57
  sentiment_data = {'Positive': max(0, vader_score), 'Negative': min(0, vader_score), 'Neutral': 1 - abs(vader_score)}
58
  sentiment_df = pd.DataFrame(list(sentiment_data.items()), columns=["Sentiment", "Score"])
59
 
60
+ # Color graphs based on sentiment (if negative, turn red)
61
+ bar_colors = ['green' if sentiment == 'Positive' else 'red' for sentiment in sentiment_df['Sentiment']]
62
+
63
+ st.bar_chart(sentiment_df.set_index("Sentiment"), color=bar_colors)
64
 
65
  # Generate and display the word cloud
66
  wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'
67
  st.image(wordcloud_img, use_column_width=True)
68
 
69
+ # Explanation of Sentiment Analysis
70
+ st.write("""
71
+ - **VADER Sentiment Score**: This is a numerical value that indicates the overall sentiment of the text.
72
+ Positive values indicate positive sentiment, negative values indicate negative sentiment, and values close to zero indicate neutral sentiment.
73
+ - **BERT Sentiment**: This is another model's analysis, where it categorizes the text as 'Positive', 'Negative', or 'Neutral' with a confidence score.
74
+ - The bar chart visually shows the balance between positive, negative, and neutral sentiments in your text.
75
+ """)
76
+
77
  # Analyze sentiment on button click
78
  if st.sidebar.button('Analyze Sentiment'):
79
  if user_input:
 
92
  # Section for past sentiment trends
93
  st.subheader("Past Sentiment Trends (Last 14 Days)")
94
  fig1 = px.line(df, x='Date', y='Sentiment Score', title='Past Sentiment Trends', markers=True, line_shape='spline')
95
+ fig1.update_traces(line_color='red' if df['Sentiment Score'].mean() < 0 else 'green')
96
  st.plotly_chart(fig1)
97
 
98
  # Section for future sentiment predictions
99
  st.subheader("Sentiment Prediction for Next 7 Days")
100
  fig2 = px.line(future_df, x='Date', y='Predicted Sentiment', title='Sentiment Prediction for Next 7 Days', markers=True, line_shape='spline')
101
+ fig2.update_traces(line_color='red' if future_df['Predicted Sentiment'].mean() < 0 else 'green')
102
  st.plotly_chart(fig2)
103
 
104
  # Sentiment Distribution Pie Chart
105
  st.subheader("Sentiment Distribution")
106
  fig3 = px.pie(values=[sum(df['Sentiment Score'] > 0), sum(df['Sentiment Score'] <= 0)],
107
  names=['Positive', 'Negative'], title='Sentiment Distribution', hole=0.3)
108
+ fig3.update_traces(marker=dict(colors=['green', 'red']))
109
  st.plotly_chart(fig3)
110
 
111
  # Histogram of Sentiment Scores (Distribution)
112
  st.subheader("Sentiment Score Distribution (Past 14 Days)")
113
  fig4 = px.histogram(df, x='Sentiment Score', nbins=20, title="Sentiment Score Distribution")
114
+ fig4.update_traces(marker_color='red' if df['Sentiment Score'].mean() < 0 else 'green')
115
  st.plotly_chart(fig4)
116
 
117
  # Time-Series Heatmap for Sentiment Over Time
 
120
  df['Hour'] = df['Date'].dt.hour
121
  heatmap_data = df.pivot('Day', 'Hour', 'Sentiment Score')
122
  fig5 = px.imshow(heatmap_data, title="Heatmap of Sentiment Over Time", labels={'x': 'Hour of Day', 'y': 'Day of Week'})
123
+ fig5.update_traces(colorscale='reds' if df['Sentiment Score'].mean() < 0 else 'greens')
124
  st.plotly_chart(fig5)
125
 
126
  # Scatter plot of Sentiment over Time
127
  st.subheader("Sentiment Scatter Plot (Past 14 Days)")
128
  fig6 = px.scatter(df, x='Date', y='Sentiment Score', title='Sentiment Over Time')
129
+ fig6.update_traces(marker=dict(color='red' if df['Sentiment Score'].mean() < 0 else 'green'))
130
  st.plotly_chart(fig6)
131
 
132
  # Rolling average of Sentiment (7-day window)
133
  st.subheader("Rolling Average of Sentiment (7-Day Window)")
134
  df['Rolling Avg Sentiment'] = df['Sentiment Score'].rolling(window=7).mean()
135
  fig7 = px.line(df, x='Date', y='Rolling Avg Sentiment', title="Rolling Average of Sentiment (7-Day Window)")
136
+ fig7.update_traces(line_color='red' if df['Rolling Avg Sentiment'].mean() < 0 else 'green')
137
  st.plotly_chart(fig7)
138
 
139
+ # Display top comments and posts (Mock Data)
140
+ st.subheader("Top Comments and Posts")
141
+ top_comments = [
142
+ "I love this! So uplifting!",
143
+ "This is terrible, I can't believe this happened.",
144
+ "I'm not sure how I feel about this, it's mixed.",
145
+ "What a great day, everything is going well!",
146
+ "This is so frustrating, why does this always happen?"
147
+ ]
148
+ for comment in top_comments:
149
+ st.write(f"- {comment}")
150
+
151
  # Reset button to clear inputs and visualizations
152
  if st.sidebar.button('Reset Analysis'):
153
  st.experimental_rerun()