Spaces:
Sleeping
Sleeping
Download engine/scripts/import_labels.py from lorcapoul/smol-neuron-studio: direct link, hf CLI and curl.
- Browser
- Download file 3.73 kB
-
https://huggingface.co/spaces/lorcapoul/smol-neuron-studio/resolve/main/engine/scripts/import_labels.py
- Command line
-
hf download hf://spaces/lorcapoul/smol-neuron-studio/engine/scripts/import_labels.py
-
curl -L -o import_labels.py https://huggingface.co/spaces/lorcapoul/smol-neuron-studio/resolve/main/engine/scripts/import_labels.py
3.73 kB
| """Importa las etiquetas de data/labeling/batch_XXX.labels.json al catálogo. | |
| Sin argumentos importa los lotes que aún no están en el catálogo; con --all los | |
| reimporta todos (p. ej. después de regenerar el catálogo con collect.py); o se | |
| pasan archivos concretos. Valida cada etiqueta y no importa nada de un archivo | |
| con errores. | |
| Uso: cd engine && uv run python -m scripts.import_labels [--all] [--check] [archivos...] | |
| """ | |
| import argparse | |
| import sys | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from app.catalog import labeling, store | |
| ROOT = Path(__file__).resolve().parents[2] | |
| DEFAULT_CATALOG = ROOT / "data" / "catalog.sqlite" | |
| DEFAULT_FOLDER = ROOT / "data" / "labeling" | |
| LABELING_DOC = ROOT / "docs" / "labeling.md" | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| parser.add_argument("files", nargs="*", type=Path) | |
| parser.add_argument("--all", action="store_true", help="reimportar todos los .labels.json") | |
| parser.add_argument("--check", action="store_true", help="solo validar, sin escribir en el catálogo") | |
| parser.add_argument("--catalog", type=Path, default=DEFAULT_CATALOG) | |
| parser.add_argument("--folder", type=Path, default=DEFAULT_FOLDER) | |
| args = parser.parse_args() | |
| if not args.catalog.exists(): | |
| sys.exit(f"No existe {args.catalog}: corre antes `uv run python -m scripts.collect`") | |
| conn = store.open_catalog(args.catalog, writable=not args.check) | |
| if not args.check: | |
| store.ensure_labels(conn) | |
| meta = store.read_meta(conn) | |
| layers, units = int(meta["layers"]), int(meta["units"]) | |
| files = args.files or sorted(args.folder.glob("batch_*.labels.json")) | |
| if not args.files and not args.all and store.has_labels_table(conn): | |
| done = {r[0] for r in conn.execute("SELECT DISTINCT batch FROM unit_labels")} | |
| files = [f for f in files if f.name not in done] | |
| if not files: | |
| print("No hay lotes pendientes de importar (usa --all para reimportar)") | |
| return | |
| now = datetime.now(timezone.utc).isoformat(timespec="seconds") | |
| failed = False | |
| for path in files: | |
| # El .md del lote dice qué unidades le tocaban y con qué versión del prompt. | |
| md = path.with_name(path.name.replace(".labels.json", ".md")) | |
| expected = labeling.units_in_batch(md.read_text(encoding="utf-8")) if md.exists() else None | |
| version = labeling.prompt_version(md if md.exists() else LABELING_DOC) | |
| labels, errors, warnings = labeling.parse_labels(path.read_text(encoding="utf-8"), layers, units, expected) | |
| if errors: | |
| failed = True | |
| print(f"{path.name}: {len(errors)} errores, no se importa nada") | |
| for e in errors: | |
| print(f" ✗ {e}") | |
| continue | |
| if args.check: | |
| unclear = sum(l.confidence < 0.2 for l in labels) | |
| print(f"{path.name}: {len(labels)} etiquetas válidas ({unclear} con confianza < 0.2)") | |
| for w in warnings: | |
| print(f" ! {w}") | |
| continue | |
| store.write_labels( | |
| conn, [(l.layer, l.unit, l.label, l.confidence, l.notes, path.name, version, now) for l in labels] | |
| ) | |
| conn.commit() | |
| unclear = sum(l.confidence < 0.2 for l in labels) | |
| print(f"{path.name}: {len(labels)} etiquetas importadas ({unclear} con confianza < 0.2)") | |
| for w in warnings: | |
| print(f" ! {w}") | |
| if not args.check: | |
| total = conn.execute("SELECT COUNT(*) FROM unit_labels").fetchone()[0] | |
| print(f"Catálogo: {total} unidades etiquetadas") | |
| if failed: | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |