nlpblogs commited on
Commit
7aa5e6a
·
verified ·
1 Parent(s): 8732927

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -78
app.py CHANGED
@@ -1,74 +1,47 @@
1
- import streamlit as st
2
-
3
  from selenium import webdriver
4
  from selenium.webdriver.common.by import By
5
  from selenium.webdriver.chrome.options import Options
6
-
7
  from selenium.webdriver.chrome.service import Service
8
-
9
  import pandas as pd
10
-
11
  from selenium.webdriver.common.keys import Keys
12
-
13
  from selenium.webdriver.support.ui import WebDriverWait
14
  from selenium.webdriver.support import expected_conditions as EC
15
  import time
16
- import sys
17
  from datetime import datetime
18
-
19
-
20
  from webdriver_manager.chrome import ChromeDriverManager
21
  from selenium.webdriver.chrome.service import Service as ChromeService
22
-
23
  from webdriver_manager.core.os_manager import ChromeType
24
-
25
  import re
26
-
27
-
28
  import transformers
29
  from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
30
  import io
31
  import plotly.express as px
32
  import zipfile
33
- import torch
34
-
35
-
36
 
37
  with st.sidebar:
38
  st.button("DEMO APP", type="primary")
39
-
40
-
41
  expander = st.expander("**Important notes on the YouTube Comments Sentiment Analysis App**")
42
  expander.write('''
43
-
44
-
45
- **How to Use**
46
- This app works with a YouTube URL. Paste the URL and press the 'Sentiment Analysis' button to perform sentiment analysis on your YouTube Comments.
47
-
48
-
49
- **Usage Limits**
50
- You can perform sentiment analysis on YouTube Comments up to 5 times.
51
-
52
-
53
- **Subscription Management**
54
- This demo app offers a one-day subscription, expiring after 24 hours. If you are interested in building your own YouTube Comments Sentiment Analysis Web App, we invite you to explore our NLP Web App Store on our website. You can select your desired features, place your order, and we will deliver your custom app in five business days. If you wish to delete your Account with us, please contact us at [email protected]
55
-
56
-
57
- **Customization**
58
- To change the app's background color to white or black, click the three-dot menu on the right-hand side of your app, go to Settings and then Choose app theme, colors and fonts.
59
-
60
-
61
- **Charts**
62
- Hover to interact with and download the charts.
63
-
64
-
65
- **File Handling and Errors**
66
- For any errors or inquiries, please contact us at [email protected]
67
-
68
-
69
-
70
- ''')
71
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  st.subheader("YouTube Comments Sentiment Analysis", divider="red")
74
  tokenizer = transformers.DistilBertTokenizer.from_pretrained("tabularisai/robust-sentiment-analysis")
@@ -103,10 +76,8 @@ if st.button("Sentiment Analysis", type="secondary"):
103
  data = []
104
  wait = WebDriverWait(driver, 30)
105
  driver.get(url)
106
-
107
  placeholder = st.empty()
108
  progress_bar = st.progress(0)
109
-
110
  for item in range(30):
111
  try:
112
  body = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.TAG_NAME, "body")))
@@ -117,31 +88,27 @@ if st.button("Sentiment Analysis", type="secondary"):
117
  except Exception as e:
118
  st.error(f"Exception during scrolling: {e}")
119
  break
120
-
121
  placeholder.text("Scrolling complete.")
122
  progress_bar.empty()
123
-
124
- try:
125
- wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#contents #contents")))
126
- comments = driver.find_elements(By.CSS_SELECTOR, "#content #content-text")
127
- user_id = 1
128
- for comment in comments:
129
- timestamp = None
130
- try:
131
- timestamp_element = comment.find_element(By.XPATH, './ancestor::ytd-comment-renderer//yt-formatted-string[@class="published-time-text style-scope ytd-comment-renderer"]')
132
- timestamp = timestamp_element.text
133
- except Exception as e:
134
- print(f"Timestamp not found for comment: {comment.text}. Error: {e}")
135
- data.append({"User ID": user_id, "Comment": comment.text, "comment_date": timestamp})
136
- user_id += 1
137
- data = [dict(t) for t in {tuple(d.items()) for d in data}]
138
- except Exception as e:
139
- st.error(f"Exception during comment extraction: {e}")
140
-
141
  driver.quit()
142
  df = pd.DataFrame(data, columns=["User ID", "Comment", "comment_date"])
143
  st.dataframe(df)
144
-
145
  if tokenizer and model:
146
  inputs = tokenizer(df['Comment'].tolist(), return_tensors="pt", padding=True, truncation=True)
147
  with torch.no_grad():
@@ -152,7 +119,6 @@ except Exception as e:
152
  for i, label in enumerate(predicted_labels):
153
  results.append({'Review Number': i + 1, 'Sentiment': model.config.id2label[label.item()]})
154
  sentiment_df = pd.DataFrame(results)
155
-
156
  value_counts1 = sentiment_df['Sentiment'].value_counts().rename_axis('Sentiment').reset_index(name='count')
157
  final_df = value_counts1
158
  tab1, tab2 = st.tabs(["Pie Chart", "Bar Chart"])
@@ -160,14 +126,11 @@ except Exception as e:
160
  fig1 = px.pie(final_df, values='count', names='Sentiment', hover_data=['count'], labels={'count': 'count'})
161
  fig1.update_traces(textposition='inside', textinfo='percent+label')
162
  st.plotly_chart(fig1)
163
-
164
  result = pd.concat([df, sentiment_df], axis=1)
165
  st.dataframe(result)
166
-
167
  with tab2:
168
  fig2 = px.bar(result, x="Sentiment", y="comment_date", color="Sentiment")
169
  st.plotly_chart(fig2)
170
-
171
  csv = result.to_csv(index=False)
172
  st.download_button(
173
  label="Download data as CSV",
@@ -175,14 +138,10 @@ except Exception as e:
175
  file_name='Summary of the results.csv',
176
  mime='text/csv',
177
  )
178
-
179
  else:
180
  st.warning("Please enter a URL.")
181
  else:
182
  st.warning(f"You have reached the maximum URL attempts ({max_attempts}).")
183
 
184
- if 'url_count' in st.session_state: #added if statement.
185
- st.write(f"URL pasted {st.session_state['url_count']} times.")
186
-
187
-
188
-
 
1
+ import streamlit as st
 
2
  from selenium import webdriver
3
  from selenium.webdriver.common.by import By
4
  from selenium.webdriver.chrome.options import Options
 
5
  from selenium.webdriver.chrome.service import Service
 
6
  import pandas as pd
 
7
  from selenium.webdriver.common.keys import Keys
 
8
  from selenium.webdriver.support.ui import WebDriverWait
9
  from selenium.webdriver.support import expected_conditions as EC
10
  import time
 
11
  from datetime import datetime
 
 
12
  from webdriver_manager.chrome import ChromeDriverManager
13
  from selenium.webdriver.chrome.service import Service as ChromeService
 
14
  from webdriver_manager.core.os_manager import ChromeType
 
15
  import re
 
 
16
  import transformers
17
  from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
18
  import io
19
  import plotly.express as px
20
  import zipfile
21
+ import torch
 
 
22
 
23
  with st.sidebar:
24
  st.button("DEMO APP", type="primary")
 
 
25
  expander = st.expander("**Important notes on the YouTube Comments Sentiment Analysis App**")
26
  expander.write('''
27
+ **How to Use**
28
+ This app works with a YouTube URL. Paste the URL and press the 'Sentiment Analysis' button to perform sentiment analysis on your YouTube Comments.
29
+
30
+ **Usage Limits**
31
+ You can perform sentiment analysis on YouTube Comments up to 5 times.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ **Subscription Management**
34
+ This demo app offers a one-day subscription, expiring after 24 hours. If you are interested in building your own YouTube Comments Sentiment Analysis Web App, we invite you to explore our NLP Web App Store on our website. You can select your desired features, place your order, and we will deliver your custom app in five business days. If you wish to delete your Account with us, please contact us at [email protected]
35
+
36
+ **Customization**
37
+ To change the app's background color to white or black, click the three-dot menu on the right-hand side of your app, go to Settings and then Choose app theme, colors and fonts.
38
+
39
+ **Charts**
40
+ Hover to interact with and download the charts.
41
+
42
+ **File Handling and Errors**
43
+ For any errors or inquiries, please contact us at [email protected]
44
+ ''')
45
 
46
  st.subheader("YouTube Comments Sentiment Analysis", divider="red")
47
  tokenizer = transformers.DistilBertTokenizer.from_pretrained("tabularisai/robust-sentiment-analysis")
 
76
  data = []
77
  wait = WebDriverWait(driver, 30)
78
  driver.get(url)
 
79
  placeholder = st.empty()
80
  progress_bar = st.progress(0)
 
81
  for item in range(30):
82
  try:
83
  body = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.TAG_NAME, "body")))
 
88
  except Exception as e:
89
  st.error(f"Exception during scrolling: {e}")
90
  break
 
91
  placeholder.text("Scrolling complete.")
92
  progress_bar.empty()
93
+ try:
94
+ wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#contents #contents")))
95
+ comments = driver.find_elements(By.CSS_SELECTOR, "#content #content-text")
96
+ user_id = 1
97
+ for comment in comments:
98
+ timestamp = None
99
+ try:
100
+ timestamp_element = comment.find_element(By.XPATH, './ancestor::ytd-comment-renderer//yt-formatted-string[@class="published-time-text style-scope ytd-comment-renderer"]')
101
+ timestamp = timestamp_element.text
102
+ except Exception as e:
103
+ print(f"Timestamp not found for comment: {comment.text}. Error: {e}")
104
+ data.append({"User ID": user_id, "Comment": comment.text, "comment_date": timestamp})
105
+ user_id += 1
106
+ data = [dict(t) for t in {tuple(d.items()) for d in data}]
107
+ except Exception as e:
108
+ st.error(f"Exception during comment extraction: {e}")
 
 
109
  driver.quit()
110
  df = pd.DataFrame(data, columns=["User ID", "Comment", "comment_date"])
111
  st.dataframe(df)
 
112
  if tokenizer and model:
113
  inputs = tokenizer(df['Comment'].tolist(), return_tensors="pt", padding=True, truncation=True)
114
  with torch.no_grad():
 
119
  for i, label in enumerate(predicted_labels):
120
  results.append({'Review Number': i + 1, 'Sentiment': model.config.id2label[label.item()]})
121
  sentiment_df = pd.DataFrame(results)
 
122
  value_counts1 = sentiment_df['Sentiment'].value_counts().rename_axis('Sentiment').reset_index(name='count')
123
  final_df = value_counts1
124
  tab1, tab2 = st.tabs(["Pie Chart", "Bar Chart"])
 
126
  fig1 = px.pie(final_df, values='count', names='Sentiment', hover_data=['count'], labels={'count': 'count'})
127
  fig1.update_traces(textposition='inside', textinfo='percent+label')
128
  st.plotly_chart(fig1)
 
129
  result = pd.concat([df, sentiment_df], axis=1)
130
  st.dataframe(result)
 
131
  with tab2:
132
  fig2 = px.bar(result, x="Sentiment", y="comment_date", color="Sentiment")
133
  st.plotly_chart(fig2)
 
134
  csv = result.to_csv(index=False)
135
  st.download_button(
136
  label="Download data as CSV",
 
138
  file_name='Summary of the results.csv',
139
  mime='text/csv',
140
  )
 
141
  else:
142
  st.warning("Please enter a URL.")
143
  else:
144
  st.warning(f"You have reached the maximum URL attempts ({max_attempts}).")
145
 
146
+ if 'url_count' in st.session_state:
147
+ st.write(f"URL pasted {st.session_state['url_count']} times.")