Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,75 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
import praw
|
| 4 |
-
import googleapiclient.discovery
|
| 5 |
-
import joblib
|
| 6 |
-
import numpy as np
|
| 7 |
import pandas as pd
|
|
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
import seaborn as sns
|
| 10 |
-
from
|
| 11 |
-
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 12 |
from transformers import pipeline
|
|
|
|
| 13 |
from prophet import Prophet
|
| 14 |
|
| 15 |
-
# Load
|
| 16 |
-
REDDIT_CLIENT_ID = os.getenv("REDDIT_CLIENT_ID")
|
| 17 |
-
REDDIT_CLIENT_SECRET = os.getenv("REDDIT_CLIENT_SECRET")
|
| 18 |
-
REDDIT_USER_AGENT = os.getenv("REDDIT_USER_AGENT")
|
| 19 |
-
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 20 |
-
|
| 21 |
-
# Authenticate Reddit
|
| 22 |
-
def authenticate_reddit():
|
| 23 |
-
return praw.Reddit(
|
| 24 |
-
client_id=REDDIT_CLIENT_ID,
|
| 25 |
-
client_secret=REDDIT_CLIENT_SECRET,
|
| 26 |
-
user_agent=REDDIT_USER_AGENT
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
# Initialize sentiment analysis tools
|
| 30 |
vader = SentimentIntensityAnalyzer()
|
| 31 |
bert_sentiment = pipeline("sentiment-analysis")
|
| 32 |
|
| 33 |
-
# Streamlit UI
|
| 34 |
-
st.title("
|
|
|
|
|
|
|
| 35 |
user_input = st.text_area("Enter text for sentiment analysis")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if user_input:
|
| 38 |
-
|
| 39 |
-
vader_score = vader.polarity_scores(user_input)["compound"]
|
| 40 |
-
|
| 41 |
-
# BERT Sentiment
|
| 42 |
-
bert_result = bert_sentiment(user_input)[0]
|
| 43 |
-
bert_label = bert_result["label"]
|
| 44 |
-
bert_score = bert_result["score"]
|
| 45 |
|
| 46 |
# Display results
|
|
|
|
| 47 |
st.write(f"**VADER Sentiment Score:** {vader_score}")
|
| 48 |
-
st.write(f"**BERT Sentiment:** {bert_label} ({
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
model = Prophet()
|
| 59 |
-
model.fit(
|
| 60 |
future = model.make_future_dataframe(periods=7)
|
| 61 |
forecast = model.predict(future)
|
| 62 |
|
| 63 |
-
# Plot
|
| 64 |
-
st.subheader("Sentiment
|
| 65 |
fig, ax = plt.subplots()
|
| 66 |
-
|
| 67 |
-
ax.fill_between(forecast['ds'], forecast['yhat_lower'], forecast['yhat_upper'], alpha=0.
|
|
|
|
| 68 |
ax.set_title("Sentiment Trend Prediction")
|
| 69 |
ax.set_xlabel("Date")
|
| 70 |
ax.set_ylabel("Sentiment Score")
|
|
|
|
| 71 |
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import seaborn as sns
|
| 7 |
+
from wordcloud import WordCloud
|
|
|
|
| 8 |
from transformers import pipeline
|
| 9 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 10 |
from prophet import Prophet
|
| 11 |
|
| 12 |
+
# Load sentiment analysis models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
vader = SentimentIntensityAnalyzer()
|
| 14 |
bert_sentiment = pipeline("sentiment-analysis")
|
| 15 |
|
| 16 |
+
# Streamlit UI setup
|
| 17 |
+
st.title("Sentiment Analysis & Prediction")
|
| 18 |
+
|
| 19 |
+
# User input
|
| 20 |
user_input = st.text_area("Enter text for sentiment analysis")
|
| 21 |
|
| 22 |
+
def analyze_sentiment(text):
|
| 23 |
+
"""Perform sentiment analysis using VADER and BERT."""
|
| 24 |
+
vader_score = vader.polarity_scores(text)['compound']
|
| 25 |
+
bert_result = bert_sentiment(text)[0]
|
| 26 |
+
return vader_score, bert_result['label'], bert_result['score']
|
| 27 |
+
|
| 28 |
+
# Process input
|
| 29 |
if user_input:
|
| 30 |
+
vader_score, bert_label, bert_confidence = analyze_sentiment(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Display results
|
| 33 |
+
st.subheader("Sentiment Analysis Results")
|
| 34 |
st.write(f"**VADER Sentiment Score:** {vader_score}")
|
| 35 |
+
st.write(f"**BERT Sentiment:** {bert_label} ({bert_confidence:.2f})")
|
| 36 |
|
| 37 |
+
# Word Cloud
|
| 38 |
+
st.subheader("Word Cloud of Input Text")
|
| 39 |
+
wordcloud = WordCloud(width=600, height=400, background_color='white').generate(user_input)
|
| 40 |
+
fig, ax = plt.subplots()
|
| 41 |
+
ax.imshow(wordcloud, interpolation='bilinear')
|
| 42 |
+
ax.axis("off")
|
| 43 |
+
st.pyplot(fig)
|
| 44 |
|
| 45 |
+
# Time Series Data for Prediction
|
| 46 |
+
days = 30
|
| 47 |
+
date_range = pd.date_range(start=pd.Timestamp.today(), periods=days, freq='D')
|
| 48 |
+
sentiment_scores = np.cumsum(np.random.randn(days) * 0.1 + vader_score)
|
| 49 |
+
df = pd.DataFrame({'ds': date_range, 'y': sentiment_scores})
|
| 50 |
+
|
| 51 |
+
# Facebook Prophet Prediction
|
| 52 |
model = Prophet()
|
| 53 |
+
model.fit(df)
|
| 54 |
future = model.make_future_dataframe(periods=7)
|
| 55 |
forecast = model.predict(future)
|
| 56 |
|
| 57 |
+
# Plot predictions
|
| 58 |
+
st.subheader("Sentiment Prediction for Next 7 Days")
|
| 59 |
fig, ax = plt.subplots()
|
| 60 |
+
ax.plot(forecast['ds'], forecast['yhat'], label='Predicted Sentiment', color='blue')
|
| 61 |
+
ax.fill_between(forecast['ds'], forecast['yhat_lower'], forecast['yhat_upper'], alpha=0.2, color='blue')
|
| 62 |
+
ax.axhline(0, color='black', linestyle='dashed')
|
| 63 |
ax.set_title("Sentiment Trend Prediction")
|
| 64 |
ax.set_xlabel("Date")
|
| 65 |
ax.set_ylabel("Sentiment Score")
|
| 66 |
+
ax.legend()
|
| 67 |
st.pyplot(fig)
|
| 68 |
+
|
| 69 |
+
# Explanation
|
| 70 |
+
st.subheader("What These Results Mean")
|
| 71 |
+
st.write("- **VADER Score:** Measures sentiment from -1 (negative) to +1 (positive)")
|
| 72 |
+
st.write("- **BERT Sentiment:** Deep learning-based classification")
|
| 73 |
+
st.write("- **Prediction Graph:** Expected sentiment trend for the next 7 days")
|
| 74 |
+
|
| 75 |
+
st.write("\n---\nBuilt with Streamlit, VADER, BERT, and Facebook Prophet 🚀")
|