Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,23 +57,16 @@ 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 |
-
#
|
| 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,26 +85,22 @@ if st.sidebar.button('Analyze Sentiment'):
|
|
| 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,34 +109,19 @@ df['Day'] = df['Date'].dt.dayofweek
|
|
| 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()
|
|
|
|
| 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 |
+
# Set color dynamically based on sentiment
|
| 61 |
+
bar_colors = ['green' if sentiment == 'Positive' else 'red' if sentiment == 'Negative' else 'gray' for sentiment in sentiment_df['Sentiment']]
|
| 62 |
|
| 63 |
+
# Display the bar chart
|
| 64 |
st.bar_chart(sentiment_df.set_index("Sentiment"), color=bar_colors)
|
| 65 |
|
| 66 |
# Generate and display the word cloud
|
| 67 |
wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'
|
| 68 |
st.image(wordcloud_img, use_column_width=True)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
# Analyze sentiment on button click
|
| 71 |
if st.sidebar.button('Analyze Sentiment'):
|
| 72 |
if user_input:
|
|
|
|
| 85 |
# Section for past sentiment trends
|
| 86 |
st.subheader("Past Sentiment Trends (Last 14 Days)")
|
| 87 |
fig1 = px.line(df, x='Date', y='Sentiment Score', title='Past Sentiment Trends', markers=True, line_shape='spline')
|
|
|
|
| 88 |
st.plotly_chart(fig1)
|
| 89 |
|
| 90 |
# Section for future sentiment predictions
|
| 91 |
st.subheader("Sentiment Prediction for Next 7 Days")
|
| 92 |
fig2 = px.line(future_df, x='Date', y='Predicted Sentiment', title='Sentiment Prediction for Next 7 Days', markers=True, line_shape='spline')
|
|
|
|
| 93 |
st.plotly_chart(fig2)
|
| 94 |
|
| 95 |
# Sentiment Distribution Pie Chart
|
| 96 |
st.subheader("Sentiment Distribution")
|
| 97 |
fig3 = px.pie(values=[sum(df['Sentiment Score'] > 0), sum(df['Sentiment Score'] <= 0)],
|
| 98 |
names=['Positive', 'Negative'], title='Sentiment Distribution', hole=0.3)
|
|
|
|
| 99 |
st.plotly_chart(fig3)
|
| 100 |
|
| 101 |
# Histogram of Sentiment Scores (Distribution)
|
| 102 |
st.subheader("Sentiment Score Distribution (Past 14 Days)")
|
| 103 |
fig4 = px.histogram(df, x='Sentiment Score', nbins=20, title="Sentiment Score Distribution")
|
|
|
|
| 104 |
st.plotly_chart(fig4)
|
| 105 |
|
| 106 |
# Time-Series Heatmap for Sentiment Over Time
|
|
|
|
| 109 |
df['Hour'] = df['Date'].dt.hour
|
| 110 |
heatmap_data = df.pivot('Day', 'Hour', 'Sentiment Score')
|
| 111 |
fig5 = px.imshow(heatmap_data, title="Heatmap of Sentiment Over Time", labels={'x': 'Hour of Day', 'y': 'Day of Week'})
|
|
|
|
| 112 |
st.plotly_chart(fig5)
|
| 113 |
|
| 114 |
# Scatter plot of Sentiment over Time
|
| 115 |
st.subheader("Sentiment Scatter Plot (Past 14 Days)")
|
| 116 |
fig6 = px.scatter(df, x='Date', y='Sentiment Score', title='Sentiment Over Time')
|
|
|
|
| 117 |
st.plotly_chart(fig6)
|
| 118 |
|
| 119 |
# Rolling average of Sentiment (7-day window)
|
| 120 |
st.subheader("Rolling Average of Sentiment (7-Day Window)")
|
| 121 |
df['Rolling Avg Sentiment'] = df['Sentiment Score'].rolling(window=7).mean()
|
| 122 |
fig7 = px.line(df, x='Date', y='Rolling Avg Sentiment', title="Rolling Average of Sentiment (7-Day Window)")
|
|
|
|
| 123 |
st.plotly_chart(fig7)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# Reset button to clear inputs and visualizations
|
| 126 |
if st.sidebar.button('Reset Analysis'):
|
| 127 |
st.experimental_rerun()
|