Use HyperView add_samples API
Browse files
demo.py
CHANGED
|
@@ -17,6 +17,7 @@ from typing import Any
|
|
| 17 |
from PIL import Image, ImageOps
|
| 18 |
|
| 19 |
import hyperview as hv
|
|
|
|
| 20 |
|
| 21 |
SPACE_DIR = Path(__file__).resolve().parent
|
| 22 |
SPACE_HOST = os.environ.get("HYPERVIEW_HOST", "127.0.0.1")
|
|
@@ -221,9 +222,7 @@ def select_visa_records() -> list[dict[str, Any]]:
|
|
| 221 |
def add_visa_samples(dataset: hv.Dataset) -> None:
|
| 222 |
existing_ids = {sample.id for sample in dataset.samples}
|
| 223 |
media_dir = media_root()
|
| 224 |
-
|
| 225 |
-
updated = 0
|
| 226 |
-
skipped = 0
|
| 227 |
for record in select_visa_records():
|
| 228 |
sample_id = safe_sample_id(record["category"], record["split_name"], record["row_index"], record["defect_label"])
|
| 229 |
destination = Path(record["local_path"]) if record.get("local_path") else media_dir / f"{sample_id}.jpg"
|
|
@@ -238,16 +237,17 @@ def add_visa_samples(dataset: hv.Dataset) -> None:
|
|
| 238 |
"defect_status": "defect_or_test_item" if record["defect_label"] else "normal_reference",
|
| 239 |
"source_dataset": "BrachioLab/visa",
|
| 240 |
}
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
| 251 |
if skipped:
|
| 252 |
print(f"Skipped {skipped} existing VisA sample rows.", flush=True)
|
| 253 |
print(f"Prepared VisA samples ({added} added, {updated} updated).", flush=True)
|
|
|
|
| 17 |
from PIL import Image, ImageOps
|
| 18 |
|
| 19 |
import hyperview as hv
|
| 20 |
+
from hyperview.core.dataset import Sample
|
| 21 |
|
| 22 |
SPACE_DIR = Path(__file__).resolve().parent
|
| 23 |
SPACE_HOST = os.environ.get("HYPERVIEW_HOST", "127.0.0.1")
|
|
|
|
| 222 |
def add_visa_samples(dataset: hv.Dataset) -> None:
|
| 223 |
existing_ids = {sample.id for sample in dataset.samples}
|
| 224 |
media_dir = media_root()
|
| 225 |
+
samples: list[Sample] = []
|
|
|
|
|
|
|
| 226 |
for record in select_visa_records():
|
| 227 |
sample_id = safe_sample_id(record["category"], record["split_name"], record["row_index"], record["defect_label"])
|
| 228 |
destination = Path(record["local_path"]) if record.get("local_path") else media_dir / f"{sample_id}.jpg"
|
|
|
|
| 237 |
"defect_status": "defect_or_test_item" if record["defect_label"] else "normal_reference",
|
| 238 |
"source_dataset": "BrachioLab/visa",
|
| 239 |
}
|
| 240 |
+
samples.append(
|
| 241 |
+
Sample(
|
| 242 |
+
id=sample_id,
|
| 243 |
+
filepath=str(destination),
|
| 244 |
+
label=record["category"],
|
| 245 |
+
metadata=metadata,
|
| 246 |
+
)
|
| 247 |
+
)
|
| 248 |
+
upserted, skipped = dataset.add_samples(samples, skip_existing=not FORCE_SAMPLE_REFRESH)
|
| 249 |
+
updated = sum(1 for sample in samples if sample.id in existing_ids) if FORCE_SAMPLE_REFRESH else 0
|
| 250 |
+
added = upserted - updated
|
| 251 |
if skipped:
|
| 252 |
print(f"Skipped {skipped} existing VisA sample rows.", flush=True)
|
| 253 |
print(f"Prepared VisA samples ({added} added, {updated} updated).", flush=True)
|