Spaces:
Sleeping
Sleeping
Commit Β·
d20ce7e
1
Parent(s): 0aec723
feat(ui): accept segments zip upload for HF Space dry-run and upload
Browse files- src/ui/upload_app_factory.py +47 -7
src/ui/upload_app_factory.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import json
|
|
|
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Any
|
| 4 |
|
|
@@ -19,6 +21,23 @@ def _build_api(token: str) -> HfApi:
|
|
| 19 |
return HfApi()
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def build_upload_app() -> gr.Blocks:
|
| 23 |
with gr.Blocks(title="BirdNET Segments Uploader") as demo:
|
| 24 |
gr.Markdown("# BirdNET Segments Uploader")
|
|
@@ -36,6 +55,15 @@ def build_upload_app() -> gr.Blocks:
|
|
| 36 |
label="Pasta raiz de segmentos",
|
| 37 |
placeholder=r"ex: C:\dados\BirdNET Segments",
|
| 38 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with gr.Accordion("Configuracao avancada", open=False):
|
| 41 |
hf_token = gr.Textbox(
|
|
@@ -70,6 +98,7 @@ def build_upload_app() -> gr.Blocks:
|
|
| 70 |
repo: str,
|
| 71 |
csv_path: str | None,
|
| 72 |
segments_path: str,
|
|
|
|
| 73 |
report_path: str,
|
| 74 |
) -> tuple[str, str]:
|
| 75 |
project = (project or "").strip()
|
|
@@ -79,14 +108,15 @@ def build_upload_app() -> gr.Blocks:
|
|
| 79 |
if not csv_path:
|
| 80 |
return "β Selecione o CSV de deteccoes", "{}"
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
try:
|
| 86 |
result = run_ingest_segments_dry_run(
|
| 87 |
project_slug=project,
|
| 88 |
detections_csv=csv_path,
|
| 89 |
-
segments_root=
|
| 90 |
)
|
| 91 |
if report_path.strip():
|
| 92 |
Path(report_path).write_text(_as_pretty_json(result), encoding="utf-8")
|
|
@@ -99,12 +129,16 @@ def build_upload_app() -> gr.Blocks:
|
|
| 99 |
return summary, _as_pretty_json(result)
|
| 100 |
except Exception as exc:
|
| 101 |
return f"β Dry-run falhou: {exc}", "{}"
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def run_upload(
|
| 104 |
project: str,
|
| 105 |
repo: str,
|
| 106 |
csv_path: str | None,
|
| 107 |
segments_path: str,
|
|
|
|
| 108 |
token: str,
|
| 109 |
batch: float,
|
| 110 |
shard: float,
|
|
@@ -121,8 +155,10 @@ def build_upload_app() -> gr.Blocks:
|
|
| 121 |
return "β Informe dataset repo no formato owner/repo", "{}"
|
| 122 |
if not csv_path:
|
| 123 |
return "β Selecione o CSV de deteccoes", "{}"
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
try:
|
| 128 |
api = _build_api(token)
|
|
@@ -131,7 +167,7 @@ def build_upload_app() -> gr.Blocks:
|
|
| 131 |
project_slug=project,
|
| 132 |
dataset_repo=repo,
|
| 133 |
detections_csv=csv_path,
|
| 134 |
-
segments_root=
|
| 135 |
batch_size=int(batch),
|
| 136 |
shard_size=int(shard),
|
| 137 |
max_retries=int(retries),
|
|
@@ -149,10 +185,13 @@ def build_upload_app() -> gr.Blocks:
|
|
| 149 |
return summary, _as_pretty_json(result)
|
| 150 |
except Exception as exc:
|
| 151 |
return f"β Upload falhou: {exc}", "{}"
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
dry_run_button.click(
|
| 154 |
fn=run_dry_run,
|
| 155 |
-
inputs=[project_slug, dataset_repo, detections_csv, segments_root, report_file],
|
| 156 |
outputs=[status, result_json],
|
| 157 |
)
|
| 158 |
|
|
@@ -163,6 +202,7 @@ def build_upload_app() -> gr.Blocks:
|
|
| 163 |
dataset_repo,
|
| 164 |
detections_csv,
|
| 165 |
segments_root,
|
|
|
|
| 166 |
hf_token,
|
| 167 |
batch_size,
|
| 168 |
shard_size,
|
|
|
|
| 1 |
import json
|
| 2 |
+
import tempfile
|
| 3 |
+
import zipfile
|
| 4 |
from pathlib import Path
|
| 5 |
from typing import Any
|
| 6 |
|
|
|
|
| 21 |
return HfApi()
|
| 22 |
|
| 23 |
|
| 24 |
+
def _resolve_segments_root(
|
| 25 |
+
segments_path: str,
|
| 26 |
+
segments_zip_path: str | None,
|
| 27 |
+
) -> tuple[Path | None, tempfile.TemporaryDirectory[str] | None]:
|
| 28 |
+
clean_segments_path = (segments_path or "").strip()
|
| 29 |
+
if clean_segments_path and Path(clean_segments_path).exists():
|
| 30 |
+
return Path(clean_segments_path), None
|
| 31 |
+
|
| 32 |
+
if segments_zip_path and Path(segments_zip_path).exists():
|
| 33 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 34 |
+
with zipfile.ZipFile(segments_zip_path) as archive:
|
| 35 |
+
archive.extractall(temp_dir.name)
|
| 36 |
+
return Path(temp_dir.name), temp_dir
|
| 37 |
+
|
| 38 |
+
return None, None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
def build_upload_app() -> gr.Blocks:
|
| 42 |
with gr.Blocks(title="BirdNET Segments Uploader") as demo:
|
| 43 |
gr.Markdown("# BirdNET Segments Uploader")
|
|
|
|
| 55 |
label="Pasta raiz de segmentos",
|
| 56 |
placeholder=r"ex: C:\dados\BirdNET Segments",
|
| 57 |
)
|
| 58 |
+
segments_zip = gr.File(
|
| 59 |
+
label="ZIP da pasta de segmentos (use no Space)",
|
| 60 |
+
file_types=[".zip"],
|
| 61 |
+
type="filepath",
|
| 62 |
+
)
|
| 63 |
+
gr.Markdown(
|
| 64 |
+
"No Hugging Face Space, caminho local do seu PC (ex: C:/...) nao existe no servidor. "
|
| 65 |
+
"Use o campo ZIP para enviar a pasta de segmentos."
|
| 66 |
+
)
|
| 67 |
|
| 68 |
with gr.Accordion("Configuracao avancada", open=False):
|
| 69 |
hf_token = gr.Textbox(
|
|
|
|
| 98 |
repo: str,
|
| 99 |
csv_path: str | None,
|
| 100 |
segments_path: str,
|
| 101 |
+
segments_zip_path: str | None,
|
| 102 |
report_path: str,
|
| 103 |
) -> tuple[str, str]:
|
| 104 |
project = (project or "").strip()
|
|
|
|
| 108 |
if not csv_path:
|
| 109 |
return "β Selecione o CSV de deteccoes", "{}"
|
| 110 |
|
| 111 |
+
segments_root, temp_dir = _resolve_segments_root(segments_path, segments_zip_path)
|
| 112 |
+
if segments_root is None:
|
| 113 |
+
return "β Pasta de segmentos invalida. No Space, envie ZIP da pasta.", "{}"
|
| 114 |
|
| 115 |
try:
|
| 116 |
result = run_ingest_segments_dry_run(
|
| 117 |
project_slug=project,
|
| 118 |
detections_csv=csv_path,
|
| 119 |
+
segments_root=str(segments_root),
|
| 120 |
)
|
| 121 |
if report_path.strip():
|
| 122 |
Path(report_path).write_text(_as_pretty_json(result), encoding="utf-8")
|
|
|
|
| 129 |
return summary, _as_pretty_json(result)
|
| 130 |
except Exception as exc:
|
| 131 |
return f"β Dry-run falhou: {exc}", "{}"
|
| 132 |
+
finally:
|
| 133 |
+
if temp_dir is not None:
|
| 134 |
+
temp_dir.cleanup()
|
| 135 |
|
| 136 |
def run_upload(
|
| 137 |
project: str,
|
| 138 |
repo: str,
|
| 139 |
csv_path: str | None,
|
| 140 |
segments_path: str,
|
| 141 |
+
segments_zip_path: str | None,
|
| 142 |
token: str,
|
| 143 |
batch: float,
|
| 144 |
shard: float,
|
|
|
|
| 155 |
return "β Informe dataset repo no formato owner/repo", "{}"
|
| 156 |
if not csv_path:
|
| 157 |
return "β Selecione o CSV de deteccoes", "{}"
|
| 158 |
+
|
| 159 |
+
segments_root, temp_dir = _resolve_segments_root(segments_path, segments_zip_path)
|
| 160 |
+
if segments_root is None:
|
| 161 |
+
return "β Pasta de segmentos invalida. No Space, envie ZIP da pasta.", "{}"
|
| 162 |
|
| 163 |
try:
|
| 164 |
api = _build_api(token)
|
|
|
|
| 167 |
project_slug=project,
|
| 168 |
dataset_repo=repo,
|
| 169 |
detections_csv=csv_path,
|
| 170 |
+
segments_root=str(segments_root),
|
| 171 |
batch_size=int(batch),
|
| 172 |
shard_size=int(shard),
|
| 173 |
max_retries=int(retries),
|
|
|
|
| 185 |
return summary, _as_pretty_json(result)
|
| 186 |
except Exception as exc:
|
| 187 |
return f"β Upload falhou: {exc}", "{}"
|
| 188 |
+
finally:
|
| 189 |
+
if temp_dir is not None:
|
| 190 |
+
temp_dir.cleanup()
|
| 191 |
|
| 192 |
dry_run_button.click(
|
| 193 |
fn=run_dry_run,
|
| 194 |
+
inputs=[project_slug, dataset_repo, detections_csv, segments_root, segments_zip, report_file],
|
| 195 |
outputs=[status, result_json],
|
| 196 |
)
|
| 197 |
|
|
|
|
| 202 |
dataset_repo,
|
| 203 |
detections_csv,
|
| 204 |
segments_root,
|
| 205 |
+
segments_zip,
|
| 206 |
hf_token,
|
| 207 |
batch_size,
|
| 208 |
shard_size,
|