TharushiPerera MihanTilk commited on
Commit
0c5d0ba
Β·
verified Β·
1 Parent(s): b0f32c5

Made the view by category section faster (#5)

Browse files

- Made the view by category section faster (f5cfdd9bf23d2ff12725cb4b92eadf2ad94b5bdc)


Co-authored-by: Mihan Tilakaratne <[email protected]>

Files changed (1) hide show
  1. app.py +36 -34
app.py CHANGED
@@ -135,7 +135,22 @@ if uploaded_file:
135
 
136
  # ----------------- Classification -----------------
137
  with st.spinner("πŸ” Classifying news articles..."):
138
- df['class'] = df['cleaned_text'].apply(lambda text: classifier(text)[0]['label'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
  st.success("βœ… Classification Complete!")
141
  st.write("πŸ”Ž Classified Results:", df[['content', 'class']].head())
@@ -235,41 +250,28 @@ if uploaded_file:
235
  st.warning("Date parsing failed")
236
 
237
  # ----------------- News Category Explorer -----------------
 
238
  st.subheader("πŸ” Explore News by Category")
239
 
240
- with st.spinner("πŸ” Classifying news articles..."):
241
- classified = df['cleaned_text'].apply(lambda text: classifier(text)[0])
242
- df['class'] = classified.apply(lambda x: x['label'])
243
- df['confidence'] = classified.apply(lambda x: x['score'])
244
-
245
- # Get unique categories
246
- categories = df['class'].unique()
247
-
248
- # Create 5 columns for category buttons
249
- cols = st.columns(5)
250
-
251
- # Create a dictionary to store category articles
252
- @st.cache_data
253
- def get_category_articles(df):
254
- return {category: df[df['class'] == category] for category in df['class'].unique()}
255
-
256
- category_articles = get_category_articles(df)
257
-
258
- # Place each category button in its own column
259
- for i, category in enumerate(categories):
260
- with cols[i]:
261
- if st.button(category, key=f"btn_{category}"):
262
- # Create pop-up window
263
- with st.popover(f"πŸ“° Articles in {category}", use_container_width=True):
264
- st.markdown(f"### {category} Articles")
265
- articles = category_articles[category]
266
-
267
- # Display articles with expandable content
268
- for idx, row in articles.iterrows():
269
- with st.expander(f"Article {idx + 1}: {row['content'][:50]}...", expanded=False):
270
- st.write(row['content'])
271
- st.caption(f"Classification confidence: {row['confidence']:.2f}")
272
-
273
  # ----------------- Footer -----------------
274
  st.markdown("---")
275
  st.markdown("<p style='text-align:center; color:#666;'>πŸš€ Built with using Streamlit & Hugging Face</p>", unsafe_allow_html=True)
 
135
 
136
  # ----------------- Classification -----------------
137
  with st.spinner("πŸ” Classifying news articles..."):
138
+ # Get full classification results (label + score)
139
+ classification_results = df['cleaned_text'].apply(lambda text: classifier(text)[0])
140
+
141
+ # Store all classification data in the dataframe
142
+ df['class'] = classification_results.apply(lambda x: x['label'])
143
+ df['confidence'] = classification_results.apply(lambda x: x['score'])
144
+
145
+
146
+ # Cache the categorized articles in session state
147
+ @st.cache_data
148
+ def get_category_articles(_df):
149
+ return {category: _df[_df['class'] == category] for category in _df['class'].unique()}
150
+
151
+
152
+ st.session_state.category_articles = get_category_articles(df)
153
+ st.session_state.classified_df = df # Store the full classified dataframe
154
 
155
  st.success("βœ… Classification Complete!")
156
  st.write("πŸ”Ž Classified Results:", df[['content', 'class']].head())
 
250
  st.warning("Date parsing failed")
251
 
252
  # ----------------- News Category Explorer -----------------
253
+
254
  st.subheader("πŸ” Explore News by Category")
255
 
256
+ if 'category_articles' in st.session_state:
257
+ categories = st.session_state.classified_df['class'].unique()
258
+ cols = st.columns(min(5, len(categories)))
259
+
260
+ for i, category in enumerate(categories):
261
+ with cols[i % len(cols)]:
262
+ if st.button(category, key=f"btn_{category}"):
263
+ with st.popover(f"πŸ“° {category} Articles", use_container_width=True):
264
+ st.markdown(f"### {category} Articles")
265
+ articles = st.session_state.category_articles[category]
266
+
267
+ with st.container(height=600):
268
+ for idx, row in articles.iterrows():
269
+ with st.expander(f"Article {idx + 1}", expanded=False):
270
+ st.write(row['content'])
271
+ st.caption(f"Confidence: {row['confidence']:.2f}")
272
+ else:
273
+ st.warning("Classification data not found. Please upload and classify a file first.")
274
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  # ----------------- Footer -----------------
276
  st.markdown("---")
277
  st.markdown("<p style='text-align:center; color:#666;'>πŸš€ Built with using Streamlit & Hugging Face</p>", unsafe_allow_html=True)