Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
import re
|
5 |
+
import pandas as pd
|
6 |
+
import googleapiclient.discovery
|
7 |
+
import plotly.express as px
|
8 |
+
|
9 |
+
# Load the BERT tokenizer and model
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
11 |
+
model = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
12 |
+
|
13 |
+
|
14 |
+
# Set up the YouTube API service
|
15 |
+
api_service_name = "youtube"
|
16 |
+
api_version = "v3"
|
17 |
+
DEVELOPER_KEY = "AIzaSyC4Vx8G6nm3Ow9xq7NluTuCCJ1d_5w4YPE" # Replace with your actual API key
|
18 |
+
|
19 |
+
youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey=DEVELOPER_KEY)
|
20 |
+
|
21 |
+
# Function to fetch comments for a video ID
|
22 |
+
def scrape_comments(video_id):
|
23 |
+
request = youtube.commentThreads().list(
|
24 |
+
part="snippet",
|
25 |
+
videoId=video_id,
|
26 |
+
maxResults=100
|
27 |
+
)
|
28 |
+
response = request.execute()
|
29 |
+
|
30 |
+
|
31 |
+
comments = []
|
32 |
+
|
33 |
+
for item in response['items']:
|
34 |
+
comment = item['snippet']['topLevelComment']['snippet']
|
35 |
+
comments.append([
|
36 |
+
comment['textDisplay']
|
37 |
+
])
|
38 |
+
|
39 |
+
comments_df = pd.DataFrame(comments, columns=['comment'])
|
40 |
+
|
41 |
+
# df.head(10).
|
42 |
+
|
43 |
+
return comments_df
|
44 |
+
|
45 |
+
|
46 |
+
# Function to extract video ID from YouTube URL
|
47 |
+
def extract_video_id(video_url):
|
48 |
+
match = re.search(r'(?<=v=)[\w-]+', video_url)
|
49 |
+
if match:
|
50 |
+
return match.group(0)
|
51 |
+
else:
|
52 |
+
st.error("Invalid YouTube video URL")
|
53 |
+
|
54 |
+
# Function to fetch YouTube comments for a video ID
|
55 |
+
def fetch_comments(video_id):
|
56 |
+
# Example using youtube-comment-scraper-python library
|
57 |
+
comments = scrape_comments(video_id)
|
58 |
+
return comments
|
59 |
+
|
60 |
+
# Function to analyze sentiment for a single comment
|
61 |
+
def analyze_sentiment(comment):
|
62 |
+
tokens = tokenizer.encode(comment, return_tensors="pt", max_length=512, truncation=True)
|
63 |
+
# input_ids = tokens['input_ids']
|
64 |
+
# attention_mask = tokens['attention_mask']
|
65 |
+
|
66 |
+
# result = model(input_ids, attention_mask=attention_mask)
|
67 |
+
result = model(tokens)
|
68 |
+
|
69 |
+
sentiment_id = torch.argmax(result.logits) + 1
|
70 |
+
if(sentiment_id > 3):
|
71 |
+
sentiment_label = "Positive"
|
72 |
+
elif(sentiment_id < 3):
|
73 |
+
sentiment_label = "Negative"
|
74 |
+
else:
|
75 |
+
sentiment_label = "Neutral"
|
76 |
+
|
77 |
+
return sentiment_label
|
78 |
+
|
79 |
+
|
80 |
+
def main():
|
81 |
+
st.title("YouTube Comments Sentiment Analysis")
|
82 |
+
st.write("Enter a YouTube video link below:")
|
83 |
+
|
84 |
+
video_url = st.text_input("YouTube Video URL:")
|
85 |
+
if st.button("Extract Comments and Analyze"):
|
86 |
+
video_id = extract_video_id(video_url)
|
87 |
+
if video_id:
|
88 |
+
comments_df = fetch_comments(video_id)
|
89 |
+
# Comments is a dataframe of just the comments text
|
90 |
+
# st.write("Top 100 Comments extracted\n", comments_df)
|
91 |
+
comments_df['sentiment'] = comments_df['comment'].apply(lambda x: analyze_sentiment(x[:512]))
|
92 |
+
sentiment_counts = comments_df['sentiment'].value_counts()
|
93 |
+
positive_count = comments_df['sentiment'].value_counts().get('Positive', 0)
|
94 |
+
negative_count = comments_df['sentiment'].value_counts().get('Negative', 0)
|
95 |
+
neutral_count = comments_df['sentiment'].value_counts().get('Neutral', 0)
|
96 |
+
|
97 |
+
# Create pie chart in col2 with custom colors
|
98 |
+
fig_pie = px.pie(values=[positive_count, negative_count, neutral_count],
|
99 |
+
names=['Positive', 'Negative', 'Neutral'],
|
100 |
+
title='Pie chart representations',
|
101 |
+
color=sentiment_counts.index, # Use sentiment categories as colors
|
102 |
+
color_discrete_map={'Positive': 'green', 'Negative': 'red', 'Neutral': 'blue'})
|
103 |
+
st.plotly_chart(fig_pie, use_container_width=True)
|
104 |
+
|
105 |
+
# Create bar chart below the pie chart with custom colors
|
106 |
+
fig_bar = px.bar(x=sentiment_counts.index, y=sentiment_counts.values,
|
107 |
+
labels={'x': 'Sentiment', 'y': 'Count'},
|
108 |
+
title='Bar plot representations',
|
109 |
+
color=sentiment_counts.index, # Use sentiment categories as colors
|
110 |
+
color_discrete_map={'Positive': 'green', 'Negative': 'red', 'Neutral': 'blue'})
|
111 |
+
st.plotly_chart(fig_bar)
|
112 |
+
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
main()
|
116 |
+
|