Spaces:
Sleeping
Sleeping
Update App.py
Browse files
App.py
CHANGED
|
@@ -1,78 +1,79 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import praw
|
| 3 |
import googleapiclient.discovery
|
| 4 |
-
import pandas as pd
|
| 5 |
import joblib
|
|
|
|
|
|
|
|
|
|
| 6 |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
# Reddit
|
| 16 |
def authenticate_reddit():
|
| 17 |
return praw.Reddit(
|
| 18 |
-
client_id=
|
| 19 |
-
client_secret=
|
| 20 |
-
user_agent=
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# YouTube
|
| 24 |
def authenticate_youtube():
|
| 25 |
-
return googleapiclient.discovery.build("youtube", "v3", developerKey=
|
| 26 |
|
| 27 |
-
# Sentiment Analysis
|
| 28 |
-
|
| 29 |
-
analyzer = SentimentIntensityAnalyzer()
|
| 30 |
-
return analyzer.polarity_scores(text)["compound"]
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
return
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return model.predict([text])[0]
|
| 39 |
-
return "Model not trained yet"
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
posts = []
|
| 45 |
-
for submission in reddit.subreddit("all").search(keyword, limit=10):
|
| 46 |
-
posts.append(submission.title)
|
| 47 |
-
return posts
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
request = youtube.search().list(q=keyword, part="snippet", maxResults=10)
|
| 53 |
-
response = request.execute()
|
| 54 |
-
return [item["snippet"]["title"] for item in response.get("items", [])]
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
reddit_data = get_reddit_data(keyword)
|
| 62 |
-
youtube_data = get_youtube_data(keyword)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
st.write(f"Regression Sentiment: {regression_sentiment(post)}")
|
| 70 |
-
st.write("---")
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
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")
|
| 14 |
+
REDDIT_CLIENT_SECRET = os.getenv("REDDIT_CLIENT_SECRET")
|
| 15 |
+
REDDIT_USER_AGENT = os.getenv("REDDIT_USER_AGENT")
|
| 16 |
+
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 17 |
|
| 18 |
+
# Authenticate Reddit
|
| 19 |
def authenticate_reddit():
|
| 20 |
return praw.Reddit(
|
| 21 |
+
client_id=REDDIT_CLIENT_ID,
|
| 22 |
+
client_secret=REDDIT_CLIENT_SECRET,
|
| 23 |
+
user_agent=REDDIT_USER_AGENT
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Authenticate YouTube
|
| 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 App")
|
| 71 |
+
user_input = st.text_area("Enter text for sentiment analysis")
|
| 72 |
+
if st.button("Analyze"):
|
| 73 |
+
vader_score = get_vader_sentiment(user_input)
|
| 74 |
+
bert_label, bert_score = get_bert_sentiment(user_input)
|
| 75 |
+
regression_score = get_regression_sentiment(user_input)
|
| 76 |
+
|
| 77 |
+
st.write(f"**VADER Sentiment Score:** {vader_score}")
|
| 78 |
+
st.write(f"**BERT Sentiment:** {bert_label} ({bert_score:.2f})")
|
| 79 |
+
st.write(f"**Regression Sentiment Score:** {regression_score:.2f}")
|