Spaces:
Sleeping
Sleeping
Commit ·
dccb9dc
1
Parent(s): f505798
feat(ui): show current validation status and project report
Browse files- src/ui/app_factory.py +83 -8
src/ui/app_factory.py
CHANGED
|
@@ -36,6 +36,24 @@ class _ValidationServiceProtocol(Protocol):
|
|
| 36 |
) -> object: ...
|
| 37 |
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def _seed_service() -> DetectionQueueService:
|
| 40 |
repo = InMemoryDetectionRepository()
|
| 41 |
demo_items = [
|
|
@@ -76,7 +94,14 @@ def _seed_service() -> DetectionQueueService:
|
|
| 76 |
return DetectionQueueService(repo)
|
| 77 |
|
| 78 |
|
| 79 |
-
def _page_to_table(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
filter_name = scientific_name.strip() if scientific_name.strip() else None
|
| 81 |
page_obj = service.get_page(
|
| 82 |
project_slug="demo-project",
|
|
@@ -86,6 +111,8 @@ def _page_to_table(service: DetectionQueueService, page: int, scientific_name: s
|
|
| 86 |
min_confidence=min_confidence,
|
| 87 |
)
|
| 88 |
|
|
|
|
|
|
|
| 89 |
rows = [
|
| 90 |
[
|
| 91 |
item.detection_key,
|
|
@@ -94,6 +121,7 @@ def _page_to_table(service: DetectionQueueService, page: int, scientific_name: s
|
|
| 94 |
round(item.confidence, 3),
|
| 95 |
item.start_time,
|
| 96 |
item.end_time,
|
|
|
|
| 97 |
]
|
| 98 |
for item in page_obj.items
|
| 99 |
]
|
|
@@ -101,6 +129,28 @@ def _page_to_table(service: DetectionQueueService, page: int, scientific_name: s
|
|
| 101 |
return rows, status, page_obj.page
|
| 102 |
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
def _extract_audio_id(rows: object, selected_index: int) -> str:
|
| 105 |
normalized_rows: list[list[object]]
|
| 106 |
|
|
@@ -203,10 +253,12 @@ def _save_selected_validation(
|
|
| 203 |
|
| 204 |
|
| 205 |
def create_app() -> gr.Blocks:
|
|
|
|
| 206 |
service = _seed_service()
|
| 207 |
audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
|
| 208 |
validation_base_dir = str(Path(tempfile.gettempdir()) / "birdnet-validator-validations")
|
| 209 |
-
|
|
|
|
| 210 |
|
| 211 |
with gr.Blocks(title="BirdNET Validator HF") as demo:
|
| 212 |
gr.Markdown("# BirdNET Validator HF")
|
|
@@ -225,7 +277,15 @@ def create_app() -> gr.Blocks:
|
|
| 225 |
|
| 226 |
page_state = gr.State(value=1)
|
| 227 |
table = gr.Dataframe(
|
| 228 |
-
headers=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
label="Deteccoes",
|
| 230 |
interactive=False,
|
| 231 |
)
|
|
@@ -245,12 +305,22 @@ def create_app() -> gr.Blocks:
|
|
| 245 |
uncertain_btn = gr.Button("Indeterminado")
|
| 246 |
skip_btn = gr.Button("Pular")
|
| 247 |
|
|
|
|
|
|
|
| 248 |
audio_player = gr.Audio(label="Audio sob demanda", type="filepath")
|
| 249 |
cache_key_state = gr.State(value="")
|
| 250 |
status = gr.Textbox(label="Status", interactive=False)
|
|
|
|
| 251 |
|
| 252 |
def refresh(page: int, species: str, confidence: float):
|
| 253 |
-
return _page_to_table(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
def go_next(page: int, species: str, confidence: float):
|
| 256 |
return refresh(page + 1, species, confidence)
|
|
@@ -306,7 +376,7 @@ def create_app() -> gr.Blocks:
|
|
| 306 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 307 |
validation_service=validation_service,
|
| 308 |
audio_service=audio_service,
|
| 309 |
-
project_slug=
|
| 310 |
rows=rows,
|
| 311 |
selected_index=int(idx),
|
| 312 |
status_value="positive",
|
|
@@ -321,7 +391,7 @@ def create_app() -> gr.Blocks:
|
|
| 321 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 322 |
validation_service=validation_service,
|
| 323 |
audio_service=audio_service,
|
| 324 |
-
project_slug=
|
| 325 |
rows=rows,
|
| 326 |
selected_index=int(idx),
|
| 327 |
status_value="negative",
|
|
@@ -336,7 +406,7 @@ def create_app() -> gr.Blocks:
|
|
| 336 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 337 |
validation_service=validation_service,
|
| 338 |
audio_service=audio_service,
|
| 339 |
-
project_slug=
|
| 340 |
rows=rows,
|
| 341 |
selected_index=int(idx),
|
| 342 |
status_value="uncertain",
|
|
@@ -351,7 +421,7 @@ def create_app() -> gr.Blocks:
|
|
| 351 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 352 |
validation_service=validation_service,
|
| 353 |
audio_service=audio_service,
|
| 354 |
-
project_slug=
|
| 355 |
rows=rows,
|
| 356 |
selected_index=int(idx),
|
| 357 |
status_value="skip",
|
|
@@ -362,5 +432,10 @@ def create_app() -> gr.Blocks:
|
|
| 362 |
inputs=[table, selected_index, validator_name, validation_notes, cache_key_state],
|
| 363 |
outputs=[status, cache_key_state, audio_player],
|
| 364 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
return demo
|
|
|
|
| 36 |
) -> object: ...
|
| 37 |
|
| 38 |
|
| 39 |
+
class _ValidationReadRepositoryProtocol(Protocol):
|
| 40 |
+
def load_current_snapshot(self, project_slug: str) -> dict[str, dict[str, object]]: ...
|
| 41 |
+
|
| 42 |
+
def list_events(self, project_slug: str) -> list[dict[str, object]]: ...
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class _QueueServiceProtocol(Protocol):
|
| 46 |
+
def get_page(
|
| 47 |
+
self,
|
| 48 |
+
project_slug: str,
|
| 49 |
+
page: int,
|
| 50 |
+
page_size: int,
|
| 51 |
+
scientific_name: str | None = None,
|
| 52 |
+
min_confidence: float | None = None,
|
| 53 |
+
max_confidence: float | None = None,
|
| 54 |
+
) -> object: ...
|
| 55 |
+
|
| 56 |
+
|
| 57 |
def _seed_service() -> DetectionQueueService:
|
| 58 |
repo = InMemoryDetectionRepository()
|
| 59 |
demo_items = [
|
|
|
|
| 94 |
return DetectionQueueService(repo)
|
| 95 |
|
| 96 |
|
| 97 |
+
def _page_to_table(
|
| 98 |
+
service: _QueueServiceProtocol,
|
| 99 |
+
snapshot_reader: _ValidationReadRepositoryProtocol,
|
| 100 |
+
project_slug: str,
|
| 101 |
+
page: int,
|
| 102 |
+
scientific_name: str,
|
| 103 |
+
min_confidence: float,
|
| 104 |
+
):
|
| 105 |
filter_name = scientific_name.strip() if scientific_name.strip() else None
|
| 106 |
page_obj = service.get_page(
|
| 107 |
project_slug="demo-project",
|
|
|
|
| 111 |
min_confidence=min_confidence,
|
| 112 |
)
|
| 113 |
|
| 114 |
+
snapshot = snapshot_reader.load_current_snapshot(project_slug=project_slug)
|
| 115 |
+
|
| 116 |
rows = [
|
| 117 |
[
|
| 118 |
item.detection_key,
|
|
|
|
| 121 |
round(item.confidence, 3),
|
| 122 |
item.start_time,
|
| 123 |
item.end_time,
|
| 124 |
+
str(snapshot.get(item.detection_key, {}).get("status", "pending")),
|
| 125 |
]
|
| 126 |
for item in page_obj.items
|
| 127 |
]
|
|
|
|
| 129 |
return rows, status, page_obj.page
|
| 130 |
|
| 131 |
|
| 132 |
+
def _build_validation_report(snapshot_reader: _ValidationReadRepositoryProtocol, project_slug: str) -> str:
|
| 133 |
+
snapshot = snapshot_reader.load_current_snapshot(project_slug=project_slug)
|
| 134 |
+
events = snapshot_reader.list_events(project_slug=project_slug)
|
| 135 |
+
|
| 136 |
+
counts: dict[str, int] = {}
|
| 137 |
+
for payload in snapshot.values():
|
| 138 |
+
status_value = str(payload.get("status", "unknown"))
|
| 139 |
+
counts[status_value] = counts.get(status_value, 0) + 1
|
| 140 |
+
|
| 141 |
+
parts = [
|
| 142 |
+
f"Projeto: {project_slug}",
|
| 143 |
+
f"Eventos append-only: {len(events)}",
|
| 144 |
+
f"Deteccoes com estado atual: {len(snapshot)}",
|
| 145 |
+
]
|
| 146 |
+
if counts:
|
| 147 |
+
summary = ", ".join(f"{key}={value}" for key, value in sorted(counts.items()))
|
| 148 |
+
parts.append(f"Status atual: {summary}")
|
| 149 |
+
else:
|
| 150 |
+
parts.append("Status atual: sem validacoes")
|
| 151 |
+
return " | ".join(parts)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
def _extract_audio_id(rows: object, selected_index: int) -> str:
|
| 155 |
normalized_rows: list[list[object]]
|
| 156 |
|
|
|
|
| 253 |
|
| 254 |
|
| 255 |
def create_app() -> gr.Blocks:
|
| 256 |
+
project_slug = "demo-project"
|
| 257 |
service = _seed_service()
|
| 258 |
audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
|
| 259 |
validation_base_dir = str(Path(tempfile.gettempdir()) / "birdnet-validator-validations")
|
| 260 |
+
validation_repository = AppendOnlyValidationRepository(base_dir=validation_base_dir)
|
| 261 |
+
validation_service = ValidationService(validation_repository)
|
| 262 |
|
| 263 |
with gr.Blocks(title="BirdNET Validator HF") as demo:
|
| 264 |
gr.Markdown("# BirdNET Validator HF")
|
|
|
|
| 277 |
|
| 278 |
page_state = gr.State(value=1)
|
| 279 |
table = gr.Dataframe(
|
| 280 |
+
headers=[
|
| 281 |
+
"detection_key",
|
| 282 |
+
"audio_id",
|
| 283 |
+
"scientific_name",
|
| 284 |
+
"confidence",
|
| 285 |
+
"start_time",
|
| 286 |
+
"end_time",
|
| 287 |
+
"validation_status",
|
| 288 |
+
],
|
| 289 |
label="Deteccoes",
|
| 290 |
interactive=False,
|
| 291 |
)
|
|
|
|
| 305 |
uncertain_btn = gr.Button("Indeterminado")
|
| 306 |
skip_btn = gr.Button("Pular")
|
| 307 |
|
| 308 |
+
report_btn = gr.Button("Gerar relatorio de validacoes")
|
| 309 |
+
|
| 310 |
audio_player = gr.Audio(label="Audio sob demanda", type="filepath")
|
| 311 |
cache_key_state = gr.State(value="")
|
| 312 |
status = gr.Textbox(label="Status", interactive=False)
|
| 313 |
+
report_box = gr.Textbox(label="Relatorio", interactive=False)
|
| 314 |
|
| 315 |
def refresh(page: int, species: str, confidence: float):
|
| 316 |
+
return _page_to_table(
|
| 317 |
+
service=service,
|
| 318 |
+
snapshot_reader=validation_repository,
|
| 319 |
+
project_slug=project_slug,
|
| 320 |
+
page=page,
|
| 321 |
+
scientific_name=species,
|
| 322 |
+
min_confidence=confidence,
|
| 323 |
+
)
|
| 324 |
|
| 325 |
def go_next(page: int, species: str, confidence: float):
|
| 326 |
return refresh(page + 1, species, confidence)
|
|
|
|
| 376 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 377 |
validation_service=validation_service,
|
| 378 |
audio_service=audio_service,
|
| 379 |
+
project_slug=project_slug,
|
| 380 |
rows=rows,
|
| 381 |
selected_index=int(idx),
|
| 382 |
status_value="positive",
|
|
|
|
| 391 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 392 |
validation_service=validation_service,
|
| 393 |
audio_service=audio_service,
|
| 394 |
+
project_slug=project_slug,
|
| 395 |
rows=rows,
|
| 396 |
selected_index=int(idx),
|
| 397 |
status_value="negative",
|
|
|
|
| 406 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 407 |
validation_service=validation_service,
|
| 408 |
audio_service=audio_service,
|
| 409 |
+
project_slug=project_slug,
|
| 410 |
rows=rows,
|
| 411 |
selected_index=int(idx),
|
| 412 |
status_value="uncertain",
|
|
|
|
| 421 |
fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
|
| 422 |
validation_service=validation_service,
|
| 423 |
audio_service=audio_service,
|
| 424 |
+
project_slug=project_slug,
|
| 425 |
rows=rows,
|
| 426 |
selected_index=int(idx),
|
| 427 |
status_value="skip",
|
|
|
|
| 432 |
inputs=[table, selected_index, validator_name, validation_notes, cache_key_state],
|
| 433 |
outputs=[status, cache_key_state, audio_player],
|
| 434 |
)
|
| 435 |
+
report_btn.click(
|
| 436 |
+
fn=lambda: _build_validation_report(validation_repository, project_slug),
|
| 437 |
+
inputs=None,
|
| 438 |
+
outputs=[report_box],
|
| 439 |
+
)
|
| 440 |
|
| 441 |
return demo
|