Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,13 @@ import praw
|
|
| 4 |
import googleapiclient.discovery
|
| 5 |
import joblib
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 7 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 8 |
-
from sklearn.linear_model import LinearRegression
|
| 9 |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 10 |
from transformers import pipeline
|
|
|
|
| 11 |
|
| 12 |
# Load environment variables
|
| 13 |
REDDIT_CLIENT_ID = os.getenv("REDDIT_CLIENT_ID")
|
|
@@ -23,57 +26,46 @@ def authenticate_reddit():
|
|
| 23 |
user_agent=REDDIT_USER_AGENT
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
def authenticate_youtube():
|
| 28 |
-
return googleapiclient.discovery.build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 29 |
-
|
| 30 |
-
# VADER Sentiment Analysis
|
| 31 |
vader = SentimentIntensityAnalyzer()
|
| 32 |
-
|
| 33 |
-
def get_vader_sentiment(text):
|
| 34 |
-
scores = vader.polarity_scores(text)
|
| 35 |
-
return scores['compound'] # Ranges from -1 (negative) to +1 (positive)
|
| 36 |
-
|
| 37 |
-
# BERT Sentiment Analysis
|
| 38 |
bert_sentiment = pipeline("sentiment-analysis")
|
| 39 |
|
| 40 |
-
def get_bert_sentiment(text):
|
| 41 |
-
result = bert_sentiment(text)[0]
|
| 42 |
-
return result['label'], result['score']
|
| 43 |
-
|
| 44 |
-
# Regression Sentiment Analysis
|
| 45 |
-
vectorizer = TfidfVectorizer()
|
| 46 |
-
regressor = LinearRegression()
|
| 47 |
-
|
| 48 |
-
def train_regression_model():
|
| 49 |
-
sample_data = [
|
| 50 |
-
("I love this!", 1.0),
|
| 51 |
-
("This is amazing", 0.9),
|
| 52 |
-
("It's okay", 0.5),
|
| 53 |
-
("Not great", 0.3),
|
| 54 |
-
("I hate this", 0.1)
|
| 55 |
-
]
|
| 56 |
-
texts, scores = zip(*sample_data)
|
| 57 |
-
X = vectorizer.fit_transform(texts)
|
| 58 |
-
regressor.fit(X, scores)
|
| 59 |
-
joblib.dump((vectorizer, regressor), "sentiment_model.pkl")
|
| 60 |
-
|
| 61 |
-
train_regression_model()
|
| 62 |
-
|
| 63 |
-
# Predict with Regression Model
|
| 64 |
-
def get_regression_sentiment(text):
|
| 65 |
-
vectorizer, regressor = joblib.load("sentiment_model.pkl")
|
| 66 |
-
X = vectorizer.transform([text])
|
| 67 |
-
return regressor.predict(X)[0]
|
| 68 |
-
|
| 69 |
# Streamlit UI
|
| 70 |
-
st.title("Sentiment Analysis
|
| 71 |
user_input = st.text_area("Enter text for sentiment analysis")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
|
|
|
| 77 |
st.write(f"**VADER Sentiment Score:** {vader_score}")
|
| 78 |
st.write(f"**BERT Sentiment:** {bert_label} ({bert_score:.2f})")
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 sklearn.feature_extraction.text import TfidfVectorizer
|
|
|
|
| 11 |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 12 |
from transformers import pipeline
|
| 13 |
+
from fbprophet import Prophet
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
REDDIT_CLIENT_ID = os.getenv("REDDIT_CLIENT_ID")
|
|
|
|
| 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("Social Media Sentiment Analysis")
|
| 35 |
user_input = st.text_area("Enter text for sentiment analysis")
|
| 36 |
+
|
| 37 |
+
if user_input:
|
| 38 |
+
# VADER Sentiment
|
| 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} ({bert_score:.2f})")
|
| 49 |
+
|
| 50 |
+
# Generate fake sentiment data for forecasting (Replace with real data)
|
| 51 |
+
date_rng = pd.date_range(start=pd.Timestamp.today(), periods=30, freq='D')
|
| 52 |
+
sentiment_data = pd.DataFrame({
|
| 53 |
+
'ds': date_rng,
|
| 54 |
+
'y': np.random.uniform(-1, 1, size=len(date_rng))
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
# Train Prophet Model
|
| 58 |
+
model = Prophet()
|
| 59 |
+
model.fit(sentiment_data)
|
| 60 |
+
future = model.make_future_dataframe(periods=7)
|
| 61 |
+
forecast = model.predict(future)
|
| 62 |
+
|
| 63 |
+
# Plot Sentiment Forecast
|
| 64 |
+
st.subheader("Sentiment Forecast for Next 7 Days")
|
| 65 |
+
fig, ax = plt.subplots()
|
| 66 |
+
sns.lineplot(x=forecast['ds'], y=forecast['yhat'], ax=ax, label='Predicted Sentiment')
|
| 67 |
+
ax.fill_between(forecast['ds'], forecast['yhat_lower'], forecast['yhat_upper'], alpha=0.3)
|
| 68 |
+
ax.set_title("Sentiment Trend Prediction")
|
| 69 |
+
ax.set_xlabel("Date")
|
| 70 |
+
ax.set_ylabel("Sentiment Score")
|
| 71 |
+
st.pyplot(fig)
|