Soundaryasos commited on
Commit
ef6fd25
·
verified ·
1 Parent(s): 6a99794

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -26
app.py CHANGED
@@ -47,23 +47,16 @@ st.sidebar.header("Sentiment Analysis Controls")
47
  st.sidebar.subheader("Input")
48
  user_input = st.sidebar.text_input('Enter text for sentiment analysis')
49
 
50
- # Display explanation and detailed sentiment scores
51
  def display_sentiment_analysis(vader_score, bert_result):
52
  st.subheader("Sentiment Analysis Results:")
53
  st.write(f"**VADER Sentiment Score**: {vader_score:.2f}")
54
  st.write(f"**BERT Sentiment**: {bert_result['label']} ({bert_result['score']:.2f})")
55
 
56
- # Display sentiment breakdown in a bar chart
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
 
@@ -71,57 +64,57 @@ def display_sentiment_analysis(vader_score, bert_result):
71
  if st.sidebar.button('Analyze Sentiment'):
72
  if user_input:
73
  with st.spinner('Analyzing text...'):
74
- # VADER sentiment analysis
75
  vader_score = vader_analyzer.polarity_scores(user_input)['compound']
76
-
77
- # BERT sentiment analysis
78
  bert_result = bert_sentiment(user_input)[0]
79
-
80
- # Display sentiment analysis details
81
  display_sentiment_analysis(vader_score, bert_result)
82
  else:
83
  st.warning("Please enter some text for analysis.")
84
 
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
107
  st.subheader("Sentiment Heatmap (Past 14 Days)")
108
- df['Day'] = df['Date'].dt.dayofweek
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()
 
47
  st.sidebar.subheader("Input")
48
  user_input = st.sidebar.text_input('Enter text for sentiment analysis')
49
 
50
+ # Display sentiment analysis results
51
  def display_sentiment_analysis(vader_score, bert_result):
52
  st.subheader("Sentiment Analysis Results:")
53
  st.write(f"**VADER Sentiment Score**: {vader_score:.2f}")
54
  st.write(f"**BERT Sentiment**: {bert_result['label']} ({bert_result['score']:.2f})")
55
 
 
56
  sentiment_data = {'Positive': max(0, vader_score), 'Negative': min(0, vader_score), 'Neutral': 1 - abs(vader_score)}
57
  sentiment_df = pd.DataFrame(list(sentiment_data.items()), columns=["Sentiment", "Score"])
58
+ st.bar_chart(sentiment_df.set_index("Sentiment"))
59
 
 
 
 
 
 
 
 
60
  wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'
61
  st.image(wordcloud_img, use_column_width=True)
62
 
 
64
  if st.sidebar.button('Analyze Sentiment'):
65
  if user_input:
66
  with st.spinner('Analyzing text...'):
 
67
  vader_score = vader_analyzer.polarity_scores(user_input)['compound']
 
 
68
  bert_result = bert_sentiment(user_input)[0]
 
 
69
  display_sentiment_analysis(vader_score, bert_result)
70
  else:
71
  st.warning("Please enter some text for analysis.")
72
 
73
+ # Past sentiment trends
74
  st.subheader("Past Sentiment Trends (Last 14 Days)")
75
  fig1 = px.line(df, x='Date', y='Sentiment Score', title='Past Sentiment Trends', markers=True, line_shape='spline')
76
  st.plotly_chart(fig1)
77
 
78
+ # Future sentiment predictions
79
  st.subheader("Sentiment Prediction for Next 7 Days")
80
  fig2 = px.line(future_df, x='Date', y='Predicted Sentiment', title='Sentiment Prediction for Next 7 Days', markers=True, line_shape='spline')
81
  st.plotly_chart(fig2)
82
 
83
+ # Sentiment distribution pie chart
84
  st.subheader("Sentiment Distribution")
85
  fig3 = px.pie(values=[sum(df['Sentiment Score'] > 0), sum(df['Sentiment Score'] <= 0)],
86
  names=['Positive', 'Negative'], title='Sentiment Distribution', hole=0.3)
87
  st.plotly_chart(fig3)
88
 
89
+ # Histogram of Sentiment Scores
90
  st.subheader("Sentiment Score Distribution (Past 14 Days)")
91
  fig4 = px.histogram(df, x='Sentiment Score', nbins=20, title="Sentiment Score Distribution")
92
  st.plotly_chart(fig4)
93
 
94
+ # Sentiment heatmap (fixed version)
95
  st.subheader("Sentiment Heatmap (Past 14 Days)")
96
+ df['Day'] = df['Date'].dt.dayofweek # Monday=0, Sunday=6
97
+ heatmap_data = df.groupby('Day')['Sentiment Score'].mean().reset_index()
98
+ heatmap_data = heatmap_data.pivot(index='Day', columns=None, values='Sentiment Score')
99
+ fig5 = px.imshow(
100
+ [heatmap_data.values],
101
+ title="Heatmap of Sentiment Over Time",
102
+ labels={'x': 'Day of Week', 'y': 'Sentiment Score'},
103
+ color_continuous_scale='RdBu'
104
+ )
105
  st.plotly_chart(fig5)
106
 
107
+ # Sentiment scatter plot
108
  st.subheader("Sentiment Scatter Plot (Past 14 Days)")
109
  fig6 = px.scatter(df, x='Date', y='Sentiment Score', title='Sentiment Over Time')
110
  st.plotly_chart(fig6)
111
 
112
+ # Rolling average sentiment
113
  st.subheader("Rolling Average of Sentiment (7-Day Window)")
114
  df['Rolling Avg Sentiment'] = df['Sentiment Score'].rolling(window=7).mean()
115
  fig7 = px.line(df, x='Date', y='Rolling Avg Sentiment', title="Rolling Average of Sentiment (7-Day Window)")
116
  st.plotly_chart(fig7)
117
 
118
+ # Reset button
119
  if st.sidebar.button('Reset Analysis'):
120
  st.experimental_rerun()