chrissoria commited on
Commit
13d9cb9
·
1 Parent(s): d9abff2

Add on-demand 'Check categories' verbosity check button

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -1137,6 +1137,8 @@ if 'image_data' not in st.session_state:
1137
  st.session_state.image_data = None
1138
  if 'extraction_params' not in st.session_state:
1139
  st.session_state.extraction_params = None # Stores params when categories are auto-extracted
 
 
1140
 
1141
  # -----------------------------------------------------------------------
1142
  # SIDEBAR
@@ -1372,6 +1374,7 @@ Soria, C. (2025). CatLLM: A Python package for LLM-based text classification. DO
1372
  st.session_state.task_mode = None
1373
  st.session_state.extracted_categories = None
1374
  st.session_state.extraction_params = None
 
1375
  st.session_state.results = None
1376
  if hasattr(st.session_state, 'example_loaded'):
1377
  del st.session_state.example_loaded
@@ -1701,10 +1704,38 @@ with col_input:
1701
  if cat_value.strip():
1702
  categories_entered.append(cat_value.strip())
1703
 
1704
- if st.session_state.category_count < MAX_CATEGORIES:
1705
- if st.button("+ Add More"):
1706
- st.session_state.category_count += 1
1707
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1708
 
1709
  if categories_entered and has_other_category and not has_other_category(categories_entered):
1710
  st.warning(
@@ -1714,6 +1745,29 @@ with col_input:
1714
  icon="⚠️",
1715
  )
1716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1717
  if st.button("Categorize Data", type="primary", use_container_width=True):
1718
  if input_data is None:
1719
  st.error("Please upload data first")
 
1137
  st.session_state.image_data = None
1138
  if 'extraction_params' not in st.session_state:
1139
  st.session_state.extraction_params = None # Stores params when categories are auto-extracted
1140
+ if 'verbosity_check' not in st.session_state:
1141
+ st.session_state.verbosity_check = None # {'categories': [...], 'result': [...]}
1142
 
1143
  # -----------------------------------------------------------------------
1144
  # SIDEBAR
 
1374
  st.session_state.task_mode = None
1375
  st.session_state.extracted_categories = None
1376
  st.session_state.extraction_params = None
1377
+ st.session_state.verbosity_check = None
1378
  st.session_state.results = None
1379
  if hasattr(st.session_state, 'example_loaded'):
1380
  del st.session_state.example_loaded
 
1704
  if cat_value.strip():
1705
  categories_entered.append(cat_value.strip())
1706
 
1707
+ add_col, check_col = st.columns(2)
1708
+ with add_col:
1709
+ if st.session_state.category_count < MAX_CATEGORIES:
1710
+ if st.button("+ Add More", use_container_width=True):
1711
+ st.session_state.category_count += 1
1712
+ st.rerun()
1713
+ with check_col:
1714
+ if st.button(
1715
+ "Check categories",
1716
+ use_container_width=True,
1717
+ disabled=not categories_entered,
1718
+ help="Use the selected model to flag categories that lack a description or example (1 API call).",
1719
+ ):
1720
+ actual_api_key, provider = get_api_key(model, model_tier, api_key)
1721
+ if not actual_api_key:
1722
+ st.error(f"{provider} API key not configured")
1723
+ else:
1724
+ with st.spinner("Checking categories…"):
1725
+ try:
1726
+ verbosity_result = catllm.check_category_verbosity(
1727
+ categories_entered,
1728
+ api_key=actual_api_key,
1729
+ user_model=model,
1730
+ model_source=get_model_source(model),
1731
+ )
1732
+ st.session_state.verbosity_check = {
1733
+ 'categories': list(categories_entered),
1734
+ 'result': verbosity_result,
1735
+ }
1736
+ except Exception as e:
1737
+ st.error(f"Verbosity check failed: {e}")
1738
+ st.rerun()
1739
 
1740
  if categories_entered and has_other_category and not has_other_category(categories_entered):
1741
  st.warning(
 
1745
  icon="⚠️",
1746
  )
1747
 
1748
+ # Verbosity check results — only show if it matches the current categories.
1749
+ vc = st.session_state.verbosity_check
1750
+ if vc and vc['categories'] == categories_entered:
1751
+ thin = [v for v in vc['result'] if not v['is_verbose']]
1752
+ if not thin:
1753
+ st.success(
1754
+ f"All {len(vc['result'])} categories include a description and examples.",
1755
+ icon="✅",
1756
+ )
1757
+ else:
1758
+ lines = []
1759
+ for v in thin:
1760
+ missing = []
1761
+ if not v.get('has_description'): missing.append("description")
1762
+ if not v.get('has_examples'): missing.append("example")
1763
+ lines.append(f"- **{v['category']}** — add a {' and '.join(missing)}")
1764
+ st.warning(
1765
+ f"{len(thin)} of {len(vc['result'])} categories could be more detailed. "
1766
+ "Verbose categories with descriptions and examples typically improve "
1767
+ "classification accuracy.\n\n" + "\n".join(lines),
1768
+ icon="⚠️",
1769
+ )
1770
+
1771
  if st.button("Categorize Data", type="primary", use_container_width=True):
1772
  if input_data is None:
1773
  st.error("Please upload data first")