Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 pre-trained regression model (if available)
|
| 10 |
+
try:
|
| 11 |
+
model = joblib.load("sentiment_regression.pkl")
|
| 12 |
+
except:
|
| 13 |
+
model = None
|
| 14 |
+
|
| 15 |
+
# Reddit Authentication
|
| 16 |
+
def authenticate_reddit():
|
| 17 |
+
return praw.Reddit(
|
| 18 |
+
client_id=st.secrets["REDDIT_CLIENT_ID"],
|
| 19 |
+
client_secret=st.secrets["REDDIT_CLIENT_SECRET"],
|
| 20 |
+
user_agent=st.secrets["REDDIT_USER_AGENT"]
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# YouTube Authentication
|
| 24 |
+
def authenticate_youtube():
|
| 25 |
+
return googleapiclient.discovery.build("youtube", "v3", developerKey=st.secrets["YOUTUBE_API_KEY"])
|
| 26 |
+
|
| 27 |
+
# Sentiment Analysis Functions
|
| 28 |
+
def vader_sentiment(text):
|
| 29 |
+
analyzer = SentimentIntensityAnalyzer()
|
| 30 |
+
return analyzer.polarity_scores(text)["compound"]
|
| 31 |
+
|
| 32 |
+
def bert_sentiment(text):
|
| 33 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 34 |
+
return sentiment_pipeline(text)[0]['label']
|
| 35 |
+
|
| 36 |
+
def regression_sentiment(text):
|
| 37 |
+
if model:
|
| 38 |
+
return model.predict([text])[0]
|
| 39 |
+
return "Model not trained yet"
|
| 40 |
+
|
| 41 |
+
# Fetch Reddit Data
|
| 42 |
+
def get_reddit_data(keyword):
|
| 43 |
+
reddit = authenticate_reddit()
|
| 44 |
+
posts = []
|
| 45 |
+
for submission in reddit.subreddit("all").search(keyword, limit=10):
|
| 46 |
+
posts.append(submission.title)
|
| 47 |
+
return posts
|
| 48 |
+
|
| 49 |
+
# Fetch YouTube Data
|
| 50 |
+
def get_youtube_data(keyword):
|
| 51 |
+
youtube = authenticate_youtube()
|
| 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 |
+
# Streamlit UI
|
| 57 |
+
st.title("Sentiment Analysis App")
|
| 58 |
+
keyword = st.text_input("Enter a keyword to analyze:")
|
| 59 |
+
|
| 60 |
+
if keyword:
|
| 61 |
+
reddit_data = get_reddit_data(keyword)
|
| 62 |
+
youtube_data = get_youtube_data(keyword)
|
| 63 |
+
|
| 64 |
+
st.subheader("Reddit Sentiment")
|
| 65 |
+
for post in reddit_data:
|
| 66 |
+
st.write(f"🔹 {post}")
|
| 67 |
+
st.write(f"VADER Sentiment: {vader_sentiment(post)}")
|
| 68 |
+
st.write(f"BERT Sentiment: {bert_sentiment(post)}")
|
| 69 |
+
st.write(f"Regression Sentiment: {regression_sentiment(post)}")
|
| 70 |
+
st.write("---")
|
| 71 |
+
|
| 72 |
+
st.subheader("YouTube Sentiment")
|
| 73 |
+
for title in youtube_data:
|
| 74 |
+
st.write(f"📺 {title}")
|
| 75 |
+
st.write(f"VADER Sentiment: {vader_sentiment(title)}")
|
| 76 |
+
st.write(f"BERT Sentiment: {bert_sentiment(title)}")
|
| 77 |
+
st.write(f"Regression Sentiment: {regression_sentiment(title)}")
|
| 78 |
+
st.write("---")
|