version 1.8.16Xc, comparing Manage Q&A features with version version 1.8.16Xd
Browse files- src/kmai.py +44 -92
src/kmai.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
# version 1.8.
|
| 2 |
-
# Updated Panel 2: Filter & Sort cached Q&A (separate, collapsible)
|
| 3 |
# (Updated to add separate "Filter & Sort cached Q&A by metric values" panel per topic — Option A)
|
|
|
|
| 4 |
|
| 5 |
import os
|
| 6 |
import io
|
|
@@ -1328,8 +1328,7 @@ with tab3:
|
|
| 1328 |
# Panel 2: Filter & Sort cached Q&A (separate, collapsible)
|
| 1329 |
# -----------------------
|
| 1330 |
if st.session_state.manage_filter_open.get(t, False):
|
| 1331 |
-
with st.expander(f"Filter & Sort cached Q&A by metric values — Topic: {t}", expanded=
|
| 1332 |
-
|
| 1333 |
rows = fetch_cache_by_topic(t, only_helpful=False)
|
| 1334 |
if not rows:
|
| 1335 |
st.info("No cached Q&A for this topic.")
|
|
@@ -1354,121 +1353,74 @@ with tab3:
|
|
| 1354 |
# --- Filtering controls for metrics (Feature: sorting/filtering)
|
| 1355 |
st.markdown("**Filter controls**")
|
| 1356 |
filter_cols = st.columns(4)
|
| 1357 |
-
|
| 1358 |
enable_cos = filter_cols[0].checkbox("Filter Cosine", value=False, key=f"f_cos_{t}_filterpanel")
|
| 1359 |
-
enable_r2
|
| 1360 |
-
enable_rL
|
| 1361 |
|
| 1362 |
-
|
|
|
|
| 1363 |
if enabled:
|
| 1364 |
-
|
| 1365 |
-
|
| 1366 |
-
|
| 1367 |
-
|
| 1368 |
-
value=(0.0, 1.0),
|
| 1369 |
-
step=0.01,
|
| 1370 |
-
key=f"sl_{t}_{label}_filterpanel"
|
| 1371 |
-
)
|
| 1372 |
-
return None
|
| 1373 |
|
| 1374 |
cos_range = metric_slider("cosine_score", enable_cos)
|
| 1375 |
-
r2_range
|
| 1376 |
-
rL_range
|
| 1377 |
|
| 1378 |
# Sorting controls
|
| 1379 |
-
sort_col = st.selectbox(
|
| 1380 |
-
|
| 1381 |
-
options=["created_at", "cosine_score", "rouge2_score", "rougeL_score", "feedback_status"],
|
| 1382 |
-
index=0,
|
| 1383 |
-
key=f"sort_{t}_filterpanel"
|
| 1384 |
-
)
|
| 1385 |
-
sort_dir = st.selectbox(
|
| 1386 |
-
"Sort direction:",
|
| 1387 |
-
options=["descending", "ascending"],
|
| 1388 |
-
index=0,
|
| 1389 |
-
key=f"sortdir_{t}_filterpanel"
|
| 1390 |
-
)
|
| 1391 |
|
| 1392 |
# Apply filters
|
| 1393 |
df_shown = df_cache.copy()
|
|
|
|
| 1394 |
for mc in ["cosine_score", "rouge2_score", "rougeL_score"]:
|
| 1395 |
df_shown[mc] = pd.to_numeric(df_shown[mc], errors="coerce")
|
| 1396 |
|
| 1397 |
if cos_range:
|
| 1398 |
-
df_shown = df_shown[
|
| 1399 |
-
(df_shown["cosine_score"].fillna(-1) >= cos_range[0]) &
|
| 1400 |
-
(df_shown["cosine_score"].fillna(-1) <= cos_range[1])
|
| 1401 |
-
]
|
| 1402 |
if r2_range:
|
| 1403 |
-
df_shown = df_shown[
|
| 1404 |
-
(df_shown["rouge2_score"].fillna(-1) >= r2_range[0]) &
|
| 1405 |
-
(df_shown["rouge2_score"].fillna(-1) <= r2_range[1])
|
| 1406 |
-
]
|
| 1407 |
if rL_range:
|
| 1408 |
-
df_shown = df_shown[
|
| 1409 |
-
(df_shown["rougeL_score"].fillna(-1) >= rL_range[0]) &
|
| 1410 |
-
(df_shown["rougeL_score"].fillna(-1) <= rL_range[1])
|
| 1411 |
-
]
|
| 1412 |
|
| 1413 |
# Sort
|
| 1414 |
-
ascending =
|
| 1415 |
if sort_col in df_shown.columns:
|
| 1416 |
-
|
| 1417 |
-
|
| 1418 |
-
ascending=ascending,
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
|
| 1422 |
-
#
|
| 1423 |
st.markdown("**Download / Export**")
|
|
|
|
| 1424 |
df_shown["topic"] = t
|
| 1425 |
-
export_df = df_shown[
|
| 1426 |
-
["id", "topic", "sub_topic", "created_at", "feedback_status",
|
| 1427 |
-
"question", "answer", "cosine_score", "rouge2_score", "rougeL_score"]
|
| 1428 |
-
]
|
| 1429 |
csv_buffer = export_df.to_csv(index=False)
|
| 1430 |
-
st.download_button(
|
| 1431 |
-
|
| 1432 |
-
|
| 1433 |
-
file_name=f"cached_qna_{t}.csv",
|
| 1434 |
-
mime="text/csv",
|
| 1435 |
-
key=f"dl_{t}_filterpanel"
|
| 1436 |
-
)
|
| 1437 |
-
|
| 1438 |
-
# --- Badge showing number of filtered rows ---
|
| 1439 |
-
row_count = len(df_shown)
|
| 1440 |
-
st.markdown(f"### 🟦 Filtered Rows: **{row_count}**")
|
| 1441 |
-
|
| 1442 |
-
# Display table
|
| 1443 |
st.markdown("**Cached Q&A (filtered view)**")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1444 |
|
| 1445 |
-
|
|
|
|
| 1446 |
st.info("No cached records match the selected filter criteria.")
|
| 1447 |
else:
|
| 1448 |
-
|
| 1449 |
-
|
| 1450 |
-
|
| 1451 |
-
)
|
| 1452 |
-
|
| 1453 |
-
|
| 1454 |
-
"id", "created_at", "sub_topic", "feedback_status",
|
| 1455 |
-
"cosine_score", "rouge2_score", "rougeL_score",
|
| 1456 |
-
"question", "answer_excerpt"
|
| 1457 |
-
]
|
| 1458 |
-
|
| 1459 |
-
# --- Fix: Prevent empty rows by using ONLY #rows × 40px ---
|
| 1460 |
-
table_height = 40 * row_count
|
| 1461 |
-
|
| 1462 |
-
st.dataframe(
|
| 1463 |
-
df_display[display_cols].reset_index(drop=True),
|
| 1464 |
-
use_container_width=True,
|
| 1465 |
-
height=table_height
|
| 1466 |
-
)
|
| 1467 |
-
|
| 1468 |
-
st.markdown(
|
| 1469 |
-
"**Note:** This filtered panel is read-only (no delete or feedback buttons). "
|
| 1470 |
-
"Use 'Manage Q&A' to operate on individual entries."
|
| 1471 |
-
)
|
| 1472 |
|
| 1473 |
st.markdown("---")
|
| 1474 |
# Clear caches and DB controls
|
|
|
|
| 1 |
+
# version 1.8.16Xc - extended to include metric fields in SQLite cache, UI download & sorting/filtering
|
|
|
|
| 2 |
# (Updated to add separate "Filter & Sort cached Q&A by metric values" panel per topic — Option A)
|
| 3 |
+
# this version is working
|
| 4 |
|
| 5 |
import os
|
| 6 |
import io
|
|
|
|
| 1328 |
# Panel 2: Filter & Sort cached Q&A (separate, collapsible)
|
| 1329 |
# -----------------------
|
| 1330 |
if st.session_state.manage_filter_open.get(t, False):
|
| 1331 |
+
with st.expander(f"Filter & Sort cached Q&A by metric values — Topic: {t}", expanded=True):
|
|
|
|
| 1332 |
rows = fetch_cache_by_topic(t, only_helpful=False)
|
| 1333 |
if not rows:
|
| 1334 |
st.info("No cached Q&A for this topic.")
|
|
|
|
| 1353 |
# --- Filtering controls for metrics (Feature: sorting/filtering)
|
| 1354 |
st.markdown("**Filter controls**")
|
| 1355 |
filter_cols = st.columns(4)
|
| 1356 |
+
# provide checkboxes to enable filtering by each metric
|
| 1357 |
enable_cos = filter_cols[0].checkbox("Filter Cosine", value=False, key=f"f_cos_{t}_filterpanel")
|
| 1358 |
+
enable_r2 = filter_cols[1].checkbox("Filter ROUGE-2", value=False, key=f"f_r2_{t}_filterpanel")
|
| 1359 |
+
enable_rL = filter_cols[2].checkbox("Filter ROUGE-L", value=False, key=f"f_rL_{t}_filterpanel")
|
| 1360 |
|
| 1361 |
+
# prepare safe min,max ranges: metrics are between 0 and 1 typically; use 0..1 sliders
|
| 1362 |
+
def metric_slider(col_name, enabled, default_min=0.0, default_max=1.0):
|
| 1363 |
if enabled:
|
| 1364 |
+
min_v, max_v = st.slider(f"{col_name} range", min_value=0.0, max_value=1.0, value=(0.0, 1.0), step=0.01, key=f"sl_{t}_{col_name}_filterpanel")
|
| 1365 |
+
return min_v, max_v
|
| 1366 |
+
else:
|
| 1367 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1368 |
|
| 1369 |
cos_range = metric_slider("cosine_score", enable_cos)
|
| 1370 |
+
r2_range = metric_slider("rouge2_score", enable_r2)
|
| 1371 |
+
rL_range = metric_slider("rougeL_score", enable_rL)
|
| 1372 |
|
| 1373 |
# Sorting controls
|
| 1374 |
+
sort_col = st.selectbox("Sort by column:", options=["created_at", "cosine_score", "rouge2_score", "rougeL_score", "feedback_status"], index=0, key=f"sort_{t}_filterpanel")
|
| 1375 |
+
sort_dir = st.selectbox("Sort direction:", options=["descending", "ascending"], index=0, key=f"sortdir_{t}_filterpanel")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1376 |
|
| 1377 |
# Apply filters
|
| 1378 |
df_shown = df_cache.copy()
|
| 1379 |
+
# convert metric columns to numeric (they may be None)
|
| 1380 |
for mc in ["cosine_score", "rouge2_score", "rougeL_score"]:
|
| 1381 |
df_shown[mc] = pd.to_numeric(df_shown[mc], errors="coerce")
|
| 1382 |
|
| 1383 |
if cos_range:
|
| 1384 |
+
df_shown = df_shown[(df_shown["cosine_score"].fillna(-1) >= cos_range[0]) & (df_shown["cosine_score"].fillna(-1) <= cos_range[1])]
|
|
|
|
|
|
|
|
|
|
| 1385 |
if r2_range:
|
| 1386 |
+
df_shown = df_shown[(df_shown["rouge2_score"].fillna(-1) >= r2_range[0]) & (df_shown["rouge2_score"].fillna(-1) <= r2_range[1])]
|
|
|
|
|
|
|
|
|
|
| 1387 |
if rL_range:
|
| 1388 |
+
df_shown = df_shown[(df_shown["rougeL_score"].fillna(-1) >= rL_range[0]) & (df_shown["rougeL_score"].fillna(-1) <= rL_range[1])]
|
|
|
|
|
|
|
|
|
|
| 1389 |
|
| 1390 |
# Sort
|
| 1391 |
+
ascending = True if sort_dir == "ascending" else False
|
| 1392 |
if sort_col in df_shown.columns:
|
| 1393 |
+
# Some columns may be strings (created_at): handle numeric vs datetime
|
| 1394 |
+
if sort_col in ["cosine_score", "rouge2_score", "rougeL_score"]:
|
| 1395 |
+
df_shown = df_shown.sort_values(by=sort_col, ascending=ascending, na_position='last')
|
| 1396 |
+
else:
|
| 1397 |
+
df_shown = df_shown.sort_values(by=sort_col, ascending=ascending)
|
| 1398 |
|
| 1399 |
+
# Small UI for CSV export (professional compact control)
|
| 1400 |
st.markdown("**Download / Export**")
|
| 1401 |
+
# Ensure 'topic' column present for context
|
| 1402 |
df_shown["topic"] = t
|
| 1403 |
+
export_df = df_shown[["id", "topic", "sub_topic", "created_at", "feedback_status", "question", "answer", "cosine_score", "rouge2_score", "rougeL_score"]]
|
|
|
|
|
|
|
|
|
|
| 1404 |
csv_buffer = export_df.to_csv(index=False)
|
| 1405 |
+
st.download_button(label="📥 Download filtered cached Q&A as CSV", data=csv_buffer, file_name=f"cached_qna_{t}.csv", mime="text/csv", key=f"dl_{t}_filterpanel")
|
| 1406 |
+
|
| 1407 |
+
# Display the filtered & sorted table (compact)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1408 |
st.markdown("**Cached Q&A (filtered view)**")
|
| 1409 |
+
# shorten answer column for table display to keep UI tidy
|
| 1410 |
+
df_display = df_shown.copy()
|
| 1411 |
+
df_display["answer_excerpt"] = df_display["answer"].str.slice(0, 500).str.replace("\n", " ")
|
| 1412 |
+
display_cols = ["id", "created_at", "sub_topic", "feedback_status", "cosine_score", "rouge2_score", "rougeL_score", "question", "answer_excerpt"]
|
| 1413 |
|
| 1414 |
+
# If df_display is empty, show a clear info message; otherwise display table with height based on rows
|
| 1415 |
+
if df_display.empty:
|
| 1416 |
st.info("No cached records match the selected filter criteria.")
|
| 1417 |
else:
|
| 1418 |
+
# dynamic height to avoid empty rows — 40px per row with min 160 and max 800
|
| 1419 |
+
row_count = len(df_display)
|
| 1420 |
+
table_height = max(160, min(800, 40 * row_count))
|
| 1421 |
+
st.dataframe(df_display[display_cols].reset_index(drop=True), use_container_width=True, height=table_height)
|
| 1422 |
+
|
| 1423 |
+
st.markdown("**Note:** This filtered panel is read-only (no delete or feedback buttons). Use 'Manage Q&A' to operate on individual entries.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1424 |
|
| 1425 |
st.markdown("---")
|
| 1426 |
# Clear caches and DB controls
|