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]>
app.py
CHANGED
|
@@ -135,7 +135,22 @@ if uploaded_file:
|
|
| 135 |
|
| 136 |
# ----------------- Classification -----------------
|
| 137 |
with st.spinner("π Classifying news articles..."):
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 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)
|