Spaces:
Sleeping
Sleeping
Commit ·
7cc41ee
1
Parent(s): cc5f912
feat(ui): add batch operations for conflicts (approve all/reject all)
Browse files
src/ui/app_factory.py
CHANGED
|
@@ -446,6 +446,145 @@ def _reapply_last_conflict_validation_with_refresh(
|
|
| 446 |
)
|
| 447 |
|
| 448 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
def create_app() -> gr.Blocks:
|
| 450 |
project_slug = "demo-project"
|
| 451 |
service = _seed_service()
|
|
@@ -504,6 +643,10 @@ def create_app() -> gr.Blocks:
|
|
| 504 |
skip_btn = gr.Button("Pular")
|
| 505 |
reapply_btn = gr.Button("Reaplicar validacao apos conflito")
|
| 506 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
report_btn = gr.Button("Gerar relatorio de validacoes")
|
| 508 |
|
| 509 |
audio_player = gr.Audio(label="Audio sob demanda", type="filepath")
|
|
@@ -721,6 +864,44 @@ def create_app() -> gr.Blocks:
|
|
| 721 |
],
|
| 722 |
outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
|
| 723 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
report_btn.click(
|
| 725 |
fn=lambda: _build_validation_report(validation_repository, project_slug),
|
| 726 |
inputs=None,
|
|
|
|
| 446 |
)
|
| 447 |
|
| 448 |
|
| 449 |
+
def _batch_validate_conflicts(
|
| 450 |
+
validation_service: _ValidationServiceProtocol,
|
| 451 |
+
audio_service: _AudioServiceProtocol,
|
| 452 |
+
queue_service: _QueueServiceProtocol,
|
| 453 |
+
snapshot_reader: _ValidationReadRepositoryProtocol,
|
| 454 |
+
project_slug: str,
|
| 455 |
+
rows: object,
|
| 456 |
+
status_value: str,
|
| 457 |
+
validator: str,
|
| 458 |
+
notes: str,
|
| 459 |
+
cache_key: str,
|
| 460 |
+
page: int,
|
| 461 |
+
scientific_name: str,
|
| 462 |
+
min_confidence: float,
|
| 463 |
+
) -> tuple[str, str, str | None, list[list[object]], int]:
|
| 464 |
+
"""Apply the same validation status to all visible conflicts in the table."""
|
| 465 |
+
validator_name = validator.strip()
|
| 466 |
+
if not validator_name:
|
| 467 |
+
return "Informe o nome do validador", "", None, [], page
|
| 468 |
+
|
| 469 |
+
normalized_rows: list[list[object]]
|
| 470 |
+
if hasattr(rows, "values"):
|
| 471 |
+
normalized_rows = [list(item) for item in rows.values.tolist()]
|
| 472 |
+
else:
|
| 473 |
+
normalized_rows = [list(item) for item in rows] if rows else []
|
| 474 |
+
|
| 475 |
+
if not normalized_rows:
|
| 476 |
+
return "Nenhuma deteccao com conflito para validar", "", None, [], page
|
| 477 |
+
|
| 478 |
+
conflict_rows = [row for row in normalized_rows if str(row[8]) == "CONFLICT"]
|
| 479 |
+
if not conflict_rows:
|
| 480 |
+
return "Nenhuma deteccao com conflito identificada na tabela", "", None, normalized_rows, page
|
| 481 |
+
|
| 482 |
+
success_count = 0
|
| 483 |
+
failure_count = 0
|
| 484 |
+
conflict_count = 0
|
| 485 |
+
|
| 486 |
+
for row in conflict_rows:
|
| 487 |
+
try:
|
| 488 |
+
detection_key = str(row[0]).strip()
|
| 489 |
+
expected_version = int(row[7])
|
| 490 |
+
|
| 491 |
+
_ = validation_service.validate_detection(
|
| 492 |
+
project_slug=project_slug,
|
| 493 |
+
detection_key=detection_key,
|
| 494 |
+
status=status_value,
|
| 495 |
+
validator=validator_name,
|
| 496 |
+
notes=notes.strip(),
|
| 497 |
+
expected_version=expected_version,
|
| 498 |
+
)
|
| 499 |
+
success_count += 1
|
| 500 |
+
if cache_key:
|
| 501 |
+
audio_service.cleanup_after_validation(cache_key=cache_key)
|
| 502 |
+
except OptimisticLockError:
|
| 503 |
+
conflict_count += 1
|
| 504 |
+
except Exception:
|
| 505 |
+
failure_count += 1
|
| 506 |
+
|
| 507 |
+
refreshed_rows, page_status, refreshed_page = _page_to_table(
|
| 508 |
+
service=queue_service,
|
| 509 |
+
snapshot_reader=snapshot_reader,
|
| 510 |
+
project_slug=project_slug,
|
| 511 |
+
page=page,
|
| 512 |
+
scientific_name=scientific_name,
|
| 513 |
+
min_confidence=min_confidence,
|
| 514 |
+
show_conflicts_only=False,
|
| 515 |
+
)
|
| 516 |
+
|
| 517 |
+
summary = f"Processados {len(conflict_rows)} conflitos: {success_count} sucesso, {conflict_count} novos conflitos, {failure_count} falhas"
|
| 518 |
+
status = f"{summary} | {page_status}"
|
| 519 |
+
|
| 520 |
+
return status, "", None, refreshed_rows, refreshed_page
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
def _batch_reapply_all_pending(
|
| 524 |
+
validation_service: _ValidationServiceProtocol,
|
| 525 |
+
audio_service: _AudioServiceProtocol,
|
| 526 |
+
queue_service: _QueueServiceProtocol,
|
| 527 |
+
snapshot_reader: _ValidationReadRepositoryProtocol,
|
| 528 |
+
project_slug: str,
|
| 529 |
+
rows: object,
|
| 530 |
+
pending_statuses: dict[str, str],
|
| 531 |
+
validator: str,
|
| 532 |
+
notes: str,
|
| 533 |
+
cache_key: str,
|
| 534 |
+
page: int,
|
| 535 |
+
scientific_name: str,
|
| 536 |
+
min_confidence: float,
|
| 537 |
+
) -> tuple[str, str, str | None, list[list[object]], int]:
|
| 538 |
+
"""Reapply all pending validations (stored conflicts) with current version."""
|
| 539 |
+
if not pending_statuses:
|
| 540 |
+
return "Nenhuma validacao pendente para reaplicar", "", None, [], page
|
| 541 |
+
|
| 542 |
+
validator_name = validator.strip()
|
| 543 |
+
if not validator_name:
|
| 544 |
+
return "Informe o nome do validador", "", None, [], page
|
| 545 |
+
|
| 546 |
+
success_count = 0
|
| 547 |
+
conflict_count = 0
|
| 548 |
+
failure_count = 0
|
| 549 |
+
|
| 550 |
+
snapshot = snapshot_reader.load_current_snapshot(project_slug=project_slug)
|
| 551 |
+
|
| 552 |
+
for detection_key, status_value in pending_statuses.items():
|
| 553 |
+
try:
|
| 554 |
+
current_version = int(snapshot.get(detection_key, {}).get("version", 0))
|
| 555 |
+
|
| 556 |
+
_ = validation_service.validate_detection(
|
| 557 |
+
project_slug=project_slug,
|
| 558 |
+
detection_key=detection_key,
|
| 559 |
+
status=status_value,
|
| 560 |
+
validator=validator_name,
|
| 561 |
+
notes=notes.strip(),
|
| 562 |
+
expected_version=current_version,
|
| 563 |
+
)
|
| 564 |
+
success_count += 1
|
| 565 |
+
if cache_key:
|
| 566 |
+
audio_service.cleanup_after_validation(cache_key=cache_key)
|
| 567 |
+
except OptimisticLockError:
|
| 568 |
+
conflict_count += 1
|
| 569 |
+
except Exception:
|
| 570 |
+
failure_count += 1
|
| 571 |
+
|
| 572 |
+
refreshed_rows, page_status, refreshed_page = _page_to_table(
|
| 573 |
+
service=queue_service,
|
| 574 |
+
snapshot_reader=snapshot_reader,
|
| 575 |
+
project_slug=project_slug,
|
| 576 |
+
page=page,
|
| 577 |
+
scientific_name=scientific_name,
|
| 578 |
+
min_confidence=min_confidence,
|
| 579 |
+
show_conflicts_only=False,
|
| 580 |
+
)
|
| 581 |
+
|
| 582 |
+
summary = f"Reaplicadas {len(pending_statuses)} validacoes: {success_count} sucesso, {conflict_count} novos conflitos, {failure_count} falhas"
|
| 583 |
+
status = f"{summary} | {page_status}"
|
| 584 |
+
|
| 585 |
+
return status, "", None, refreshed_rows, refreshed_page
|
| 586 |
+
|
| 587 |
+
|
| 588 |
def create_app() -> gr.Blocks:
|
| 589 |
project_slug = "demo-project"
|
| 590 |
service = _seed_service()
|
|
|
|
| 643 |
skip_btn = gr.Button("Pular")
|
| 644 |
reapply_btn = gr.Button("Reaplicar validacao apos conflito")
|
| 645 |
|
| 646 |
+
with gr.Row():
|
| 647 |
+
batch_approve_conflicts_btn = gr.Button("Aprovar todos os conflitos")
|
| 648 |
+
batch_reject_conflicts_btn = gr.Button("Rejeitar todos os conflitos")
|
| 649 |
+
|
| 650 |
report_btn = gr.Button("Gerar relatorio de validacoes")
|
| 651 |
|
| 652 |
audio_player = gr.Audio(label="Audio sob demanda", type="filepath")
|
|
|
|
| 864 |
],
|
| 865 |
outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
|
| 866 |
)
|
| 867 |
+
batch_approve_conflicts_btn.click(
|
| 868 |
+
fn=lambda rows, name, notes, cache_key, page, species, confidence: _batch_validate_conflicts(
|
| 869 |
+
validation_service=validation_service,
|
| 870 |
+
audio_service=audio_service,
|
| 871 |
+
queue_service=service,
|
| 872 |
+
snapshot_reader=validation_repository,
|
| 873 |
+
project_slug=project_slug,
|
| 874 |
+
rows=rows,
|
| 875 |
+
status_value="positive",
|
| 876 |
+
validator=name,
|
| 877 |
+
notes=notes,
|
| 878 |
+
cache_key=cache_key,
|
| 879 |
+
page=int(page),
|
| 880 |
+
scientific_name=species,
|
| 881 |
+
min_confidence=float(confidence),
|
| 882 |
+
),
|
| 883 |
+
inputs=[table, validator_name, validation_notes, cache_key_state, page_state, species_filter, min_confidence],
|
| 884 |
+
outputs=[status, cache_key_state, audio_player, table, page_state],
|
| 885 |
+
)
|
| 886 |
+
batch_reject_conflicts_btn.click(
|
| 887 |
+
fn=lambda rows, name, notes, cache_key, page, species, confidence: _batch_validate_conflicts(
|
| 888 |
+
validation_service=validation_service,
|
| 889 |
+
audio_service=audio_service,
|
| 890 |
+
queue_service=service,
|
| 891 |
+
snapshot_reader=validation_repository,
|
| 892 |
+
project_slug=project_slug,
|
| 893 |
+
rows=rows,
|
| 894 |
+
status_value="negative",
|
| 895 |
+
validator=name,
|
| 896 |
+
notes=notes,
|
| 897 |
+
cache_key=cache_key,
|
| 898 |
+
page=int(page),
|
| 899 |
+
scientific_name=species,
|
| 900 |
+
min_confidence=float(confidence),
|
| 901 |
+
),
|
| 902 |
+
inputs=[table, validator_name, validation_notes, cache_key_state, page_state, species_filter, min_confidence],
|
| 903 |
+
outputs=[status, cache_key_state, audio_player, table, page_state],
|
| 904 |
+
)
|
| 905 |
report_btn.click(
|
| 906 |
fn=lambda: _build_validation_report(validation_repository, project_slug),
|
| 907 |
inputs=None,
|
tests/unit/test_app_factory_audio_helpers.py
CHANGED
|
@@ -14,6 +14,7 @@ from src.ui.app_factory import (
|
|
| 14 |
_save_selected_validation,
|
| 15 |
_save_selected_validation_with_refresh,
|
| 16 |
_reapply_last_conflict_validation_with_refresh,
|
|
|
|
| 17 |
create_app,
|
| 18 |
)
|
| 19 |
|
|
@@ -464,3 +465,63 @@ def test_create_app_with_keyboard_shortcuts() -> None:
|
|
| 464 |
# Verify the app is a Gradio Blocks instance
|
| 465 |
assert hasattr(app, "queue")
|
| 466 |
assert hasattr(app, "launch")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
_save_selected_validation,
|
| 15 |
_save_selected_validation_with_refresh,
|
| 16 |
_reapply_last_conflict_validation_with_refresh,
|
| 17 |
+
_batch_validate_conflicts,
|
| 18 |
create_app,
|
| 19 |
)
|
| 20 |
|
|
|
|
| 465 |
# Verify the app is a Gradio Blocks instance
|
| 466 |
assert hasattr(app, "queue")
|
| 467 |
assert hasattr(app, "launch")
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
def test_batch_validate_conflicts_all_success() -> None:
|
| 471 |
+
"""Test batch approval of all conflicts in table."""
|
| 472 |
+
audio_service = FakeAudioService()
|
| 473 |
+
validation_service = FakeValidationService()
|
| 474 |
+
rows = [
|
| 475 |
+
["dkey_01", "audio_11", "sp", 0.9, 0.0, 1.0, "pending", 1, "CONFLICT", "HIGH"],
|
| 476 |
+
["dkey_02", "audio_12", "sp", 0.85, 1.0, 2.0, "pending", 1, "CONFLICT", "HIGH"],
|
| 477 |
+
]
|
| 478 |
+
|
| 479 |
+
status, cache_key, audio_path, refreshed_rows, refreshed_page = _batch_validate_conflicts(
|
| 480 |
+
validation_service=validation_service,
|
| 481 |
+
audio_service=audio_service,
|
| 482 |
+
queue_service=FakeQueueService(),
|
| 483 |
+
snapshot_reader=FakeSnapshotReader(),
|
| 484 |
+
project_slug="demo-project",
|
| 485 |
+
rows=rows,
|
| 486 |
+
status_value="positive",
|
| 487 |
+
validator="validator-demo",
|
| 488 |
+
notes="batch approval",
|
| 489 |
+
cache_key="",
|
| 490 |
+
page=1,
|
| 491 |
+
scientific_name="",
|
| 492 |
+
min_confidence=0.0,
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
assert "Processados 2 conflitos" in status
|
| 496 |
+
assert "2 sucesso" in status
|
| 497 |
+
assert cache_key == ""
|
| 498 |
+
assert refreshed_page == 1
|
| 499 |
+
assert len(validation_service.calls) == 2
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
def test_batch_validate_conflicts_no_conflicts() -> None:
|
| 503 |
+
"""Test batch validation when no conflicts are present."""
|
| 504 |
+
audio_service = FakeAudioService()
|
| 505 |
+
validation_service = FakeValidationService()
|
| 506 |
+
rows = [
|
| 507 |
+
["dkey_01", "audio_11", "sp", 0.9, 0.0, 1.0, "positive", 2, "", ""],
|
| 508 |
+
]
|
| 509 |
+
|
| 510 |
+
status, cache_key, audio_path, refreshed_rows, refreshed_page = _batch_validate_conflicts(
|
| 511 |
+
validation_service=validation_service,
|
| 512 |
+
audio_service=audio_service,
|
| 513 |
+
queue_service=FakeQueueService(),
|
| 514 |
+
snapshot_reader=FakeSnapshotReader(),
|
| 515 |
+
project_slug="demo-project",
|
| 516 |
+
rows=rows,
|
| 517 |
+
status_value="positive",
|
| 518 |
+
validator="validator-demo",
|
| 519 |
+
notes="batch approval",
|
| 520 |
+
cache_key="",
|
| 521 |
+
page=1,
|
| 522 |
+
scientific_name="",
|
| 523 |
+
min_confidence=0.0,
|
| 524 |
+
)
|
| 525 |
+
|
| 526 |
+
assert "Nenhuma deteccao com conflito" in status
|
| 527 |
+
assert len(validation_service.calls) == 0
|