Spaces:
Sleeping
Sleeping
Sync app from GitHub Actions (feature/project-state-security-privacy-review)
Browse files
src/ui/app_factory.py
CHANGED
|
@@ -58,6 +58,14 @@ from src.ui.components import admin_overview_html, compact_metric_grid, coverage
|
|
| 58 |
from src.ui.theme import APP_CSS, CRITICAL_HEAD_HTML, app_header_html
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
class _AudioFetchResultProtocol(Protocol):
|
| 62 |
cache_key: str
|
| 63 |
local_path: str
|
|
@@ -1611,6 +1619,69 @@ def _is_running_in_hf_space() -> bool:
|
|
| 1611 |
return bool((os.getenv("SPACE_ID") or os.getenv("SPACE_HOST") or "").strip())
|
| 1612 |
|
| 1613 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1614 |
def _validate_hf_admin_storage_source_dataset(
|
| 1615 |
*,
|
| 1616 |
project: Project,
|
|
@@ -4952,11 +5023,11 @@ def create_app() -> gr.Blocks:
|
|
| 4952 |
auto_play_audio = gr.Checkbox(label="Auto-play when selecting a row", value=True)
|
| 4953 |
|
| 4954 |
with gr.Row(elem_classes=["bn-action-row"]):
|
| 4955 |
-
approve_btn = gr.Button(
|
| 4956 |
-
reject_btn = gr.Button("
|
| 4957 |
-
uncertain_btn = gr.Button("
|
| 4958 |
-
skip_btn = gr.Button("
|
| 4959 |
-
favorite_btn = gr.Button(
|
| 4960 |
|
| 4961 |
corrected_species_input = gr.Dropdown(
|
| 4962 |
label="Corrected species",
|
|
@@ -5032,25 +5103,7 @@ def create_app() -> gr.Blocks:
|
|
| 5032 |
gr.Markdown("### Review details")
|
| 5033 |
validator_name = gr.Textbox(label="Validator", value="", interactive=False)
|
| 5034 |
validation_notes = gr.Textbox(label="Notes", placeholder="Optional", lines=4)
|
| 5035 |
-
keyboard_shortcuts_info = gr.HTML(
|
| 5036 |
-
value="<script>"
|
| 5037 |
-
"document.addEventListener('keydown', function(event) {"
|
| 5038 |
-
" if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') return;"
|
| 5039 |
-
" const key = event.key;"
|
| 5040 |
-
" let buttonText = null;"
|
| 5041 |
-
" if (key === 'ArrowUp' || key === '1') buttonText = 'Confirm';"
|
| 5042 |
-
" else if (key === 'ArrowDown' || key === '2') buttonText = 'Reject';"
|
| 5043 |
-
" else if (key === '3') buttonText = 'Uncertain';"
|
| 5044 |
-
" else if (key === '4') buttonText = 'Skip';"
|
| 5045 |
-
" if (!buttonText) return;"
|
| 5046 |
-
" event.preventDefault();"
|
| 5047 |
-
" const buttons = document.querySelectorAll('button');"
|
| 5048 |
-
" for (const btn of buttons) {"
|
| 5049 |
-
" if ((btn.textContent || '').includes(buttonText)) { btn.click(); break; }"
|
| 5050 |
-
" }"
|
| 5051 |
-
"});"
|
| 5052 |
-
"</script>"
|
| 5053 |
-
)
|
| 5054 |
|
| 5055 |
cache_key_state = gr.State(value="")
|
| 5056 |
pending_status_state = gr.State(value="")
|
|
@@ -5352,7 +5405,7 @@ def create_app() -> gr.Blocks:
|
|
| 5352 |
):
|
| 5353 |
normalized_rows = _normalize_rows(rows)
|
| 5354 |
if not project_slug or not normalized_rows:
|
| 5355 |
-
return "No detection selected to favorite", favorite_map, gr.update(value=
|
| 5356 |
|
| 5357 |
safe_idx = max(0, min(int(idx), len(normalized_rows) - 1))
|
| 5358 |
detection_key = str(normalized_rows[safe_idx][0]).strip()
|
|
@@ -5361,11 +5414,11 @@ def create_app() -> gr.Blocks:
|
|
| 5361 |
if detection_key in project_favs:
|
| 5362 |
project_favs.remove(detection_key)
|
| 5363 |
action = "removed from favorites"
|
| 5364 |
-
button_update = gr.update(value=
|
| 5365 |
else:
|
| 5366 |
project_favs.add(detection_key)
|
| 5367 |
action = "added to favorites"
|
| 5368 |
-
button_update = gr.update(value=
|
| 5369 |
updated_map[project_slug] = sorted(project_favs)
|
| 5370 |
return f"Detection {detection_key} {action}", updated_map, button_update
|
| 5371 |
|
|
@@ -5377,14 +5430,14 @@ def create_app() -> gr.Blocks:
|
|
| 5377 |
):
|
| 5378 |
normalized_rows = _normalize_rows(rows)
|
| 5379 |
if not project_slug or not normalized_rows:
|
| 5380 |
-
return gr.update(value=
|
| 5381 |
|
| 5382 |
safe_idx = max(0, min(int(idx), len(normalized_rows) - 1))
|
| 5383 |
detection_key = str(normalized_rows[safe_idx][0]).strip()
|
| 5384 |
favs = set((favorite_map or {}).get(project_slug, []))
|
| 5385 |
if detection_key in favs:
|
| 5386 |
-
return gr.update(value=
|
| 5387 |
-
return gr.update(value=
|
| 5388 |
|
| 5389 |
def on_table_select(project_slug: str, repo: str, rows: object, cache_key: str, session, evt: gr.SelectData):
|
| 5390 |
if not _can_access_project(session, project_slug):
|
|
|
|
| 58 |
from src.ui.theme import APP_CSS, CRITICAL_HEAD_HTML, app_header_html
|
| 59 |
|
| 60 |
|
| 61 |
+
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):
|
| 70 |
cache_key: str
|
| 71 |
local_path: str
|
|
|
|
| 1619 |
return bool((os.getenv("SPACE_ID") or os.getenv("SPACE_HOST") or "").strip())
|
| 1620 |
|
| 1621 |
|
| 1622 |
+
def _validation_shortcuts_script() -> str:
|
| 1623 |
+
"""Bind validation-only keyboard shortcuts to stable button IDs."""
|
| 1624 |
+
return """
|
| 1625 |
+
<script>
|
| 1626 |
+
(() => {
|
| 1627 |
+
if (window.__birdnetValidationShortcutsBound) return;
|
| 1628 |
+
window.__birdnetValidationShortcutsBound = true;
|
| 1629 |
+
|
| 1630 |
+
function isTypingTarget(target) {
|
| 1631 |
+
if (!target) return false;
|
| 1632 |
+
const tag = String(target.tagName || "").toUpperCase();
|
| 1633 |
+
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") return true;
|
| 1634 |
+
if (target.isContentEditable) return true;
|
| 1635 |
+
if (target.closest && target.closest('[contenteditable="true"], [role="textbox"], .cm-editor')) return true;
|
| 1636 |
+
return false;
|
| 1637 |
+
}
|
| 1638 |
+
|
| 1639 |
+
function isVisible(element) {
|
| 1640 |
+
if (!element) return false;
|
| 1641 |
+
const rects = element.getClientRects();
|
| 1642 |
+
if (!rects || rects.length === 0) return false;
|
| 1643 |
+
let node = element;
|
| 1644 |
+
while (node && node !== document.body) {
|
| 1645 |
+
const style = window.getComputedStyle(node);
|
| 1646 |
+
if (style.display === "none" || style.visibility === "hidden") return false;
|
| 1647 |
+
node = node.parentElement;
|
| 1648 |
+
}
|
| 1649 |
+
return true;
|
| 1650 |
+
}
|
| 1651 |
+
|
| 1652 |
+
function validateTabIsActive() {
|
| 1653 |
+
return isVisible(document.getElementById("bn-validation-queue-table"));
|
| 1654 |
+
}
|
| 1655 |
+
|
| 1656 |
+
function clickButtonById(id) {
|
| 1657 |
+
const root = document.getElementById(id);
|
| 1658 |
+
const button = root && root.matches("button") ? root : root?.querySelector("button");
|
| 1659 |
+
if (!button || button.disabled || button.getAttribute("aria-disabled") === "true") return false;
|
| 1660 |
+
button.click();
|
| 1661 |
+
return true;
|
| 1662 |
+
}
|
| 1663 |
+
|
| 1664 |
+
const actionByKey = {
|
| 1665 |
+
ArrowUp: "bn-validate-confirm-btn",
|
| 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);
|
| 1679 |
+
});
|
| 1680 |
+
})();
|
| 1681 |
+
</script>
|
| 1682 |
+
"""
|
| 1683 |
+
|
| 1684 |
+
|
| 1685 |
def _validate_hf_admin_storage_source_dataset(
|
| 1686 |
*,
|
| 1687 |
project: Project,
|
|
|
|
| 5023 |
auto_play_audio = gr.Checkbox(label="Auto-play when selecting a row", value=True)
|
| 5024 |
|
| 5025 |
with gr.Row(elem_classes=["bn-action-row"]):
|
| 5026 |
+
approve_btn = gr.Button(VALIDATION_CONFIRM_LABEL, variant="primary", elem_id="bn-validate-confirm-btn")
|
| 5027 |
+
reject_btn = gr.Button(VALIDATION_REJECT_LABEL, elem_id="bn-validate-reject-btn")
|
| 5028 |
+
uncertain_btn = gr.Button(VALIDATION_UNCERTAIN_LABEL, elem_id="bn-validate-uncertain-btn")
|
| 5029 |
+
skip_btn = gr.Button(VALIDATION_SKIP_LABEL, elem_id="bn-validate-skip-btn")
|
| 5030 |
+
favorite_btn = gr.Button(VALIDATION_FAVORITE_LABEL, variant="secondary", elem_id="bn-validate-favorite-btn")
|
| 5031 |
|
| 5032 |
corrected_species_input = gr.Dropdown(
|
| 5033 |
label="Corrected species",
|
|
|
|
| 5103 |
gr.Markdown("### Review details")
|
| 5104 |
validator_name = gr.Textbox(label="Validator", value="", interactive=False)
|
| 5105 |
validation_notes = gr.Textbox(label="Notes", placeholder="Optional", lines=4)
|
| 5106 |
+
keyboard_shortcuts_info = gr.HTML(value=_validation_shortcuts_script())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5107 |
|
| 5108 |
cache_key_state = gr.State(value="")
|
| 5109 |
pending_status_state = gr.State(value="")
|
|
|
|
| 5405 |
):
|
| 5406 |
normalized_rows = _normalize_rows(rows)
|
| 5407 |
if not project_slug or not normalized_rows:
|
| 5408 |
+
return "No detection selected to favorite", favorite_map, gr.update(value=VALIDATION_FAVORITE_LABEL, variant="secondary")
|
| 5409 |
|
| 5410 |
safe_idx = max(0, min(int(idx), len(normalized_rows) - 1))
|
| 5411 |
detection_key = str(normalized_rows[safe_idx][0]).strip()
|
|
|
|
| 5414 |
if detection_key in project_favs:
|
| 5415 |
project_favs.remove(detection_key)
|
| 5416 |
action = "removed from favorites"
|
| 5417 |
+
button_update = gr.update(value=VALIDATION_FAVORITE_LABEL, variant="secondary")
|
| 5418 |
else:
|
| 5419 |
project_favs.add(detection_key)
|
| 5420 |
action = "added to favorites"
|
| 5421 |
+
button_update = gr.update(value=VALIDATION_FAVORITED_LABEL, variant="primary")
|
| 5422 |
updated_map[project_slug] = sorted(project_favs)
|
| 5423 |
return f"Detection {detection_key} {action}", updated_map, button_update
|
| 5424 |
|
|
|
|
| 5430 |
):
|
| 5431 |
normalized_rows = _normalize_rows(rows)
|
| 5432 |
if not project_slug or not normalized_rows:
|
| 5433 |
+
return gr.update(value=VALIDATION_FAVORITE_LABEL, variant="secondary")
|
| 5434 |
|
| 5435 |
safe_idx = max(0, min(int(idx), len(normalized_rows) - 1))
|
| 5436 |
detection_key = str(normalized_rows[safe_idx][0]).strip()
|
| 5437 |
favs = set((favorite_map or {}).get(project_slug, []))
|
| 5438 |
if detection_key in favs:
|
| 5439 |
+
return gr.update(value=VALIDATION_FAVORITED_LABEL, variant="primary")
|
| 5440 |
+
return gr.update(value=VALIDATION_FAVORITE_LABEL, variant="secondary")
|
| 5441 |
|
| 5442 |
def on_table_select(project_slug: str, repo: str, rows: object, cache_key: str, session, evt: gr.SelectData):
|
| 5443 |
if not _can_access_project(session, project_slug):
|
tests/unit/test_app_factory_audio_helpers.py
CHANGED
|
@@ -40,6 +40,7 @@ from src.ui.app_factory import (
|
|
| 40 |
_resolve_project_fetch_token,
|
| 41 |
_initialize_hf_admin_storage,
|
| 42 |
_write_validation_export,
|
|
|
|
| 43 |
)
|
| 44 |
from src.auth.auth_service import AuthService
|
| 45 |
from src.config.runtime_config import RuntimeConfig
|
|
@@ -1018,6 +1019,25 @@ def test_create_app_with_keyboard_shortcuts() -> None:
|
|
| 1018 |
assert hasattr(app, "launch")
|
| 1019 |
|
| 1020 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1021 |
def test_batch_validate_conflicts_all_success() -> None:
|
| 1022 |
"""Test batch approval of all conflicts in table."""
|
| 1023 |
audio_service = FakeAudioService()
|
|
|
|
| 40 |
_resolve_project_fetch_token,
|
| 41 |
_initialize_hf_admin_storage,
|
| 42 |
_write_validation_export,
|
| 43 |
+
_validation_shortcuts_script,
|
| 44 |
)
|
| 45 |
from src.auth.auth_service import AuthService
|
| 46 |
from src.config.runtime_config import RuntimeConfig
|
|
|
|
| 1019 |
assert hasattr(app, "launch")
|
| 1020 |
|
| 1021 |
|
| 1022 |
+
def test_validation_shortcuts_are_validate_tab_scoped() -> None:
|
| 1023 |
+
script = _validation_shortcuts_script()
|
| 1024 |
+
|
| 1025 |
+
assert "bn-validation-queue-table" in script
|
| 1026 |
+
assert "validateTabIsActive" in script
|
| 1027 |
+
assert "ArrowUp" in script
|
| 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
|
| 1035 |
+
assert "bn-validate-skip-btn" in script
|
| 1036 |
+
assert "bn-validate-favorite-btn" in script
|
| 1037 |
+
assert "event.repeat" in script
|
| 1038 |
+
assert "isTypingTarget" in script
|
| 1039 |
+
|
| 1040 |
+
|
| 1041 |
def test_batch_validate_conflicts_all_success() -> None:
|
| 1042 |
"""Test batch approval of all conflicts in table."""
|
| 1043 |
audio_service = FakeAudioService()
|