jrrribeiro commited on
Commit
65f7caa
·
verified ·
1 Parent(s): 2f156bc

Sync app from GitHub Actions (feature/project-state-security-privacy-review)

Browse files
src/ui/app_factory.py CHANGED
@@ -62,8 +62,8 @@ VALIDATION_CONFIRM_LABEL = "↑ Confirm"
62
  VALIDATION_REJECT_LABEL = "↓ Reject"
63
  VALIDATION_UNCERTAIN_LABEL = "← Uncertain"
64
  VALIDATION_SKIP_LABEL = "→ Skip"
65
- VALIDATION_FAVORITE_LABEL = "⌫ Favorite"
66
- VALIDATION_FAVORITED_LABEL = "⌫ Favorited"
67
 
68
 
69
  class _AudioFetchResultProtocol(Protocol):
@@ -1666,13 +1666,12 @@ def _validation_shortcuts_script() -> str:
1666
  ArrowDown: "bn-validate-reject-btn",
1667
  ArrowLeft: "bn-validate-uncertain-btn",
1668
  ArrowRight: "bn-validate-skip-btn",
1669
- Backspace: "bn-validate-favorite-btn",
1670
  };
1671
 
1672
  document.addEventListener("keydown", (event) => {
1673
  if (event.repeat || event.altKey || event.ctrlKey || event.metaKey) return;
1674
  if (isTypingTarget(event.target)) return;
1675
- const buttonId = actionByKey[event.key];
1676
  if (!buttonId || !validateTabIsActive()) return;
1677
  event.preventDefault();
1678
  clickButtonById(buttonId);
@@ -1889,7 +1888,7 @@ def _page_to_table(
1889
  if show_conflicts_only:
1890
  rows = [row for row in rows if str(row[8]) == "CONFLICT"]
1891
 
1892
- rows = _sort_rows_by_confidence_desc(rows)
1893
  filtered_total = len(rows)
1894
  total_pages = max(1, ((filtered_total - 1) // page_size) + 1) if filtered_total else 1
1895
  safe_page = max(1, min(int(page), total_pages))
@@ -2487,8 +2486,31 @@ def _extract_species_options_from_queue(
2487
  return sorted(species_set)
2488
 
2489
 
2490
- def _sort_rows_by_confidence_desc(rows: list[list[object]]) -> list[list[object]]:
2491
- return sorted(rows, key=lambda row: float(row[3]) if len(row) > 3 else 0.0, reverse=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2492
 
2493
 
2494
  def _select_and_fetch_audio_with_title(
@@ -2578,12 +2600,16 @@ def _advance_to_next_row_with_title(
2578
 
2579
 
2580
  def _first_pending_row_index(rows: object) -> int:
 
 
 
 
 
2581
  normalized_rows = _normalize_rows(rows)
2582
  for index, row in enumerate(normalized_rows):
2583
- status_value = str(row[6] if len(row) > 6 else "pending").strip().lower()
2584
- if status_value in {"", "pending"}:
2585
  return index
2586
- return 0
2587
 
2588
 
2589
  def _first_pending_queue_page(
@@ -2622,12 +2648,10 @@ def _first_pending_queue_page(
2622
  return None
2623
  visited_pages.add(actual_page)
2624
 
2625
- page_rows = _sort_rows_by_confidence_desc(page_rows)
2626
- pending_index = _first_pending_row_index(page_rows)
2627
- if page_rows:
2628
- status_value = str(page_rows[pending_index][6] if len(page_rows[pending_index]) > 6 else "pending").strip().lower()
2629
- if status_value in {"", "pending"}:
2630
- return page_rows, actual_page, pending_index
2631
 
2632
  requested_page = actual_page + 1
2633
 
@@ -2742,7 +2766,7 @@ def _save_selected_validation_with_refresh(
2742
  show_conflicts_only=show_conflicts_only,
2743
  actor_username=validator,
2744
  )
2745
- refreshed_rows = _sort_rows_by_confidence_desc(refreshed_rows)
2746
 
2747
  if selected_key:
2748
  refreshed_index = _post_validation_queue_anchor(refreshed_rows, selected_key, selected_index)
@@ -2765,14 +2789,22 @@ def _save_selected_validation_with_refresh(
2765
  show_conflicts_only=show_conflicts_only,
2766
  actor_username=validator,
2767
  )
2768
- refreshed_rows = _sort_rows_by_confidence_desc(refreshed_rows)
2769
  refreshed_index = _find_detection_row_index(refreshed_rows, selected_key) if selected_key else 0
2770
  pending_status_value = status_value
2771
  status = f"{save_status} Table reloaded to resolve conflict."
2772
  else:
2773
  conflict_key = ""
2774
  pending_status_value = ""
2775
- if selected_was_last_row:
 
 
 
 
 
 
 
 
2776
  first_pending_page = _first_pending_queue_page(
2777
  queue_service=queue_service,
2778
  snapshot_reader=snapshot_reader,
@@ -2788,6 +2820,25 @@ def _save_selected_validation_with_refresh(
2788
  if first_pending_page is not None:
2789
  refreshed_rows, refreshed_page, pending_index = first_pending_page
2790
  refreshed_index = pending_index - 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2791
  status = f"{save_status} | {page_status}"
2792
 
2793
  return (
@@ -2837,7 +2888,7 @@ def _reapply_last_conflict_validation_with_refresh(
2837
  show_conflicts_only=show_conflicts_only,
2838
  actor_username=validator,
2839
  )
2840
- refreshed_rows = _sort_rows_by_confidence_desc(refreshed_rows)
2841
  return (
2842
  f"No pending validation to reapply | {page_status}",
2843
  cache_key,
@@ -2946,7 +2997,7 @@ def _batch_validate_conflicts(
2946
  show_conflicts_only=False,
2947
  actor_username=validator_name,
2948
  )
2949
- refreshed_rows = _sort_rows_by_confidence_desc(refreshed_rows)
2950
 
2951
  summary = f"Processed {len(conflict_rows)} conflicts: {success_count} success, {conflict_count} new conflicts, {failure_count} failures"
2952
  status = f"{summary} | {page_status}"
@@ -5158,7 +5209,7 @@ def create_app() -> gr.Blocks:
5158
  )
5159
  except HfBucketValidationError as exc:
5160
  return [], str(exc), 1
5161
- rows = _sort_rows_by_confidence_desc(rows)
5162
  return rows, status_text, updated_page
5163
 
5164
  def go_next(
 
62
  VALIDATION_REJECT_LABEL = "↓ Reject"
63
  VALIDATION_UNCERTAIN_LABEL = "← Uncertain"
64
  VALIDATION_SKIP_LABEL = "→ Skip"
65
+ VALIDATION_FAVORITE_LABEL = "␣ Favorite"
66
+ VALIDATION_FAVORITED_LABEL = "␣ Favorited"
67
 
68
 
69
  class _AudioFetchResultProtocol(Protocol):
 
1666
  ArrowDown: "bn-validate-reject-btn",
1667
  ArrowLeft: "bn-validate-uncertain-btn",
1668
  ArrowRight: "bn-validate-skip-btn",
 
1669
  };
1670
 
1671
  document.addEventListener("keydown", (event) => {
1672
  if (event.repeat || event.altKey || event.ctrlKey || event.metaKey) return;
1673
  if (isTypingTarget(event.target)) return;
1674
+ const buttonId = event.code === "Space" ? "bn-validate-favorite-btn" : actionByKey[event.key];
1675
  if (!buttonId || !validateTabIsActive()) return;
1676
  event.preventDefault();
1677
  clickButtonById(buttonId);
 
1888
  if show_conflicts_only:
1889
  rows = [row for row in rows if str(row[8]) == "CONFLICT"]
1890
 
1891
+ rows = _sort_rows_for_validation_queue(rows)
1892
  filtered_total = len(rows)
1893
  total_pages = max(1, ((filtered_total - 1) // page_size) + 1) if filtered_total else 1
1894
  safe_page = max(1, min(int(page), total_pages))
 
2486
  return sorted(species_set)
2487
 
2488
 
2489
+ def _row_confidence(row: list[object]) -> float:
2490
+ try:
2491
+ return float(row[3]) if len(row) > 3 else 0.0
2492
+ except Exception:
2493
+ return 0.0
2494
+
2495
+
2496
+ def _row_is_pending(row: list[object]) -> bool:
2497
+ status_value = str(row[6] if len(row) > 6 else "pending").strip().lower()
2498
+ return status_value in {"", "pending"}
2499
+
2500
+
2501
+ def _sort_rows_for_validation_queue(rows: list[list[object]]) -> list[list[object]]:
2502
+ """Prioritize unreviewed detections, keeping confidence-desc order within each group."""
2503
+ return [
2504
+ row
2505
+ for _, row in sorted(
2506
+ enumerate(rows),
2507
+ key=lambda item: (
2508
+ 0 if _row_is_pending(item[1]) else 1,
2509
+ -_row_confidence(item[1]),
2510
+ item[0],
2511
+ ),
2512
+ )
2513
+ ]
2514
 
2515
 
2516
  def _select_and_fetch_audio_with_title(
 
2600
 
2601
 
2602
  def _first_pending_row_index(rows: object) -> int:
2603
+ pending_index = _pending_row_index_or_none(rows)
2604
+ return pending_index if pending_index is not None else 0
2605
+
2606
+
2607
+ def _pending_row_index_or_none(rows: object) -> int | None:
2608
  normalized_rows = _normalize_rows(rows)
2609
  for index, row in enumerate(normalized_rows):
2610
+ if _row_is_pending(row):
 
2611
  return index
2612
+ return None
2613
 
2614
 
2615
  def _first_pending_queue_page(
 
2648
  return None
2649
  visited_pages.add(actual_page)
2650
 
2651
+ page_rows = _sort_rows_for_validation_queue(page_rows)
2652
+ pending_index = _pending_row_index_or_none(page_rows)
2653
+ if pending_index is not None:
2654
+ return page_rows, actual_page, pending_index
 
 
2655
 
2656
  requested_page = actual_page + 1
2657
 
 
2766
  show_conflicts_only=show_conflicts_only,
2767
  actor_username=validator,
2768
  )
2769
+ refreshed_rows = _sort_rows_for_validation_queue(refreshed_rows)
2770
 
2771
  if selected_key:
2772
  refreshed_index = _post_validation_queue_anchor(refreshed_rows, selected_key, selected_index)
 
2789
  show_conflicts_only=show_conflicts_only,
2790
  actor_username=validator,
2791
  )
2792
+ refreshed_rows = _sort_rows_for_validation_queue(refreshed_rows)
2793
  refreshed_index = _find_detection_row_index(refreshed_rows, selected_key) if selected_key else 0
2794
  pending_status_value = status_value
2795
  status = f"{save_status} Table reloaded to resolve conflict."
2796
  else:
2797
  conflict_key = ""
2798
  pending_status_value = ""
2799
+ normalized_status_filter = (status_filter or "all").strip().lower()
2800
+ pending_index = (
2801
+ _pending_row_index_or_none(refreshed_rows)
2802
+ if normalized_status_filter in {"", "all", "pending"} and int(refreshed_page) <= 1
2803
+ else None
2804
+ )
2805
+ if pending_index is not None:
2806
+ refreshed_index = pending_index - 1
2807
+ elif normalized_status_filter in {"", "all", "pending"}:
2808
  first_pending_page = _first_pending_queue_page(
2809
  queue_service=queue_service,
2810
  snapshot_reader=snapshot_reader,
 
2820
  if first_pending_page is not None:
2821
  refreshed_rows, refreshed_page, pending_index = first_pending_page
2822
  refreshed_index = pending_index - 1
2823
+ else:
2824
+ if refreshed_page != 1:
2825
+ refreshed_rows, page_status, refreshed_page = _page_to_table(
2826
+ service=queue_service,
2827
+ snapshot_reader=snapshot_reader,
2828
+ project_slug=project_slug,
2829
+ page=1,
2830
+ scientific_name=scientific_name,
2831
+ min_confidence=min_confidence,
2832
+ validator_filter=validator_filter,
2833
+ status_filter=status_filter,
2834
+ updated_after=updated_after,
2835
+ show_conflicts_only=show_conflicts_only,
2836
+ actor_username=validator,
2837
+ )
2838
+ refreshed_rows = _sort_rows_for_validation_queue(refreshed_rows)
2839
+ refreshed_index = -1
2840
+ elif selected_was_last_row:
2841
+ refreshed_index = -1
2842
  status = f"{save_status} | {page_status}"
2843
 
2844
  return (
 
2888
  show_conflicts_only=show_conflicts_only,
2889
  actor_username=validator,
2890
  )
2891
+ refreshed_rows = _sort_rows_for_validation_queue(refreshed_rows)
2892
  return (
2893
  f"No pending validation to reapply | {page_status}",
2894
  cache_key,
 
2997
  show_conflicts_only=False,
2998
  actor_username=validator_name,
2999
  )
3000
+ refreshed_rows = _sort_rows_for_validation_queue(refreshed_rows)
3001
 
3002
  summary = f"Processed {len(conflict_rows)} conflicts: {success_count} success, {conflict_count} new conflicts, {failure_count} failures"
3003
  status = f"{summary} | {page_status}"
 
5209
  )
5210
  except HfBucketValidationError as exc:
5211
  return [], str(exc), 1
5212
+ rows = _sort_rows_for_validation_queue(rows)
5213
  return rows, status_text, updated_page
5214
 
5215
  def go_next(
tests/integration/test_validation_flow_integration.py CHANGED
@@ -96,8 +96,11 @@ def test_validation_write_refreshes_table_and_snapshot(tmp_path: Path) -> None:
96
  assert refreshed_page == 1
97
  assert pending_status == ""
98
  assert conflict_key == ""
99
- assert refreshed_rows[0][6] == "positive"
100
- assert refreshed_rows[0][7] == 1
 
 
 
101
  assert audio_service.cleaned == ["cache:audio_001"]
102
 
103
  events = validation_repo.list_events(project_slug)
 
96
  assert refreshed_page == 1
97
  assert pending_status == ""
98
  assert conflict_key == ""
99
+ assert refreshed_rows[0][0] == "0000000000000002"
100
+ assert refreshed_rows[0][6] == "pending"
101
+ assert refreshed_rows[1][0] == "0000000000000001"
102
+ assert refreshed_rows[1][6] == "positive"
103
+ assert refreshed_rows[1][7] == 1
104
  assert audio_service.cleaned == ["cache:audio_001"]
105
 
106
  events = validation_repo.list_events(project_slug)
tests/unit/test_app_factory_audio_helpers.py CHANGED
@@ -674,6 +674,71 @@ def test_page_to_table_includes_validation_status() -> None:
674
  assert queue.last_kwargs["project_slug"] == "kenya-2024"
675
 
676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677
  def test_page_to_table_marks_conflict_row() -> None:
678
  rows, _, _ = _page_to_table(
679
  service=FakeQueueService(),
@@ -896,12 +961,85 @@ def test_save_selected_validation_with_refresh_success() -> None:
896
  assert cache_key == ""
897
  assert audio_path is None
898
  assert refreshed_page == 1
899
- assert refreshed_index == 0
900
  assert refreshed_rows[0][0] == "dkey_01"
901
  assert pending_status == ""
902
  assert conflict_key == ""
903
 
904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905
  def test_save_selected_validation_with_refresh_conflict() -> None:
906
  audio_service = FakeAudioService()
907
  validation_service = FakeConflictValidationService()
@@ -972,7 +1110,7 @@ def test_reapply_last_conflict_validation_with_refresh() -> None:
972
  assert cache_key == ""
973
  assert audio_path is None
974
  assert refreshed_page == 1
975
- assert refreshed_index == 0
976
  assert refreshed_rows[0][0] == "dkey_01"
977
  assert pending_status == ""
978
  assert conflict_key == ""
@@ -1028,7 +1166,8 @@ def test_validation_shortcuts_are_validate_tab_scoped() -> None:
1028
  assert "ArrowDown" in script
1029
  assert "ArrowLeft" in script
1030
  assert "ArrowRight" in script
1031
- assert "Backspace" in script
 
1032
  assert "bn-validate-confirm-btn" in script
1033
  assert "bn-validate-reject-btn" in script
1034
  assert "bn-validate-uncertain-btn" in script
 
674
  assert queue.last_kwargs["project_slug"] == "kenya-2024"
675
 
676
 
677
+ def test_page_to_table_prioritizes_pending_then_confidence() -> None:
678
+ class QueueWithMixedStatus:
679
+ def list_all_detections(self, **kwargs: object) -> list[object]:
680
+ _ = kwargs
681
+ return [
682
+ SimpleNamespace(
683
+ detection_key="reviewed_high",
684
+ audio_id="reviewed_high.wav",
685
+ scientific_name="sp",
686
+ confidence=0.99,
687
+ start_time=0,
688
+ end_time=1,
689
+ ),
690
+ SimpleNamespace(
691
+ detection_key="pending_mid",
692
+ audio_id="pending_mid.wav",
693
+ scientific_name="sp",
694
+ confidence=0.82,
695
+ start_time=1,
696
+ end_time=2,
697
+ ),
698
+ SimpleNamespace(
699
+ detection_key="pending_low",
700
+ audio_id="pending_low.wav",
701
+ scientific_name="sp",
702
+ confidence=0.61,
703
+ start_time=2,
704
+ end_time=3,
705
+ ),
706
+ SimpleNamespace(
707
+ detection_key="reviewed_low",
708
+ audio_id="reviewed_low.wav",
709
+ scientific_name="sp",
710
+ confidence=0.55,
711
+ start_time=3,
712
+ end_time=4,
713
+ ),
714
+ ]
715
+
716
+ class SnapshotWithMixedStatus:
717
+ def load_current_snapshot(self, project_slug: str, actor_username: str = "") -> dict[str, dict[str, object]]:
718
+ _ = (project_slug, actor_username)
719
+ return {
720
+ "reviewed_high": {"status": "positive", "version": 1},
721
+ "reviewed_low": {"status": "negative", "version": 1},
722
+ }
723
+
724
+ rows, _, _ = _page_to_table(
725
+ service=QueueWithMixedStatus(),
726
+ snapshot_reader=SnapshotWithMixedStatus(),
727
+ project_slug="demo-project",
728
+ page=1,
729
+ scientific_name="sp",
730
+ min_confidence=0.0,
731
+ page_size=10,
732
+ )
733
+
734
+ assert [row[0] for row in rows] == [
735
+ "pending_mid",
736
+ "pending_low",
737
+ "reviewed_high",
738
+ "reviewed_low",
739
+ ]
740
+
741
+
742
  def test_page_to_table_marks_conflict_row() -> None:
743
  rows, _, _ = _page_to_table(
744
  service=FakeQueueService(),
 
961
  assert cache_key == ""
962
  assert audio_path is None
963
  assert refreshed_page == 1
964
+ assert refreshed_index == -1
965
  assert refreshed_rows[0][0] == "dkey_01"
966
  assert pending_status == ""
967
  assert conflict_key == ""
968
 
969
 
970
+ def test_save_selected_validation_advances_to_highest_confidence_pending_row() -> None:
971
+ class QueueAfterSave:
972
+ def list_all_detections(self, **kwargs: object) -> list[object]:
973
+ _ = kwargs
974
+ return [
975
+ SimpleNamespace(
976
+ detection_key="dkey_selected",
977
+ audio_id="audio_selected",
978
+ scientific_name="sp",
979
+ confidence=0.99,
980
+ start_time=0,
981
+ end_time=1,
982
+ ),
983
+ SimpleNamespace(
984
+ detection_key="dkey_pending_high",
985
+ audio_id="audio_pending_high",
986
+ scientific_name="sp",
987
+ confidence=0.95,
988
+ start_time=1,
989
+ end_time=2,
990
+ ),
991
+ SimpleNamespace(
992
+ detection_key="dkey_pending_low",
993
+ audio_id="audio_pending_low",
994
+ scientific_name="sp",
995
+ confidence=0.60,
996
+ start_time=2,
997
+ end_time=3,
998
+ ),
999
+ ]
1000
+
1001
+ class SnapshotAfterSave:
1002
+ def load_current_snapshot(self, project_slug: str, actor_username: str = "") -> dict[str, dict[str, object]]:
1003
+ _ = (project_slug, actor_username)
1004
+ return {"dkey_selected": {"status": "positive", "version": 1}}
1005
+
1006
+ audio_service = FakeAudioService()
1007
+ status, cache_key, _, refreshed_rows, _, refreshed_index, _, _ = _save_selected_validation_with_refresh(
1008
+ validation_service=FakeValidationService(),
1009
+ audio_service=audio_service,
1010
+ queue_service=QueueAfterSave(),
1011
+ snapshot_reader=SnapshotAfterSave(),
1012
+ project_slug="demo-project",
1013
+ rows=[["dkey_selected", "audio_selected", "sp", 0.99, 0.0, 1.0, "pending", 0]],
1014
+ selected_index=0,
1015
+ status_value="positive",
1016
+ validator="validator-demo",
1017
+ notes="ok",
1018
+ cache_key="cache:audio_selected",
1019
+ page=1,
1020
+ scientific_name="sp",
1021
+ min_confidence=0.0,
1022
+ validator_filter="",
1023
+ status_filter="all",
1024
+ updated_after="",
1025
+ show_conflicts_only=False,
1026
+ )
1027
+
1028
+ selected_index, audio_path, updated_cache_key, _, _, _ = _advance_to_next_row_with_title(
1029
+ audio_service=audio_service,
1030
+ dataset_repo="org/dataset",
1031
+ rows=refreshed_rows,
1032
+ selected_index=refreshed_index,
1033
+ cache_key=cache_key,
1034
+ )
1035
+
1036
+ assert "Validation saved" in status
1037
+ assert [row[0] for row in refreshed_rows] == ["dkey_pending_high", "dkey_pending_low", "dkey_selected"]
1038
+ assert selected_index == 0
1039
+ assert audio_path == "/tmp/audio_pending_high.wav"
1040
+ assert updated_cache_key == "key:audio_pending_high"
1041
+
1042
+
1043
  def test_save_selected_validation_with_refresh_conflict() -> None:
1044
  audio_service = FakeAudioService()
1045
  validation_service = FakeConflictValidationService()
 
1110
  assert cache_key == ""
1111
  assert audio_path is None
1112
  assert refreshed_page == 1
1113
+ assert refreshed_index == -1
1114
  assert refreshed_rows[0][0] == "dkey_01"
1115
  assert pending_status == ""
1116
  assert conflict_key == ""
 
1166
  assert "ArrowDown" in script
1167
  assert "ArrowLeft" in script
1168
  assert "ArrowRight" in script
1169
+ assert 'event.code === "Space"' in script
1170
+ assert "Backspace" not in script
1171
  assert "bn-validate-confirm-btn" in script
1172
  assert "bn-validate-reject-btn" in script
1173
  assert "bn-validate-uncertain-btn" in script