Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 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 โค๏ธ")
|