Soundaryasos commited on
Commit
56b9772
·
verified ·
1 Parent(s): 726e58d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -94
app.py CHANGED
@@ -1,105 +1,126 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
- from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
4
- import numpy as np
5
  import pandas as pd
6
- from datetime import datetime, timedelta
7
- import plotly.express as px
8
- from sklearn.linear_model import LinearRegression
9
- from wordcloud import WordCloud
10
- import base64
11
- from io import BytesIO
12
  import nltk
13
  from textblob import TextBlob
 
 
 
 
 
 
 
 
 
14
 
 
15
  nltk.download('punkt')
16
 
17
- # Initialize sentiment models
18
- bert_sentiment = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
19
- vader_analyzer = SentimentIntensityAnalyzer()
20
-
21
- # Generate sample past sentiment data
22
- dates = [datetime.today() - timedelta(days=i) for i in range(14)]
23
- sentiment_scores = np.random.uniform(-1, 1, len(dates))
24
- df = pd.DataFrame({"Date": dates, "Sentiment Score": sentiment_scores})
25
 
26
- # Train a regression model
27
- X = np.array(range(len(df))).reshape(-1, 1)
28
- y = df["Sentiment Score"]
29
- model = LinearRegression()
30
- model.fit(X, y)
31
-
32
- # Predict for next 7 days
33
- future_dates = [datetime.today() + timedelta(days=i) for i in range(1, 8)]
34
- X_future = np.array(range(len(df), len(df) + 7)).reshape(-1, 1)
35
- predictions = model.predict(X_future)
36
-
37
- future_df = pd.DataFrame({"Date": future_dates, "Predicted Sentiment": predictions})
38
-
39
- # Generate Word Cloud
40
- def generate_wordcloud(text):
41
- wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
42
- img = BytesIO()
43
- wordcloud.to_image().save(img, format='PNG')
44
- return base64.b64encode(img.getvalue()).decode()
45
-
46
- # Streamlit app setup
47
- st.title("🌟 Advanced Sentiment Analysis Dashboard")
48
 
49
  # Sidebar for user input
50
- st.sidebar.header("🔍 Sentiment Analysis Controls")
51
- user_input = st.sidebar.text_area("Enter text for sentiment analysis")
52
-
53
- # Display sentiment analysis results
54
- def display_sentiment_analysis(vader_score, bert_result, textblob_score):
55
- st.subheader("📊 Sentiment Analysis Results:")
56
- st.write(f"**VADER Sentiment Score**: {vader_score:.2f}")
57
- st.write(f"**BERT Sentiment**: {bert_result['label']} ({bert_result['score']:.2f})")
58
- st.write(f"**TextBlob Sentiment Polarity**: {textblob_score:.2f}")
59
-
60
- sentiment_data = {'Positive': max(0, vader_score), 'Negative': min(0, vader_score), 'Neutral': 1 - abs(vader_score)}
61
- sentiment_df = pd.DataFrame(list(sentiment_data.items()), columns=["Sentiment", "Score"])
62
- st.bar_chart(sentiment_df.set_index("Sentiment"))
63
-
64
- wordcloud_img = f'data:image/png;base64,{generate_wordcloud(user_input)}'
65
- st.image(wordcloud_img, use_column_width=True)
66
-
67
- if st.sidebar.button("Analyze Sentiment"):
68
- if user_input:
69
- with st.spinner("Analyzing text..."):
70
- vader_score = vader_analyzer.polarity_scores(user_input)['compound']
71
- bert_result = bert_sentiment(user_input)[0]
72
- textblob_score = TextBlob(user_input).sentiment.polarity
73
- display_sentiment_analysis(vader_score, bert_result, textblob_score)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  else:
75
- st.warning("⚠️ Please enter some text for analysis.")
76
-
77
- # Past sentiment trends
78
- st.subheader("📅 Past Sentiment Trends (Last 14 Days)")
79
- fig1 = px.line(df, x='Date', y='Sentiment Score', title='Sentiment Over Time', markers=True, line_shape='spline')
80
- st.plotly_chart(fig1)
81
-
82
- # Future sentiment predictions
83
- st.subheader("🔮 Sentiment Prediction for Next 7 Days")
84
- fig2 = px.line(future_df, x='Date', y='Predicted Sentiment', title='Predicted Sentiment Trend', markers=True, line_shape='spline')
85
- st.plotly_chart(fig2)
86
-
87
- # Sentiment distribution pie chart
88
- st.subheader("📊 Sentiment Distribution")
89
- fig3 = px.pie(values=[sum(df['Sentiment Score'] > 0), sum(df['Sentiment Score'] <= 0)], names=['Positive', 'Negative'], title='Sentiment Distribution', hole=0.3)
90
- st.plotly_chart(fig3)
91
-
92
- # Sentiment scatter plot
93
- st.subheader("🔎 Sentiment Scatter Plot (Last 14 Days)")
94
- fig4 = px.scatter(df, x='Date', y='Sentiment Score', title='Sentiment Over Time')
95
- st.plotly_chart(fig4)
96
-
97
- # Rolling average sentiment
98
- st.subheader("📈 Rolling Average of Sentiment (7-Day Window)")
99
- df['Rolling Avg Sentiment'] = df['Sentiment Score'].rolling(window=7).mean()
100
- fig5 = px.line(df, x='Date', y='Rolling Avg Sentiment', title="7-Day Rolling Average Sentiment")
101
- st.plotly_chart(fig5)
102
-
103
- # Reset button
104
- if st.sidebar.button('🔄 Reset Analysis'):
105
- st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
 
 
2
  import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
 
 
 
 
5
  import nltk
6
  from textblob import TextBlob
7
+ import numpy as np
8
+ from wordcloud import WordCloud
9
+ import plotly.express as px
10
+ from datetime import datetime, timedelta
11
+ from transformers import pipeline
12
+ 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')
19
 
20
+ st.set_page_config(page_title="Advanced Sentiment Analyzer", layout="wide")
 
 
 
 
 
 
 
21
 
22
+ 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
+
39
+ # Initialize sentiment analyzers
40
+ analyzer = SentimentIntensityAnalyzer()
41
+ bert_sentiment = pipeline("sentiment-analysis")
42
+
43
+ def analyze_vader_sentiment(text):
44
+ score = analyzer.polarity_scores(text)['compound']
45
+ return "Positive" if score > 0.05 else "Negative" if score < -0.05 else "Neutral"
46
+
47
+ 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 ❤️")