Spaces:
Sleeping
Sleeping
Commit ·
2534871
1
Parent(s): 6bb5e51
Replace consensus threshold slider with clear button options
Browse files- Replace numeric slider with radio buttons: Majority (50%+), Two-Thirds (67%+), Unanimous (100%)
- Pass consensus_threshold to code generation for accurate reproducibility code
- Update "See in Code" modal to reflect current classify mode and consensus settings
- Add inline comment in generated code explaining the threshold
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -258,7 +258,7 @@ print(result["counts_df"])
|
|
| 258 |
'''
|
| 259 |
|
| 260 |
|
| 261 |
-
def generate_classify_code(input_type, description, categories, model, model_source, mode=None, classify_mode="Single Model", models_list=None):
|
| 262 |
"""Generate Python code for classification."""
|
| 263 |
categories_str = ",\n ".join([f'"{cat}"' for cat in categories])
|
| 264 |
|
|
@@ -309,7 +309,7 @@ result.to_csv("classified_results.csv", index=False)
|
|
| 309 |
models_str = '("gpt-4o", "auto", "YOUR_API_KEY"),\n ("claude-sonnet-4-5-20250929", "auto", "YOUR_API_KEY")'
|
| 310 |
|
| 311 |
mode_param = f',\n mode="{mode}"' if mode and input_type == "pdf" else ''
|
| 312 |
-
consensus_param = ',\n consensus_threshold=0.5' if classify_mode == "Ensemble" else ''
|
| 313 |
|
| 314 |
return f'''import catllm
|
| 315 |
{load_data}
|
|
@@ -1339,16 +1339,21 @@ with col_input:
|
|
| 1339 |
api_key = st.text_input("API Key", type="password", key="classify_api_key")
|
| 1340 |
|
| 1341 |
# Ensemble-specific options
|
|
|
|
| 1342 |
if classify_mode == "Ensemble":
|
| 1343 |
-
|
| 1344 |
-
"
|
| 1345 |
-
|
| 1346 |
-
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
| 1350 |
-
|
|
|
|
|
|
|
|
|
|
| 1351 |
)
|
|
|
|
| 1352 |
|
| 1353 |
if st.button("Categorize Data", type="primary", use_container_width=True):
|
| 1354 |
if input_data is None:
|
|
@@ -1529,7 +1534,8 @@ with col_input:
|
|
| 1529 |
code = generate_classify_code(
|
| 1530 |
input_type_selected, description, categories_entered,
|
| 1531 |
report_model, report_model_source, mode,
|
| 1532 |
-
classify_mode=classify_mode, models_list=models_list
|
|
|
|
| 1533 |
)
|
| 1534 |
|
| 1535 |
# Generate methodology report with code included
|
|
@@ -1650,14 +1656,32 @@ if st.session_state.get('show_code_modal'):
|
|
| 1650 |
current_input_type = input_type_map.get(st.session_state.get('input_type_radio', 'Survey Responses'), 'text')
|
| 1651 |
current_description = st.session_state.get('survey_column', '') or st.session_state.get('pdf_desc', '') or st.session_state.get('image_desc', '') or 'your_data'
|
| 1652 |
|
| 1653 |
-
# Get current
|
| 1654 |
-
|
| 1655 |
-
|
| 1656 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1657 |
else:
|
| 1658 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1659 |
|
| 1660 |
-
|
|
|
|
|
|
|
| 1661 |
|
| 1662 |
# Get PDF mode if applicable
|
| 1663 |
current_mode = None
|
|
@@ -1672,7 +1696,9 @@ if st.session_state.get('show_code_modal'):
|
|
| 1672 |
if current_categories:
|
| 1673 |
code_to_show = generate_classify_code(
|
| 1674 |
current_input_type, current_description, current_categories,
|
| 1675 |
-
current_model, current_model_source, current_mode
|
|
|
|
|
|
|
| 1676 |
)
|
| 1677 |
else:
|
| 1678 |
code_to_show = '''import catllm
|
|
|
|
| 258 |
'''
|
| 259 |
|
| 260 |
|
| 261 |
+
def generate_classify_code(input_type, description, categories, model, model_source, mode=None, classify_mode="Single Model", models_list=None, consensus_threshold=0.5):
|
| 262 |
"""Generate Python code for classification."""
|
| 263 |
categories_str = ",\n ".join([f'"{cat}"' for cat in categories])
|
| 264 |
|
|
|
|
| 309 |
models_str = '("gpt-4o", "auto", "YOUR_API_KEY"),\n ("claude-sonnet-4-5-20250929", "auto", "YOUR_API_KEY")'
|
| 310 |
|
| 311 |
mode_param = f',\n mode="{mode}"' if mode and input_type == "pdf" else ''
|
| 312 |
+
consensus_param = f',\n consensus_threshold={consensus_threshold} # {"Majority" if consensus_threshold == 0.5 else "Two-Thirds" if consensus_threshold == 0.67 else "Unanimous"}' if classify_mode == "Ensemble" else ''
|
| 313 |
|
| 314 |
return f'''import catllm
|
| 315 |
{load_data}
|
|
|
|
| 1339 |
api_key = st.text_input("API Key", type="password", key="classify_api_key")
|
| 1340 |
|
| 1341 |
# Ensemble-specific options
|
| 1342 |
+
consensus_threshold = 0.5 # Default
|
| 1343 |
if classify_mode == "Ensemble":
|
| 1344 |
+
consensus_options = {
|
| 1345 |
+
"Majority (50%+)": 0.5,
|
| 1346 |
+
"Two-Thirds (67%+)": 0.67,
|
| 1347 |
+
"Unanimous (100%)": 1.0,
|
| 1348 |
+
}
|
| 1349 |
+
consensus_choice = st.radio(
|
| 1350 |
+
"Consensus Rule",
|
| 1351 |
+
options=list(consensus_options.keys()),
|
| 1352 |
+
horizontal=True,
|
| 1353 |
+
key="consensus_choice",
|
| 1354 |
+
help="How many models must agree for a category to be marked present"
|
| 1355 |
)
|
| 1356 |
+
consensus_threshold = consensus_options[consensus_choice]
|
| 1357 |
|
| 1358 |
if st.button("Categorize Data", type="primary", use_container_width=True):
|
| 1359 |
if input_data is None:
|
|
|
|
| 1534 |
code = generate_classify_code(
|
| 1535 |
input_type_selected, description, categories_entered,
|
| 1536 |
report_model, report_model_source, mode,
|
| 1537 |
+
classify_mode=classify_mode, models_list=models_list,
|
| 1538 |
+
consensus_threshold=consensus_threshold
|
| 1539 |
)
|
| 1540 |
|
| 1541 |
# Generate methodology report with code included
|
|
|
|
| 1656 |
current_input_type = input_type_map.get(st.session_state.get('input_type_radio', 'Survey Responses'), 'text')
|
| 1657 |
current_description = st.session_state.get('survey_column', '') or st.session_state.get('pdf_desc', '') or st.session_state.get('image_desc', '') or 'your_data'
|
| 1658 |
|
| 1659 |
+
# Get current classification mode and models
|
| 1660 |
+
current_classify_mode = st.session_state.get('classify_mode', 'Single Model')
|
| 1661 |
+
current_model_tier = st.session_state.get('classify_model_tier', 'Free Models')
|
| 1662 |
+
|
| 1663 |
+
if current_classify_mode in ["Model Comparison", "Ensemble"]:
|
| 1664 |
+
# Multi-model mode
|
| 1665 |
+
if current_model_tier == 'Free Models':
|
| 1666 |
+
model_displays = st.session_state.get('classify_models_multi', [])
|
| 1667 |
+
current_models_list = [FREE_MODELS_MAP.get(d, d) for d in model_displays]
|
| 1668 |
+
else:
|
| 1669 |
+
current_models_list = st.session_state.get('classify_models_multi_paid', [])
|
| 1670 |
+
current_model = ", ".join(current_models_list) if current_models_list else "gpt-4o-mini"
|
| 1671 |
+
current_model_source = f"{current_classify_mode} ({len(current_models_list)} models)"
|
| 1672 |
else:
|
| 1673 |
+
# Single model mode
|
| 1674 |
+
if current_model_tier == 'Free Models':
|
| 1675 |
+
model_display = st.session_state.get('classify_model', 'GPT-4o Mini')
|
| 1676 |
+
current_model = FREE_MODELS_MAP.get(model_display, 'gpt-4o-mini')
|
| 1677 |
+
else:
|
| 1678 |
+
current_model = st.session_state.get('classify_model_paid', 'gpt-4o-mini')
|
| 1679 |
+
current_models_list = [current_model]
|
| 1680 |
+
current_model_source = get_model_source(current_model)
|
| 1681 |
|
| 1682 |
+
# Get consensus threshold for ensemble mode
|
| 1683 |
+
consensus_options = {"Majority (50%+)": 0.5, "Two-Thirds (67%+)": 0.67, "Unanimous (100%)": 1.0}
|
| 1684 |
+
current_consensus = consensus_options.get(st.session_state.get('consensus_choice', 'Majority (50%+)'), 0.5)
|
| 1685 |
|
| 1686 |
# Get PDF mode if applicable
|
| 1687 |
current_mode = None
|
|
|
|
| 1696 |
if current_categories:
|
| 1697 |
code_to_show = generate_classify_code(
|
| 1698 |
current_input_type, current_description, current_categories,
|
| 1699 |
+
current_model, current_model_source, current_mode,
|
| 1700 |
+
classify_mode=current_classify_mode, models_list=current_models_list,
|
| 1701 |
+
consensus_threshold=current_consensus
|
| 1702 |
)
|
| 1703 |
else:
|
| 1704 |
code_to_show = '''import catllm
|