Spaces:
Sleeping
Sleeping
Commit ·
56d12ec
1
Parent(s): bde5e4c
feat(persistence): add append-only validation event repository and wire UI
Browse files
src/repositories/append_only_validation_repository.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from datetime import UTC, datetime
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from uuid import uuid4
|
| 5 |
+
|
| 6 |
+
from src.domain.models import Validation
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class AppendOnlyValidationRepository:
|
| 10 |
+
def __init__(self, base_dir: str) -> None:
|
| 11 |
+
self._base_dir = Path(base_dir)
|
| 12 |
+
|
| 13 |
+
def save_validation(self, project_slug: str, item: Validation) -> None:
|
| 14 |
+
project_dir = self._base_dir / project_slug / "validations"
|
| 15 |
+
project_dir.mkdir(parents=True, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
event_date = datetime.now(UTC).strftime("%Y%m%d")
|
| 18 |
+
events_file = project_dir / f"events-{event_date}.jsonl"
|
| 19 |
+
event = {
|
| 20 |
+
"event_id": str(uuid4()),
|
| 21 |
+
"timestamp": datetime.now(UTC).isoformat(),
|
| 22 |
+
"project_slug": project_slug,
|
| 23 |
+
**item.model_dump(),
|
| 24 |
+
}
|
| 25 |
+
with events_file.open("a", encoding="utf-8") as handle:
|
| 26 |
+
handle.write(json.dumps(event, ensure_ascii=True) + "\n")
|
| 27 |
+
|
| 28 |
+
current_file = project_dir / "current.json"
|
| 29 |
+
if current_file.exists():
|
| 30 |
+
current_payload = json.loads(current_file.read_text(encoding="utf-8"))
|
| 31 |
+
else:
|
| 32 |
+
current_payload = {}
|
| 33 |
+
|
| 34 |
+
current_payload[item.detection_key] = {
|
| 35 |
+
"status": item.status,
|
| 36 |
+
"corrected_species": item.corrected_species,
|
| 37 |
+
"notes": item.notes,
|
| 38 |
+
"validator": item.validator,
|
| 39 |
+
"updated_at": event["timestamp"],
|
| 40 |
+
}
|
| 41 |
+
current_file.write_text(json.dumps(current_payload, indent=2), encoding="utf-8")
|
| 42 |
+
|
| 43 |
+
def list_events(self, project_slug: str) -> list[dict[str, object]]:
|
| 44 |
+
project_dir = self._base_dir / project_slug / "validations"
|
| 45 |
+
if not project_dir.exists():
|
| 46 |
+
return []
|
| 47 |
+
|
| 48 |
+
events: list[dict[str, object]] = []
|
| 49 |
+
for path in sorted(project_dir.glob("events-*.jsonl")):
|
| 50 |
+
with path.open("r", encoding="utf-8") as handle:
|
| 51 |
+
for line in handle:
|
| 52 |
+
payload = line.strip()
|
| 53 |
+
if payload:
|
| 54 |
+
events.append(json.loads(payload))
|
| 55 |
+
return events
|
| 56 |
+
|
| 57 |
+
def load_current_snapshot(self, project_slug: str) -> dict[str, dict[str, object]]:
|
| 58 |
+
current_file = self._base_dir / project_slug / "validations" / "current.json"
|
| 59 |
+
if not current_file.exists():
|
| 60 |
+
return {}
|
| 61 |
+
return json.loads(current_file.read_text(encoding="utf-8"))
|
src/ui/app_factory.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from typing import Protocol
|
| 3 |
|
| 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
|
|
@@ -203,7 +205,8 @@ def _save_selected_validation(
|
|
| 203 |
def create_app() -> gr.Blocks:
|
| 204 |
service = _seed_service()
|
| 205 |
audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
|
| 206 |
-
|
|
|
|
| 207 |
|
| 208 |
with gr.Blocks(title="BirdNET Validator HF") as demo:
|
| 209 |
gr.Markdown("# BirdNET Validator HF")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
+
from pathlib import Path
|
| 4 |
from typing import Protocol
|
| 5 |
|
| 6 |
from src.cache.ephemeral_cache_manager import EphemeralCacheManager
|
| 7 |
from src.domain.models import Detection
|
| 8 |
+
from src.repositories.append_only_validation_repository import AppendOnlyValidationRepository
|
| 9 |
from src.repositories.in_memory_detection_repository import InMemoryDetectionRepository
|
|
|
|
| 10 |
from src.services.audio_fetch_service import AudioFetchService
|
| 11 |
from src.services.detection_queue_service import DetectionQueueService
|
| 12 |
from src.services.validation_service import ValidationService
|
|
|
|
| 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 |
+
validation_service = ValidationService(AppendOnlyValidationRepository(base_dir=validation_base_dir))
|
| 210 |
|
| 211 |
with gr.Blocks(title="BirdNET Validator HF") as demo:
|
| 212 |
gr.Markdown("# BirdNET Validator HF")
|