Soundaryasos commited on
Commit
e77bbae
ยท
verified ยท
1 Parent(s): 56b9772

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -78
app.py CHANGED
@@ -13,6 +13,8 @@ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
13
  from sklearn.linear_model import LinearRegression
14
  from sklearn.model_selection import train_test_split
15
  from sklearn.preprocessing import LabelEncoder
 
 
16
 
17
  # Ensure necessary NLTK data is available
18
  nltk.download('punkt')
@@ -23,16 +25,6 @@ st.title("๐Ÿ” Advanced Sentiment Analysis Dashboard")
23
  st.markdown("Analyze sentiments with deep insights and visualizations")
24
 
25
  # Sidebar for user input
26
- st.sidebar.header("Upload your dataset")
27
- uploaded_file = st.sidebar.file_uploader("Upload a CSV file", type=["csv"])
28
-
29
- if uploaded_file is not None:
30
- df = pd.read_csv(uploaded_file)
31
- st.sidebar.success("File uploaded successfully!")
32
- else:
33
- st.sidebar.warning("Please upload a CSV file to proceed.")
34
-
35
- # Sample Text Input
36
  st.sidebar.subheader("Enter Text for Sentiment Analysis")
37
  user_input = st.sidebar.text_area("Type or paste text here", "The product is amazing!")
38
 
@@ -48,79 +40,77 @@ def analyze_bert_sentiment(text):
48
  result = bert_sentiment(text)[0]
49
  return result['label']
50
 
 
 
 
51
  if user_input:
52
  vader_result = analyze_vader_sentiment(user_input)
53
  bert_result = analyze_bert_sentiment(user_input)
 
 
54
  st.sidebar.markdown(f"**VADER Sentiment:** {vader_result}")
55
  st.sidebar.markdown(f"**BERT Sentiment:** {bert_result}")
 
56
 
57
- # If dataset is uploaded, analyze sentiments
58
- if uploaded_file is not None:
59
- df['VADER Sentiment'] = df['text'].apply(analyze_vader_sentiment)
60
- df['BERT Sentiment'] = df['text'].apply(analyze_bert_sentiment)
61
-
62
- # Sentiment Distribution
63
- st.subheader("๐Ÿ“Š Sentiment Distribution")
64
- fig, ax = plt.subplots()
65
- sns.countplot(x=df['VADER Sentiment'], palette="coolwarm", ax=ax)
66
- st.pyplot(fig)
67
-
68
- # Word Cloud
69
- st.subheader("โ˜๏ธ Word Cloud")
70
- sentiment_filter = st.selectbox("Filter Word Cloud by Sentiment", ["All", "Positive", "Neutral", "Negative"])
71
- if sentiment_filter != "All":
72
- filtered_df = df[df['VADER Sentiment'] == sentiment_filter]
73
- else:
74
- filtered_df = df
75
- all_text = " ".join(filtered_df['text'].dropna().astype(str))
76
- wordcloud = WordCloud(width=800, height=400, background_color='white').generate(all_text)
77
- st.image(wordcloud.to_array(), use_column_width=True)
78
-
79
- # Sentiment Over Time (Simulated for Demo)
80
- st.subheader("๐Ÿ“ˆ Sentiment Trend Over Time")
81
- df['date'] = pd.date_range(end=datetime.today(), periods=len(df), freq='D')
82
- sentiment_map = {'Positive': 1, 'Neutral': 0, 'Negative': -1}
83
- df['sentiment_score'] = df['VADER Sentiment'].map(sentiment_map)
84
-
85
- fig = px.line(df, x='date', y='sentiment_score', title='Sentiment Trend')
86
- st.plotly_chart(fig)
87
-
88
- # Regression Model for Sentiment Prediction
89
- st.subheader("๐Ÿ“ˆ Sentiment Prediction")
90
- label_encoder = LabelEncoder()
91
- df['sentiment_encoded'] = label_encoder.fit_transform(df['VADER Sentiment'])
92
- X = np.array(range(len(df))).reshape(-1, 1)
93
- y = df['sentiment_encoded']
94
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
95
- model = LinearRegression()
96
- model.fit(X_train, y_train)
97
- future_days = np.array(range(len(df), len(df) + 30)).reshape(-1, 1)
98
- predicted_sentiments = model.predict(future_days)
99
-
100
- fig_pred = px.line(x=range(len(df), len(df) + 30), y=predicted_sentiments, title='Predicted Sentiment Trend')
101
- st.plotly_chart(fig_pred)
102
-
103
- # Interactive Pie Chart
104
- st.subheader("๐Ÿ“ Sentiment Breakdown")
105
- sentiment_counts = df['VADER Sentiment'].value_counts()
106
- fig_pie = px.pie(names=sentiment_counts.index, values=sentiment_counts.values, title="Sentiment Breakdown")
107
- st.plotly_chart(fig_pie)
108
-
109
- # Sentiment Heatmap
110
- st.subheader("๐Ÿ”ฅ Sentiment Heatmap")
111
- heatmap_df = df.pivot_table(index=df['date'].dt.date, columns='VADER Sentiment', aggfunc='size', fill_value=0)
112
- fig_heatmap = px.imshow(heatmap_df.T, title="Sentiment Heatmap", labels=dict(x="Date", y="Sentiment", color="Count"))
113
- st.plotly_chart(fig_heatmap)
114
-
115
- # Keyword Sentiment Analysis
116
- st.subheader("๐Ÿ” Keyword Sentiment Analysis")
117
- keyword = st.text_input("Enter a keyword to analyze sentiment", "great")
118
- keyword_df = df[df['text'].str.contains(keyword, case=False, na=False)]
119
- st.write(keyword_df[['text', 'VADER Sentiment']])
120
-
121
- # Download Report as CSV
122
- st.subheader("๐Ÿ“„ Download Report")
123
- csv = df.to_csv(index=False).encode('utf-8')
124
- st.download_button(label="Download CSV", data=csv, file_name="sentiment_analysis.csv", mime='text/csv')
125
 
126
  st.sidebar.markdown("Developed with โค๏ธ")
 
13
  from sklearn.linear_model import LinearRegression
14
  from sklearn.model_selection import train_test_split
15
  from sklearn.preprocessing import LabelEncoder
16
+ from io import BytesIO
17
+ import base64
18
 
19
  # Ensure necessary NLTK data is available
20
  nltk.download('punkt')
 
25
  st.markdown("Analyze sentiments with deep insights and visualizations")
26
 
27
  # Sidebar for user input
 
 
 
 
 
 
 
 
 
 
28
  st.sidebar.subheader("Enter Text for Sentiment Analysis")
29
  user_input = st.sidebar.text_area("Type or paste text here", "The product is amazing!")
30
 
 
40
  result = bert_sentiment(text)[0]
41
  return result['label']
42
 
43
+ def analyze_textblob_sentiment(text):
44
+ return "Positive" if TextBlob(text).sentiment.polarity > 0 else "Negative" if TextBlob(text).sentiment.polarity < 0 else "Neutral"
45
+
46
  if user_input:
47
  vader_result = analyze_vader_sentiment(user_input)
48
  bert_result = analyze_bert_sentiment(user_input)
49
+ textblob_result = analyze_textblob_sentiment(user_input)
50
+
51
  st.sidebar.markdown(f"**VADER Sentiment:** {vader_result}")
52
  st.sidebar.markdown(f"**BERT Sentiment:** {bert_result}")
53
+ st.sidebar.markdown(f"**TextBlob Sentiment:** {textblob_result}")
54
 
55
+ # Simulated past sentiment data
56
+ dates = [datetime.today() - timedelta(days=i) for i in range(14)]
57
+ sentiment_scores = np.random.uniform(-1, 1, len(dates))
58
+ df = pd.DataFrame({"Date": dates, "Sentiment Score": sentiment_scores})
59
+
60
+ # Train a regression model
61
+ X = np.array(range(len(df))).reshape(-1, 1)
62
+ y = df["Sentiment Score"]
63
+ model = LinearRegression()
64
+ model.fit(X, y)
65
+
66
+ # Predict for next 7 days
67
+ future_dates = [datetime.today() + timedelta(days=i) for i in range(1, 8)]
68
+ X_future = np.array(range(len(df), len(df) + 7)).reshape(-1, 1)
69
+ predictions = model.predict(X_future)
70
+ future_df = pd.DataFrame({"Date": future_dates, "Predicted Sentiment": predictions})
71
+
72
+ # Past sentiment trends
73
+ st.subheader("๐Ÿ“… Past Sentiment Trends (Last 14 Days)")
74
+ fig1 = px.line(df, x='Date', y='Sentiment Score', title='Sentiment Over Time', markers=True, line_shape='spline')
75
+ st.plotly_chart(fig1)
76
+
77
+ # Future sentiment predictions
78
+ st.subheader("๐Ÿ”ฎ Sentiment Prediction for Next 7 Days")
79
+ fig2 = px.line(future_df, x='Date', y='Predicted Sentiment', title='Predicted Sentiment Trend', markers=True, line_shape='spline')
80
+ st.plotly_chart(fig2)
81
+
82
+ # Sentiment distribution pie chart
83
+ st.subheader("๐Ÿ“Š Sentiment Distribution")
84
+ fig3 = px.pie(values=[sum(df['Sentiment Score'] > 0), sum(df['Sentiment Score'] <= 0)], names=['Positive', 'Negative'], title='Sentiment Distribution', hole=0.3)
85
+ st.plotly_chart(fig3)
86
+
87
+ # Sentiment scatter plot
88
+ st.subheader("๐Ÿ”Ž Sentiment Scatter Plot (Last 14 Days)")
89
+ fig4 = px.scatter(df, x='Date', y='Sentiment Score', title='Sentiment Over Time')
90
+ st.plotly_chart(fig4)
91
+
92
+ # Rolling average sentiment
93
+ st.subheader("๐Ÿ“ˆ Rolling Average of Sentiment (7-Day Window)")
94
+ df['Rolling Avg Sentiment'] = df['Sentiment Score'].rolling(window=7).mean()
95
+ fig5 = px.line(df, x='Date', y='Rolling Avg Sentiment', title="7-Day Rolling Average Sentiment")
96
+ st.plotly_chart(fig5)
97
+
98
+ # Generate Word Cloud
99
+ def generate_wordcloud(text):
100
+ wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
101
+ img = BytesIO()
102
+ wordcloud.to_image().save(img, format='PNG')
103
+ return base64.b64encode(img.getvalue()).decode()
104
+
105
+ # Word Cloud
106
+ st.subheader("โ˜๏ธ Word Cloud")
107
+ if user_input:
108
+ wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'
109
+ st.image(wordcloud_img, use_column_width=True)
110
+
111
+ # Download Report as CSV
112
+ st.subheader("๐Ÿ“„ Download Report")
113
+ csv = df.to_csv(index=False).encode('utf-8')
114
+ st.download_button(label="Download CSV", data=csv, file_name="sentiment_analysis.csv", mime='text/csv')
 
 
 
 
 
 
 
 
115
 
116
  st.sidebar.markdown("Developed with โค๏ธ")