indkhan commited on
Commit
4e7579a
1 Parent(s): dd06dec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from dotenv import load_dotenv
3
+ import googleapiclient.errors
4
+ import googleapiclient.discovery
5
+ import os
6
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
7
+ import torch
8
+
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained(
11
+ 'nlptown/bert-base-multilingual-uncased-sentiment')
12
+ model = AutoModelForSequenceClassification.from_pretrained(
13
+ 'nlptown/bert-base-multilingual-uncased-sentiment')
14
+
15
+ load_dotenv()
16
+ api_key = os.getenv("API_KEY")
17
+
18
+
19
+ def get_comments(youtube, **kwargs):
20
+ comments = []
21
+ results = youtube.commentThreads().list(**kwargs).execute()
22
+
23
+ while results:
24
+ for item in results['items']:
25
+ comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
26
+ comments.append(comment)
27
+
28
+ # check if there are more comments
29
+ if 'nextPageToken' in results:
30
+ kwargs['pageToken'] = results['nextPageToken']
31
+ results = youtube.commentThreads().list(**kwargs).execute()
32
+ else:
33
+ break
34
+
35
+ return comments
36
+
37
+
38
+ def get_video_comments(video_id, api_key):
39
+ os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
40
+
41
+ youtube = googleapiclient.discovery.build(
42
+ "youtube", "v3", developerKey=api_key)
43
+
44
+ comments = get_comments(youtube, part="snippet",
45
+ videoId=video_id, textFormat="plainText")
46
+ return comments
47
+
48
+
49
+ def makevideoid(url):
50
+ if "?v=" in url:
51
+ video_id = url.split("=")[1].split("&")[0]
52
+ return video_id
53
+ else:
54
+ video_id = url.split("/")[3].split("?")[0]
55
+ return video_id
56
+
57
+
58
+ st.title("YouTube Comment Sentiment Analysis")
59
+
60
+ # User input for video URL
61
+ video_url = st.text_input("Enter YouTube Video URL")
62
+
63
+
64
+ # Create a radio button
65
+
66
+
67
+ # Content to be displayed based on the radio button state
68
+
69
+
70
+ if video_url:
71
+
72
+ videoid = makevideoid(video_url)
73
+ comments = get_video_comments(videoid, api_key)
74
+
75
+ # Display comments and sentiment analysis
76
+ num = 0
77
+ with st.expander("Comment with sentiment analysis"):
78
+ for i, comment in enumerate(comments, 1):
79
+ tokens = tokenizer.encode(
80
+ comment, return_tensors='pt', max_length=512)
81
+ result = model(tokens)
82
+ sentiment = int(torch.argmax(result.logits)) + 1
83
+ num += sentiment
84
+
85
+ st.write(f"Comment {i}: {comment} (Sentiment: {sentiment})")
86
+ st.title(num/len(comments))