Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 7 |
-
import
|
| 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 |
-
|
| 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 |
-
|
| 27 |
-
|
| 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("
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
st.
|
| 56 |
-
|
| 57 |
-
st.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
else:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
st.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
st.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ❤️")
|