Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import nltk | |
| from textblob import TextBlob | |
| import numpy as np | |
| from wordcloud import WordCloud | |
| import plotly.express as px | |
| from datetime import datetime, timedelta | |
| from transformers import pipeline | |
| from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer | |
| from sklearn.linear_model import LinearRegression | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import LabelEncoder | |
| # Ensure necessary NLTK data is available | |
| nltk.download('punkt') | |
| st.set_page_config(page_title="Advanced Sentiment Analyzer", layout="wide") | |
| st.title("π Advanced Sentiment Analysis Dashboard") | |
| st.markdown("Analyze sentiments with deep insights and visualizations") | |
| # Sidebar for user input | |
| st.sidebar.header("Upload your dataset") | |
| uploaded_file = st.sidebar.file_uploader("Upload a CSV file", type=["csv"]) | |
| if uploaded_file is not None: | |
| df = pd.read_csv(uploaded_file) | |
| st.sidebar.success("File uploaded successfully!") | |
| else: | |
| st.sidebar.warning("Please upload a CSV file to proceed.") | |
| # Sample Text Input | |
| st.sidebar.subheader("Enter Text for Sentiment Analysis") | |
| user_input = st.sidebar.text_area("Type or paste text here", "The product is amazing!") | |
| # Initialize sentiment analyzers | |
| analyzer = SentimentIntensityAnalyzer() | |
| bert_sentiment = pipeline("sentiment-analysis") | |
| def analyze_vader_sentiment(text): | |
| score = analyzer.polarity_scores(text)['compound'] | |
| return "Positive" if score > 0.05 else "Negative" if score < -0.05 else "Neutral" | |
| def analyze_bert_sentiment(text): | |
| result = bert_sentiment(text)[0] | |
| return result['label'] | |
| if user_input: | |
| vader_result = analyze_vader_sentiment(user_input) | |
| bert_result = analyze_bert_sentiment(user_input) | |
| st.sidebar.markdown(f"**VADER Sentiment:** {vader_result}") | |
| st.sidebar.markdown(f"**BERT Sentiment:** {bert_result}") | |
| # If dataset is uploaded, analyze sentiments | |
| if uploaded_file is not None: | |
| df['VADER Sentiment'] = df['text'].apply(analyze_vader_sentiment) | |
| df['BERT Sentiment'] = df['text'].apply(analyze_bert_sentiment) | |
| # Sentiment Distribution | |
| st.subheader("π Sentiment Distribution") | |
| fig, ax = plt.subplots() | |
| sns.countplot(x=df['VADER Sentiment'], palette="coolwarm", ax=ax) | |
| st.pyplot(fig) | |
| # Word Cloud | |
| st.subheader("βοΈ Word Cloud") | |
| sentiment_filter = st.selectbox("Filter Word Cloud by Sentiment", ["All", "Positive", "Neutral", "Negative"]) | |
| if sentiment_filter != "All": | |
| filtered_df = df[df['VADER Sentiment'] == sentiment_filter] | |
| else: | |
| filtered_df = df | |
| all_text = " ".join(filtered_df['text'].dropna().astype(str)) | |
| wordcloud = WordCloud(width=800, height=400, background_color='white').generate(all_text) | |
| st.image(wordcloud.to_array(), use_column_width=True) | |
| # Sentiment Over Time (Simulated for Demo) | |
| st.subheader("π Sentiment Trend Over Time") | |
| df['date'] = pd.date_range(end=datetime.today(), periods=len(df), freq='D') | |
| sentiment_map = {'Positive': 1, 'Neutral': 0, 'Negative': -1} | |
| df['sentiment_score'] = df['VADER Sentiment'].map(sentiment_map) | |
| fig = px.line(df, x='date', y='sentiment_score', title='Sentiment Trend') | |
| st.plotly_chart(fig) | |
| # Regression Model for Sentiment Prediction | |
| st.subheader("π Sentiment Prediction") | |
| label_encoder = LabelEncoder() | |
| df['sentiment_encoded'] = label_encoder.fit_transform(df['VADER Sentiment']) | |
| X = np.array(range(len(df))).reshape(-1, 1) | |
| y = df['sentiment_encoded'] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| model = LinearRegression() | |
| model.fit(X_train, y_train) | |
| future_days = np.array(range(len(df), len(df) + 30)).reshape(-1, 1) | |
| predicted_sentiments = model.predict(future_days) | |
| fig_pred = px.line(x=range(len(df), len(df) + 30), y=predicted_sentiments, title='Predicted Sentiment Trend') | |
| st.plotly_chart(fig_pred) | |
| # Interactive Pie Chart | |
| st.subheader("π Sentiment Breakdown") | |
| sentiment_counts = df['VADER Sentiment'].value_counts() | |
| fig_pie = px.pie(names=sentiment_counts.index, values=sentiment_counts.values, title="Sentiment Breakdown") | |
| st.plotly_chart(fig_pie) | |
| # Sentiment Heatmap | |
| st.subheader("π₯ Sentiment Heatmap") | |
| heatmap_df = df.pivot_table(index=df['date'].dt.date, columns='VADER Sentiment', aggfunc='size', fill_value=0) | |
| fig_heatmap = px.imshow(heatmap_df.T, title="Sentiment Heatmap", labels=dict(x="Date", y="Sentiment", color="Count")) | |
| st.plotly_chart(fig_heatmap) | |
| # Keyword Sentiment Analysis | |
| st.subheader("π Keyword Sentiment Analysis") | |
| keyword = st.text_input("Enter a keyword to analyze sentiment", "great") | |
| keyword_df = df[df['text'].str.contains(keyword, case=False, na=False)] | |
| st.write(keyword_df[['text', 'VADER Sentiment']]) | |
| # Download Report as CSV | |
| st.subheader("π Download Report") | |
| csv = df.to_csv(index=False).encode('utf-8') | |
| st.download_button(label="Download CSV", data=csv, file_name="sentiment_analysis.csv", mime='text/csv') | |
| st.sidebar.markdown("Developed with β€οΈ") | |