jrrribeiro commited on
Commit
bde5e4c
·
1 Parent(s): 75d15f3

feat(validation): implement validation service and in-memory repository for detection validations

Browse files
README.md CHANGED
@@ -51,3 +51,4 @@ Estrutura base criada por projeto/dataset:
51
  - Sprint 2 (inicial): fila paginada de deteccoes com filtros basicos em demo local Gradio
52
  - Sprint 2 (backend): AudioFetchService com download sob demanda por audio_id + cache efemero com cleanup pos-validacao
53
  - Sprint 2 (UI): carregar audio da deteccao selecionada e limpar cache apos validacao
 
 
51
  - Sprint 2 (inicial): fila paginada de deteccoes com filtros basicos em demo local Gradio
52
  - Sprint 2 (backend): AudioFetchService com download sob demanda por audio_id + cache efemero com cleanup pos-validacao
53
  - Sprint 2 (UI): carregar audio da deteccao selecionada e limpar cache apos validacao
54
+ - Sprint 2 (UI fluxo): botoes de validacao rapida com salvamento em memoria e limpeza automatica do cache de audio
src/repositories/in_memory_validation_repository.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import defaultdict
2
+ from typing import DefaultDict
3
+
4
+ from src.domain.models import Validation
5
+
6
+
7
+ class InMemoryValidationRepository:
8
+ def __init__(self) -> None:
9
+ self._by_project: DefaultDict[str, list[Validation]] = defaultdict(list)
10
+
11
+ def save_validation(self, project_slug: str, item: Validation) -> None:
12
+ self._by_project[project_slug].append(item)
13
+
14
+ def list_validations(self, project_slug: str) -> list[Validation]:
15
+ return list(self._by_project.get(project_slug, []))
src/services/validation_service.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from src.domain.models import Validation
2
+ from src.repositories.contracts import ValidationRepository
3
+
4
+
5
+ class ValidationService:
6
+ def __init__(self, repository: ValidationRepository) -> None:
7
+ self._repository = repository
8
+
9
+ def validate_detection(
10
+ self,
11
+ project_slug: str,
12
+ detection_key: str,
13
+ status: str,
14
+ validator: str,
15
+ notes: str = "",
16
+ corrected_species: str | None = None,
17
+ ) -> Validation:
18
+ item = Validation(
19
+ detection_key=detection_key,
20
+ status=status,
21
+ corrected_species=corrected_species,
22
+ notes=notes,
23
+ validator=validator,
24
+ )
25
+ self._repository.save_validation(project_slug=project_slug, item=item)
26
+ return item
src/ui/app_factory.py CHANGED
@@ -4,8 +4,10 @@ from typing import Protocol
4
  from src.cache.ephemeral_cache_manager import EphemeralCacheManager
5
  from src.domain.models import Detection
6
  from src.repositories.in_memory_detection_repository import InMemoryDetectionRepository
 
7
  from src.services.audio_fetch_service import AudioFetchService
8
  from src.services.detection_queue_service import DetectionQueueService
 
9
 
10
 
11
  class _AudioFetchResultProtocol(Protocol):
@@ -20,6 +22,18 @@ class _AudioServiceProtocol(Protocol):
20
  def cleanup_after_validation(self, cache_key: str) -> None: ...
21
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def _seed_service() -> DetectionQueueService:
24
  repo = InMemoryDetectionRepository()
25
  demo_items = [
@@ -105,6 +119,26 @@ def _extract_audio_id(rows: object, selected_index: int) -> str:
105
  return audio_id
106
 
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  def _fetch_selected_audio(
109
  audio_service: _AudioServiceProtocol,
110
  dataset_repo: str,
@@ -135,9 +169,41 @@ def _cleanup_selected_audio(audio_service: _AudioServiceProtocol, cache_key: str
135
  return "Cache de audio limpo apos validacao", None
136
 
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  def create_app() -> gr.Blocks:
139
  service = _seed_service()
140
  audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
 
141
 
142
  with gr.Blocks(title="BirdNET Validator HF") as demo:
143
  gr.Markdown("# BirdNET Validator HF")
@@ -166,6 +232,16 @@ def create_app() -> gr.Blocks:
166
  load_audio_btn = gr.Button("Carregar audio selecionado")
167
  clear_audio_btn = gr.Button("Limpar cache apos validacao")
168
 
 
 
 
 
 
 
 
 
 
 
169
  audio_player = gr.Audio(label="Audio sob demanda", type="filepath")
170
  cache_key_state = gr.State(value="")
171
  status = gr.Textbox(label="Status", interactive=False)
@@ -223,5 +299,65 @@ def create_app() -> gr.Blocks:
223
  inputs=[cache_key_state],
224
  outputs=[status, audio_player],
225
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  return demo
 
4
  from src.cache.ephemeral_cache_manager import EphemeralCacheManager
5
  from src.domain.models import Detection
6
  from src.repositories.in_memory_detection_repository import InMemoryDetectionRepository
7
+ from src.repositories.in_memory_validation_repository import InMemoryValidationRepository
8
  from src.services.audio_fetch_service import AudioFetchService
9
  from src.services.detection_queue_service import DetectionQueueService
10
+ from src.services.validation_service import ValidationService
11
 
12
 
13
  class _AudioFetchResultProtocol(Protocol):
 
22
  def cleanup_after_validation(self, cache_key: str) -> None: ...
23
 
24
 
25
+ class _ValidationServiceProtocol(Protocol):
26
+ def validate_detection(
27
+ self,
28
+ project_slug: str,
29
+ detection_key: str,
30
+ status: str,
31
+ validator: str,
32
+ notes: str = "",
33
+ corrected_species: str | None = None,
34
+ ) -> object: ...
35
+
36
+
37
  def _seed_service() -> DetectionQueueService:
38
  repo = InMemoryDetectionRepository()
39
  demo_items = [
 
119
  return audio_id
120
 
121
 
122
+ def _extract_detection_key(rows: object, selected_index: int) -> str:
123
+ normalized_rows: list[list[object]]
124
+
125
+ if hasattr(rows, "values"):
126
+ normalized_rows = [list(item) for item in rows.values.tolist()]
127
+ else:
128
+ normalized_rows = [list(item) for item in rows] if rows else []
129
+
130
+ if not normalized_rows:
131
+ raise ValueError("Nenhuma deteccao carregada na tabela")
132
+ if selected_index < 0 or selected_index >= len(normalized_rows):
133
+ raise ValueError("Selecione uma deteccao valida na tabela")
134
+
135
+ value = normalized_rows[selected_index][0]
136
+ detection_key = str(value).strip()
137
+ if not detection_key:
138
+ raise ValueError("detection_key invalido na deteccao selecionada")
139
+ return detection_key
140
+
141
+
142
  def _fetch_selected_audio(
143
  audio_service: _AudioServiceProtocol,
144
  dataset_repo: str,
 
169
  return "Cache de audio limpo apos validacao", None
170
 
171
 
172
+ def _save_selected_validation(
173
+ validation_service: _ValidationServiceProtocol,
174
+ audio_service: _AudioServiceProtocol,
175
+ project_slug: str,
176
+ rows: object,
177
+ selected_index: int,
178
+ status_value: str,
179
+ validator: str,
180
+ notes: str,
181
+ cache_key: str,
182
+ ) -> tuple[str, str, str | None]:
183
+ validator_name = validator.strip()
184
+ if not validator_name:
185
+ return "Informe o nome do validador", cache_key, None
186
+
187
+ try:
188
+ detection_key = _extract_detection_key(rows=rows, selected_index=selected_index)
189
+ _ = validation_service.validate_detection(
190
+ project_slug=project_slug,
191
+ detection_key=detection_key,
192
+ status=status_value,
193
+ validator=validator_name,
194
+ notes=notes.strip(),
195
+ )
196
+ if cache_key:
197
+ audio_service.cleanup_after_validation(cache_key=cache_key)
198
+ return f"Validacao salva: {detection_key} -> {status_value}", "", None
199
+ except Exception as exc:
200
+ return f"Falha ao salvar validacao: {exc}", cache_key, None
201
+
202
+
203
  def create_app() -> gr.Blocks:
204
  service = _seed_service()
205
  audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
206
+ validation_service = ValidationService(InMemoryValidationRepository())
207
 
208
  with gr.Blocks(title="BirdNET Validator HF") as demo:
209
  gr.Markdown("# BirdNET Validator HF")
 
232
  load_audio_btn = gr.Button("Carregar audio selecionado")
233
  clear_audio_btn = gr.Button("Limpar cache apos validacao")
234
 
235
+ with gr.Row():
236
+ validator_name = gr.Textbox(label="Validador", value="validator-demo")
237
+ validation_notes = gr.Textbox(label="Notas", placeholder="Opcional")
238
+
239
+ with gr.Row():
240
+ approve_btn = gr.Button("Validar positivo")
241
+ reject_btn = gr.Button("Validar negativo")
242
+ uncertain_btn = gr.Button("Indeterminado")
243
+ skip_btn = gr.Button("Pular")
244
+
245
  audio_player = gr.Audio(label="Audio sob demanda", type="filepath")
246
  cache_key_state = gr.State(value="")
247
  status = gr.Textbox(label="Status", interactive=False)
 
299
  inputs=[cache_key_state],
300
  outputs=[status, audio_player],
301
  )
302
+ approve_btn.click(
303
+ fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
304
+ validation_service=validation_service,
305
+ audio_service=audio_service,
306
+ project_slug="demo-project",
307
+ rows=rows,
308
+ selected_index=int(idx),
309
+ status_value="positive",
310
+ validator=name,
311
+ notes=notes,
312
+ cache_key=cache_key,
313
+ ),
314
+ inputs=[table, selected_index, validator_name, validation_notes, cache_key_state],
315
+ outputs=[status, cache_key_state, audio_player],
316
+ )
317
+ reject_btn.click(
318
+ fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
319
+ validation_service=validation_service,
320
+ audio_service=audio_service,
321
+ project_slug="demo-project",
322
+ rows=rows,
323
+ selected_index=int(idx),
324
+ status_value="negative",
325
+ validator=name,
326
+ notes=notes,
327
+ cache_key=cache_key,
328
+ ),
329
+ inputs=[table, selected_index, validator_name, validation_notes, cache_key_state],
330
+ outputs=[status, cache_key_state, audio_player],
331
+ )
332
+ uncertain_btn.click(
333
+ fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
334
+ validation_service=validation_service,
335
+ audio_service=audio_service,
336
+ project_slug="demo-project",
337
+ rows=rows,
338
+ selected_index=int(idx),
339
+ status_value="uncertain",
340
+ validator=name,
341
+ notes=notes,
342
+ cache_key=cache_key,
343
+ ),
344
+ inputs=[table, selected_index, validator_name, validation_notes, cache_key_state],
345
+ outputs=[status, cache_key_state, audio_player],
346
+ )
347
+ skip_btn.click(
348
+ fn=lambda rows, idx, name, notes, cache_key: _save_selected_validation(
349
+ validation_service=validation_service,
350
+ audio_service=audio_service,
351
+ project_slug="demo-project",
352
+ rows=rows,
353
+ selected_index=int(idx),
354
+ status_value="skip",
355
+ validator=name,
356
+ notes=notes,
357
+ cache_key=cache_key,
358
+ ),
359
+ inputs=[table, selected_index, validator_name, validation_notes, cache_key_state],
360
+ outputs=[status, cache_key_state, audio_player],
361
+ )
362
 
363
  return demo
tests/unit/test_app_factory_audio_helpers.py CHANGED
@@ -3,7 +3,13 @@ from pathlib import Path
3
 
4
  import pandas as pd
5
 
6
- from src.ui.app_factory import _cleanup_selected_audio, _extract_audio_id, _fetch_selected_audio
 
 
 
 
 
 
7
 
8
 
9
  @dataclass
@@ -25,6 +31,31 @@ class FakeAudioService:
25
  self.cleaned.append(cache_key)
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def test_extract_audio_id_from_list_rows() -> None:
29
  rows = [["k1", "audio_01", "sp", 0.9, 0.0, 1.0]]
30
  assert _extract_audio_id(rows, 0) == "audio_01"
@@ -52,6 +83,11 @@ def test_fetch_selected_audio_success() -> None:
52
  assert "Audio carregado" in status
53
 
54
 
 
 
 
 
 
55
  def test_fetch_selected_audio_validates_repo() -> None:
56
  service = FakeAudioService()
57
  rows = [["k1", "audio_03", "sp", 0.9, 0.0, 1.0]]
@@ -77,3 +113,28 @@ def test_cleanup_selected_audio() -> None:
77
  assert "Cache de audio limpo" in status
78
  assert player_value is None
79
  assert service.cleaned == ["key:audio_03"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  import pandas as pd
5
 
6
+ from src.ui.app_factory import (
7
+ _cleanup_selected_audio,
8
+ _extract_audio_id,
9
+ _extract_detection_key,
10
+ _fetch_selected_audio,
11
+ _save_selected_validation,
12
+ )
13
 
14
 
15
  @dataclass
 
31
  self.cleaned.append(cache_key)
32
 
33
 
34
+ class FakeValidationService:
35
+ def __init__(self) -> None:
36
+ self.calls: list[dict[str, str]] = []
37
+
38
+ def validate_detection(
39
+ self,
40
+ project_slug: str,
41
+ detection_key: str,
42
+ status: str,
43
+ validator: str,
44
+ notes: str = "",
45
+ corrected_species: str | None = None,
46
+ ) -> dict[str, str]:
47
+ _ = corrected_species
48
+ payload = {
49
+ "project_slug": project_slug,
50
+ "detection_key": detection_key,
51
+ "status": status,
52
+ "validator": validator,
53
+ "notes": notes,
54
+ }
55
+ self.calls.append(payload)
56
+ return payload
57
+
58
+
59
  def test_extract_audio_id_from_list_rows() -> None:
60
  rows = [["k1", "audio_01", "sp", 0.9, 0.0, 1.0]]
61
  assert _extract_audio_id(rows, 0) == "audio_01"
 
83
  assert "Audio carregado" in status
84
 
85
 
86
+ def test_extract_detection_key_from_rows() -> None:
87
+ rows = [["dkey_01", "audio_01", "sp", 0.9, 0.0, 1.0]]
88
+ assert _extract_detection_key(rows, 0) == "dkey_01"
89
+
90
+
91
  def test_fetch_selected_audio_validates_repo() -> None:
92
  service = FakeAudioService()
93
  rows = [["k1", "audio_03", "sp", 0.9, 0.0, 1.0]]
 
113
  assert "Cache de audio limpo" in status
114
  assert player_value is None
115
  assert service.cleaned == ["key:audio_03"]
116
+
117
+
118
+ def test_save_selected_validation_saves_and_cleans_audio_cache() -> None:
119
+ audio_service = FakeAudioService()
120
+ validation_service = FakeValidationService()
121
+ rows = [["0000000000001111", "audio_11", "sp", 0.9, 0.0, 1.0]]
122
+
123
+ status, cache_key, audio_path = _save_selected_validation(
124
+ validation_service=validation_service,
125
+ audio_service=audio_service,
126
+ project_slug="demo-project",
127
+ rows=rows,
128
+ selected_index=0,
129
+ status_value="positive",
130
+ validator="validator-demo",
131
+ notes="ok",
132
+ cache_key="cache:audio_11",
133
+ )
134
+
135
+ assert "Validacao salva" in status
136
+ assert cache_key == ""
137
+ assert audio_path is None
138
+ assert len(validation_service.calls) == 1
139
+ assert validation_service.calls[0]["detection_key"] == "0000000000001111"
140
+ assert audio_service.cleaned == ["cache:audio_11"]
tests/unit/test_validation_service.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from src.repositories.in_memory_validation_repository import InMemoryValidationRepository
2
+ from src.services.validation_service import ValidationService
3
+
4
+
5
+ def test_validate_detection_saves_item() -> None:
6
+ repo = InMemoryValidationRepository()
7
+ service = ValidationService(repo)
8
+
9
+ item = service.validate_detection(
10
+ project_slug="demo-project",
11
+ detection_key="0000000000009999",
12
+ status="positive",
13
+ validator="validator-demo",
14
+ notes="ok",
15
+ )
16
+
17
+ assert item.status == "positive"
18
+ assert item.validator == "validator-demo"
19
+ saved = repo.list_validations("demo-project")
20
+ assert len(saved) == 1
21
+ assert saved[0].detection_key == "0000000000009999"