Spaces:
Sleeping
Sleeping
Commit ·
6b16157
1
Parent(s): cd7d464
feat: align validation loader with uploader index schema
Browse files- requirements.txt +2 -0
- src/ui/app_factory.py +95 -0
- tests/unit/test_app_factory_audio_helpers.py +63 -0
requirements.txt
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
gradio==5.23.1
|
| 2 |
huggingface_hub==0.28.1
|
| 3 |
numpy>=2.0.0
|
|
|
|
| 4 |
Pillow>=10.4.0
|
|
|
|
| 5 |
pydantic>=2.8.0
|
| 6 |
pytest>=8.3.0
|
|
|
|
| 1 |
gradio==5.23.1
|
| 2 |
huggingface_hub==0.28.1
|
| 3 |
numpy>=2.0.0
|
| 4 |
+
pandas>=2.2.0
|
| 5 |
Pillow>=10.4.0
|
| 6 |
+
pyarrow>=16.0.0
|
| 7 |
pydantic>=2.8.0
|
| 8 |
pytest>=8.3.0
|
src/ui/app_factory.py
CHANGED
|
@@ -139,6 +139,8 @@ def _build_detection_from_row(raw: dict[str, object], row_index: int, project_sl
|
|
| 139 |
_pick_row_value(
|
| 140 |
raw,
|
| 141 |
[
|
|
|
|
|
|
|
| 142 |
"audio_id",
|
| 143 |
"audio_file",
|
| 144 |
"audio_path",
|
|
@@ -249,6 +251,15 @@ def _load_dataset_detections_for_project(project: Project) -> tuple[list[Detecti
|
|
| 249 |
if not repo_files:
|
| 250 |
return [], f"⚠️ Dataset {dataset_repo} has no files."
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
preferred = _candidate_metadata_files(project.project_slug)
|
| 253 |
selected_file = next((name for name in preferred if name in repo_files), "")
|
| 254 |
if not selected_file:
|
|
@@ -323,6 +334,90 @@ def _load_dataset_detections_for_project(project: Project) -> tuple[list[Detecti
|
|
| 323 |
return parsed, ""
|
| 324 |
|
| 325 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
def _parse_segment_filename_hint(filename: str) -> tuple[float, float, float]:
|
| 327 |
# Common uploader pattern: ..._12.0-15.0s_85%.wav
|
| 328 |
segment_match = re.search(r"_(\d+(?:\.\d+)?)\-(\d+(?:\.\d+)?)s_(\d+(?:\.\d+)?)%", filename)
|
|
|
|
| 139 |
_pick_row_value(
|
| 140 |
raw,
|
| 141 |
[
|
| 142 |
+
"segment_path_in_repo",
|
| 143 |
+
"segment_relpath",
|
| 144 |
"audio_id",
|
| 145 |
"audio_file",
|
| 146 |
"audio_path",
|
|
|
|
| 251 |
if not repo_files:
|
| 252 |
return [], f"⚠️ Dataset {dataset_repo} has no files."
|
| 253 |
|
| 254 |
+
shard_detections, shard_warning = _load_detections_from_parquet_shards(
|
| 255 |
+
project=project,
|
| 256 |
+
dataset_repo=dataset_repo,
|
| 257 |
+
token=token,
|
| 258 |
+
repo_files=repo_files,
|
| 259 |
+
)
|
| 260 |
+
if shard_detections:
|
| 261 |
+
return shard_detections, shard_warning
|
| 262 |
+
|
| 263 |
preferred = _candidate_metadata_files(project.project_slug)
|
| 264 |
selected_file = next((name for name in preferred if name in repo_files), "")
|
| 265 |
if not selected_file:
|
|
|
|
| 334 |
return parsed, ""
|
| 335 |
|
| 336 |
|
| 337 |
+
def _resolve_shard_paths_from_repo_files(repo_files: list[str]) -> list[str]:
|
| 338 |
+
return sorted(
|
| 339 |
+
{
|
| 340 |
+
file_path
|
| 341 |
+
for file_path in repo_files
|
| 342 |
+
if str(file_path).lower().startswith("index/shards/") and str(file_path).lower().endswith(".parquet")
|
| 343 |
+
}
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
def _load_detections_from_parquet_shards(
|
| 348 |
+
project: Project,
|
| 349 |
+
dataset_repo: str,
|
| 350 |
+
token: str | None,
|
| 351 |
+
repo_files: list[str],
|
| 352 |
+
) -> tuple[list[Detection], str]:
|
| 353 |
+
shard_paths = _resolve_shard_paths_from_repo_files(repo_files)
|
| 354 |
+
|
| 355 |
+
if "manifest.json" in repo_files:
|
| 356 |
+
try:
|
| 357 |
+
if token:
|
| 358 |
+
manifest_path = hf_hub_download(
|
| 359 |
+
repo_id=dataset_repo,
|
| 360 |
+
repo_type="dataset",
|
| 361 |
+
filename="manifest.json",
|
| 362 |
+
token=token,
|
| 363 |
+
)
|
| 364 |
+
else:
|
| 365 |
+
manifest_path = hf_hub_download(
|
| 366 |
+
repo_id=dataset_repo,
|
| 367 |
+
repo_type="dataset",
|
| 368 |
+
filename="manifest.json",
|
| 369 |
+
)
|
| 370 |
+
manifest_payload = json.loads(Path(manifest_path).read_text(encoding="utf-8"))
|
| 371 |
+
manifest_shards = manifest_payload.get("index", {}).get("shards", [])
|
| 372 |
+
if isinstance(manifest_shards, list):
|
| 373 |
+
manifest_paths = [
|
| 374 |
+
str(item.get("path", "")).strip()
|
| 375 |
+
for item in manifest_shards
|
| 376 |
+
if isinstance(item, dict)
|
| 377 |
+
]
|
| 378 |
+
manifest_paths = [p for p in manifest_paths if p.lower().endswith(".parquet")]
|
| 379 |
+
if manifest_paths:
|
| 380 |
+
shard_paths = manifest_paths
|
| 381 |
+
except Exception:
|
| 382 |
+
pass
|
| 383 |
+
|
| 384 |
+
if not shard_paths:
|
| 385 |
+
return [], ""
|
| 386 |
+
|
| 387 |
+
try:
|
| 388 |
+
import pandas as pd # type: ignore[import-not-found]
|
| 389 |
+
except Exception:
|
| 390 |
+
return [], (
|
| 391 |
+
f"⚠️ Dataset {dataset_repo} contains parquet index shards, but pandas/pyarrow are unavailable to read them."
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
rows: list[dict[str, object]] = []
|
| 395 |
+
for shard_path in shard_paths:
|
| 396 |
+
try:
|
| 397 |
+
if token:
|
| 398 |
+
downloaded = hf_hub_download(
|
| 399 |
+
repo_id=dataset_repo,
|
| 400 |
+
repo_type="dataset",
|
| 401 |
+
filename=shard_path,
|
| 402 |
+
token=token,
|
| 403 |
+
)
|
| 404 |
+
else:
|
| 405 |
+
downloaded = hf_hub_download(
|
| 406 |
+
repo_id=dataset_repo,
|
| 407 |
+
repo_type="dataset",
|
| 408 |
+
filename=shard_path,
|
| 409 |
+
)
|
| 410 |
+
frame = pd.read_parquet(downloaded)
|
| 411 |
+
rows.extend(frame.to_dict(orient="records"))
|
| 412 |
+
except Exception:
|
| 413 |
+
continue
|
| 414 |
+
|
| 415 |
+
parsed = _parse_detection_metadata_payload(rows, project.project_slug)
|
| 416 |
+
if not parsed:
|
| 417 |
+
return [], f"⚠️ Dataset {dataset_repo} index shards were found but contain no rows for project {project.project_slug}."
|
| 418 |
+
return parsed, ""
|
| 419 |
+
|
| 420 |
+
|
| 421 |
def _parse_segment_filename_hint(filename: str) -> tuple[float, float, float]:
|
| 422 |
# Common uploader pattern: ..._12.0-15.0s_85%.wav
|
| 423 |
segment_match = re.search(r"_(\d+(?:\.\d+)?)\-(\d+(?:\.\d+)?)s_(\d+(?:\.\d+)?)%", filename)
|
tests/unit/test_app_factory_audio_helpers.py
CHANGED
|
@@ -984,3 +984,66 @@ def test_parse_segment_filename_hint_reads_time_and_confidence() -> None:
|
|
| 984 |
assert start == 12.0
|
| 985 |
assert end == 15.0
|
| 986 |
assert conf == 0.85
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 984 |
assert start == 12.0
|
| 985 |
assert end == 15.0
|
| 986 |
assert conf == 0.85
|
| 987 |
+
|
| 988 |
+
|
| 989 |
+
def test_build_detection_from_row_prefers_segment_path_for_audio_id() -> None:
|
| 990 |
+
from src.ui import app_factory as module
|
| 991 |
+
|
| 992 |
+
row = {
|
| 993 |
+
"project_slug": "project-a",
|
| 994 |
+
"segment_path_in_repo": "audio/segments/species_a/example.wav",
|
| 995 |
+
"audio_id": "source_stem_only",
|
| 996 |
+
"scientific_name": "Species A",
|
| 997 |
+
"confidence": 0.77,
|
| 998 |
+
"start_time": 0.0,
|
| 999 |
+
"end_time": 3.0,
|
| 1000 |
+
}
|
| 1001 |
+
|
| 1002 |
+
detection = module._build_detection_from_row(row, 0, "project-a")
|
| 1003 |
+
|
| 1004 |
+
assert detection is not None
|
| 1005 |
+
assert detection.audio_id == "segments/species_a/example.wav"
|
| 1006 |
+
|
| 1007 |
+
|
| 1008 |
+
def test_load_dataset_detections_for_project_uses_parquet_shards_when_available(monkeypatch: pytest.MonkeyPatch) -> None:
|
| 1009 |
+
from src.ui import app_factory as module
|
| 1010 |
+
|
| 1011 |
+
class FakeHfApi:
|
| 1012 |
+
def __init__(self, token: str | None = None) -> None:
|
| 1013 |
+
_ = token
|
| 1014 |
+
|
| 1015 |
+
def list_repo_files(self, repo_id: str, repo_type: str = "dataset") -> list[str]:
|
| 1016 |
+
_ = repo_id
|
| 1017 |
+
_ = repo_type
|
| 1018 |
+
return ["manifest.json", "index/shards/shard-00000.parquet"]
|
| 1019 |
+
|
| 1020 |
+
expected = [
|
| 1021 |
+
Detection(
|
| 1022 |
+
detection_key="0000000000003333",
|
| 1023 |
+
audio_id="segments/species_a/example.wav",
|
| 1024 |
+
scientific_name="Species A",
|
| 1025 |
+
confidence=0.9,
|
| 1026 |
+
start_time=0.0,
|
| 1027 |
+
end_time=3.0,
|
| 1028 |
+
)
|
| 1029 |
+
]
|
| 1030 |
+
|
| 1031 |
+
monkeypatch.setattr(module, "HfApi", FakeHfApi)
|
| 1032 |
+
monkeypatch.setattr(
|
| 1033 |
+
module,
|
| 1034 |
+
"_load_detections_from_parquet_shards",
|
| 1035 |
+
lambda project, dataset_repo, token, repo_files: (expected, ""),
|
| 1036 |
+
)
|
| 1037 |
+
|
| 1038 |
+
project = Project(
|
| 1039 |
+
project_slug="project-a",
|
| 1040 |
+
name="Project A",
|
| 1041 |
+
dataset_repo_id="org/project-a",
|
| 1042 |
+
active=True,
|
| 1043 |
+
)
|
| 1044 |
+
|
| 1045 |
+
detections, warning = module._load_dataset_detections_for_project(project)
|
| 1046 |
+
|
| 1047 |
+
assert warning == ""
|
| 1048 |
+
assert len(detections) == 1
|
| 1049 |
+
assert detections[0].scientific_name == "Species A"
|