nlpblogs commited on
Commit
d29ce57
·
verified ·
1 Parent(s): c6127ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -56
app.py CHANGED
@@ -125,61 +125,11 @@ if st.button("Sentiment Analysis", type="secondary"):
125
  try:
126
  wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#contents #contents")))
127
  comments = driver.find_elements(By.CSS_SELECTOR, "#content #content-text")
128
- user_id = 1
129
- for comment in comments:
130
- match = re.search(r'\d{4}-\d{2}-\d{2}', comment)
131
- timestamp = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
132
- st.write(timestamp)
133
- data.append({"User ID": user_id, "Comment": comment.text, "comment_date": timestamp})
134
- user_id += 1
135
- data = [dict(t) for t in {tuple(d.items()) for d in data}]
136
  except Exception as e:
137
  st.error(f"Exception during comment extraction: {e}")
138
- driver.quit()
139
- df = pd.DataFrame(data, columns=["User ID", "Comment", "comment_date"])
140
- st.dataframe(df)
141
-
142
- if tokenizer and model:
143
- inputs = tokenizer(df['Comment'].tolist(), return_tensors="pt", padding=True, truncation=True)
144
- with torch.no_grad():
145
- logits = model(**inputs).logits
146
- predicted_probabilities = torch.nn.functional.softmax(logits, dim=-1)
147
- predicted_labels = predicted_probabilities.argmax(dim=1)
148
- results = []
149
- for i, label in enumerate(predicted_labels):
150
- results.append({'Review Number': i + 1, 'Sentiment': model.config.id2label[label.item()]})
151
- sentiment_df = pd.DataFrame(results)
152
-
153
- value_counts1 = sentiment_df['Sentiment'].value_counts().rename_axis('Sentiment').reset_index(name='count')
154
- final_df = value_counts1
155
- tab1, tab2 = st.tabs(["Pie Chart", "Bar Chart"])
156
- with tab1:
157
- fig1 = px.pie(final_df, values='count', names='Sentiment', hover_data=['count'], labels={'count': 'count'})
158
- fig1.update_traces(textposition='inside', textinfo='percent+label')
159
- st.plotly_chart(fig1)
160
-
161
- result = pd.concat([df, sentiment_df], axis=1)
162
- st.dataframe(result)
163
-
164
- with tab2:
165
- fig2 = px.bar(result, x="Sentiment", y="comment_date", color="Sentiment")
166
- st.plotly_chart(fig2)
167
-
168
- csv = result.to_csv(index=False)
169
- st.download_button(
170
- label="Download data as CSV",
171
- data=csv,
172
- file_name='Summary of the results.csv',
173
- mime='text/csv',
174
- )
175
-
176
- else:
177
- st.warning("Please enter a URL.")
178
- else:
179
- st.warning(f"You have reached the maximum URL attempts ({max_attempts}).")
180
-
181
- if 'url_count' in st.session_state: #added if statement.
182
- st.write(f"URL pasted {st.session_state['url_count']} times.")
183
-
184
-
185
-
 
125
  try:
126
  wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#contents #contents")))
127
  comments = driver.find_elements(By.CSS_SELECTOR, "#content #content-text")
128
+ st.write(comments)
129
+ match = re.search(r'\d{4}-\d{2}-\d{2}', comment)
130
+ timestamp = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
131
+ st.write(timestamp)
 
 
 
 
132
  except Exception as e:
133
  st.error(f"Exception during comment extraction: {e}")
134
+
135
+