nlpblogs commited on
Commit
8ae1eff
·
verified ·
1 Parent(s): f9bad0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -56
app.py CHANGED
@@ -143,62 +143,8 @@ if st.button("Sentiment Analysis", type="secondary"):
143
  driver.quit()
144
  df1 = pd.DataFrame(data, columns=["User ID", "Comment", "comment_date"])
145
  st.dataframe(df1)
146
- df = df1.dropna(subset=['Comment'], inplace=True)
147
  st.dataframe(df)
148
 
149
- if tokenizer and model:
150
- inputs = tokenizer(df['Comment'].tolist(), return_tensors="pt", padding=True, truncation=True)
151
- with torch.no_grad():
152
- logits = model(**inputs).logits
153
- predicted_probabilities = torch.nn.functional.softmax(logits, dim=-1)
154
- predicted_labels = predicted_probabilities.argmax(dim=1)
155
- results = []
156
- for i, label in enumerate(predicted_labels):
157
- results.append({'Review Number': i + 1, 'Sentiment': model.config.id2label[label.item()]})
158
- sentiment_df = pd.DataFrame(results)
159
-
160
- value_counts1 = sentiment_df['Sentiment'].value_counts().rename_axis('Sentiment').reset_index(name='count')
161
- final_df = value_counts1
162
- tab1, tab2 = st.tabs(["Pie Chart", "Bar Chart"])
163
- with tab1:
164
- fig1 = px.pie(final_df, values='count', names='Sentiment', hover_data=['count'], labels={'count': 'count'})
165
- fig1.update_traces(textposition='inside', textinfo='percent+label')
166
- st.plotly_chart(fig1)
167
-
168
- result = pd.concat([df, sentiment_df], axis=1)
169
- st.dataframe(result)
170
-
171
-
172
-
173
- with tab2:
174
- fig2 = px.bar(result, x="Sentiment", y="comment_date", color="Sentiment")
175
- st.plotly_chart(fig2)
176
-
177
- text = " ".join(comment for comment in df['Comment'])
178
- stopwords_set = set(stopwords.words('english')) # Correct import and usage
179
- text = re.sub('[^A-Za-z]+', ' ', text)
180
- words = text.split()
181
- clean_text = [word for word in words if word.lower() not in stopwords_set]
182
- clean_text = ' '.join(clean_text)
183
- wc = WordCloud(width=3000, height=2000, background_color='black', colormap='Pastel1', collocations=False).generate(clean_text)
184
- fig = plt.figure(figsize=(40, 30))
185
- plt.imshow(wc)
186
- plt.axis('off')
187
- st.pyplot(fig)
188
-
189
-
190
- csv = result.to_csv(index=False)
191
- st.download_button(
192
- label="Download data as CSV",
193
- data=csv,
194
- file_name='Summary of the results.csv',
195
- mime='text/csv',
196
- )
197
 
198
- else:
199
- st.warning("Please enter a URL.")
200
- else:
201
- st.warning(f"You have reached the maximum URL attempts ({max_attempts}).")
202
-
203
- if 'url_count' in st.session_state: #added if statement.
204
- st.write(f"URL pasted {st.session_state['url_count']} times.")
 
143
  driver.quit()
144
  df1 = pd.DataFrame(data, columns=["User ID", "Comment", "comment_date"])
145
  st.dataframe(df1)
146
+ df = df1.dropna(subset=['Comment'])
147
  st.dataframe(df)
148
 
149
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150