Florent Gbelidji commited on
Sync DeepSeek OCR HF job code
Browse files- ds_batch_ocr/stages.py +120 -38
ds_batch_ocr/stages.py
CHANGED
|
@@ -208,14 +208,8 @@ def _dataset_features() -> Features:
|
|
| 208 |
"document_with_boxes_image_path": HfImage(),
|
| 209 |
"raw_response_path": Value("string"),
|
| 210 |
"source_image_path": HfImage(),
|
| 211 |
-
"figures": Sequence(
|
| 212 |
-
|
| 213 |
-
"figure_id": Value("string"),
|
| 214 |
-
"image_path": Value("string"),
|
| 215 |
-
"image": HfImage(),
|
| 216 |
-
"description": Value("string"),
|
| 217 |
-
}
|
| 218 |
-
),
|
| 219 |
}
|
| 220 |
)
|
| 221 |
|
|
@@ -236,6 +230,7 @@ def _build_dataset_records(documents: List[Dict[str, Any]]) -> List[Dict[str, An
|
|
| 236 |
document_with_boxes_relpath = relpath_obj.with_suffix(".png").as_posix()
|
| 237 |
|
| 238 |
figure_entries: List[Dict[str, Any]] = []
|
|
|
|
| 239 |
for figure in figures:
|
| 240 |
figure_image_relpath = _coerce_to_str(
|
| 241 |
figure.get("image")
|
|
@@ -247,14 +242,13 @@ def _build_dataset_records(documents: List[Dict[str, Any]]) -> List[Dict[str, An
|
|
| 247 |
figure_relpath_obj = Path(figure_image_relpath)
|
| 248 |
if figure_relpath_obj.suffix.lower() != ".png":
|
| 249 |
figure_image_relpath = figure_relpath_obj.with_suffix(".png").as_posix()
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
)
|
| 258 |
|
| 259 |
records.append(
|
| 260 |
{
|
|
@@ -268,6 +262,7 @@ def _build_dataset_records(documents: List[Dict[str, Any]]) -> List[Dict[str, An
|
|
| 268 |
"raw_response_path": doc.get("raw_response_path", ""),
|
| 269 |
"source_image_path": doc.get("source_image_path", ""),
|
| 270 |
"figures": figure_entries,
|
|
|
|
| 271 |
}
|
| 272 |
)
|
| 273 |
return records
|
|
@@ -319,26 +314,73 @@ def _push_dataset_records(
|
|
| 319 |
output_dir,
|
| 320 |
record.get("source_image_path")
|
| 321 |
)
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
if not raw_figure_path:
|
| 326 |
LOGGER.warning(
|
| 327 |
"Skipping figure with missing image reference | sample=%s | figure=%s",
|
| 328 |
sample_id or "<unknown>",
|
| 329 |
-
figure.get("figure_id"),
|
| 330 |
)
|
| 331 |
continue
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
}
|
| 341 |
-
|
| 342 |
|
| 343 |
normalized_records.append(
|
| 344 |
{
|
|
@@ -359,7 +401,8 @@ def _push_dataset_records(
|
|
| 359 |
"document_with_boxes_image_path": document_with_boxes_image_path,
|
| 360 |
"raw_response_path": _coerce_to_str(record.get("raw_response_path")),
|
| 361 |
"source_image_path": source_image_path,
|
| 362 |
-
"figures":
|
|
|
|
| 363 |
}
|
| 364 |
)
|
| 365 |
|
|
@@ -1023,6 +1066,53 @@ def publish_dataset_viewer_assets(
|
|
| 1023 |
normalized: List[Dict[str, Any]] = []
|
| 1024 |
for record in dataset_records:
|
| 1025 |
figures = record.get("figures", []) or []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1026 |
normalized.append(
|
| 1027 |
{
|
| 1028 |
"sample_id": str(record.get("sample_id", "")),
|
|
@@ -1030,15 +1120,7 @@ def publish_dataset_viewer_assets(
|
|
| 1030 |
"document_markdown_path": str(record.get("document_markdown_path", "")),
|
| 1031 |
"document_markdown_text": record.get("document_markdown_text", ""),
|
| 1032 |
"document_with_boxes_image": record.get("document_with_boxes_image_path"),
|
| 1033 |
-
"figures":
|
| 1034 |
-
{
|
| 1035 |
-
"figure_id": str(fig.get("figure_id", "")),
|
| 1036 |
-
"image_path": str(fig.get("image_path", "")),
|
| 1037 |
-
"image": str(fig.get("image") or fig.get("image_path") or ""),
|
| 1038 |
-
"description": fig.get("description", ""),
|
| 1039 |
-
}
|
| 1040 |
-
for fig in figures
|
| 1041 |
-
],
|
| 1042 |
}
|
| 1043 |
)
|
| 1044 |
|
|
|
|
| 208 |
"document_with_boxes_image_path": HfImage(),
|
| 209 |
"raw_response_path": Value("string"),
|
| 210 |
"source_image_path": HfImage(),
|
| 211 |
+
"figures": Sequence(HfImage()),
|
| 212 |
+
"figures_metadata": Sequence(Value("string")),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
}
|
| 214 |
)
|
| 215 |
|
|
|
|
| 230 |
document_with_boxes_relpath = relpath_obj.with_suffix(".png").as_posix()
|
| 231 |
|
| 232 |
figure_entries: List[Dict[str, Any]] = []
|
| 233 |
+
figure_metadata_json: List[str] = []
|
| 234 |
for figure in figures:
|
| 235 |
figure_image_relpath = _coerce_to_str(
|
| 236 |
figure.get("image")
|
|
|
|
| 242 |
figure_relpath_obj = Path(figure_image_relpath)
|
| 243 |
if figure_relpath_obj.suffix.lower() != ".png":
|
| 244 |
figure_image_relpath = figure_relpath_obj.with_suffix(".png").as_posix()
|
| 245 |
+
metadata_entry = {
|
| 246 |
+
"figure_id": _coerce_to_str(figure.get("figure_id")),
|
| 247 |
+
"image_path": figure_image_relpath,
|
| 248 |
+
"description": _coerce_to_str(figure.get("description")),
|
| 249 |
+
}
|
| 250 |
+
figure_entries.append({**metadata_entry, "image": figure_image_relpath})
|
| 251 |
+
figure_metadata_json.append(json.dumps(metadata_entry, ensure_ascii=False))
|
|
|
|
| 252 |
|
| 253 |
records.append(
|
| 254 |
{
|
|
|
|
| 262 |
"raw_response_path": doc.get("raw_response_path", ""),
|
| 263 |
"source_image_path": doc.get("source_image_path", ""),
|
| 264 |
"figures": figure_entries,
|
| 265 |
+
"figures_metadata": figure_metadata_json,
|
| 266 |
}
|
| 267 |
)
|
| 268 |
return records
|
|
|
|
| 314 |
output_dir,
|
| 315 |
record.get("source_image_path")
|
| 316 |
)
|
| 317 |
+
|
| 318 |
+
figure_images: List[str] = []
|
| 319 |
+
figure_metadata_json: List[str] = []
|
| 320 |
+
|
| 321 |
+
raw_figures = record.get("figures", []) or []
|
| 322 |
+
if isinstance(raw_figures, dict):
|
| 323 |
+
raw_figures = [raw_figures]
|
| 324 |
+
|
| 325 |
+
for figure in raw_figures:
|
| 326 |
+
raw_figure_path = _coerce_to_str(
|
| 327 |
+
(figure or {}).get("image")
|
| 328 |
+
or (figure or {}).get("image_path")
|
| 329 |
+
or (figure or {}).get("document_relative_path")
|
| 330 |
+
or (figure or {}).get("path")
|
| 331 |
+
)
|
| 332 |
if not raw_figure_path:
|
| 333 |
LOGGER.warning(
|
| 334 |
"Skipping figure with missing image reference | sample=%s | figure=%s",
|
| 335 |
sample_id or "<unknown>",
|
| 336 |
+
(figure or {}).get("figure_id"),
|
| 337 |
)
|
| 338 |
continue
|
| 339 |
|
| 340 |
+
resolved_path = _resolve_image_path(output_dir, raw_figure_path)
|
| 341 |
+
figure_images.append(resolved_path)
|
| 342 |
+
|
| 343 |
+
metadata_entry = {
|
| 344 |
+
"figure_id": _coerce_to_str((figure or {}).get("figure_id")),
|
| 345 |
+
"image_path": _coerce_to_str((figure or {}).get("image_path") or raw_figure_path),
|
| 346 |
+
"description": _coerce_to_str((figure or {}).get("description")),
|
| 347 |
+
}
|
| 348 |
+
figure_metadata_json.append(json.dumps(metadata_entry, ensure_ascii=False))
|
| 349 |
+
|
| 350 |
+
if not figure_images and record.get("figures_metadata"):
|
| 351 |
+
for metadata in record.get("figures_metadata", []):
|
| 352 |
+
parsed: Dict[str, Any]
|
| 353 |
+
if isinstance(metadata, str):
|
| 354 |
+
try:
|
| 355 |
+
parsed = json.loads(metadata)
|
| 356 |
+
except Exception:
|
| 357 |
+
LOGGER.warning(
|
| 358 |
+
"Unable to parse figure metadata JSON | sample=%s | value=%r",
|
| 359 |
+
sample_id or "<unknown>",
|
| 360 |
+
metadata,
|
| 361 |
+
)
|
| 362 |
+
continue
|
| 363 |
+
elif isinstance(metadata, dict):
|
| 364 |
+
parsed = metadata
|
| 365 |
+
else:
|
| 366 |
+
continue
|
| 367 |
+
|
| 368 |
+
raw_figure_path = _coerce_to_str(
|
| 369 |
+
parsed.get("image_path")
|
| 370 |
+
or parsed.get("image")
|
| 371 |
+
or ""
|
| 372 |
+
)
|
| 373 |
+
if not raw_figure_path:
|
| 374 |
+
continue
|
| 375 |
+
resolved_path = _resolve_image_path(output_dir, raw_figure_path)
|
| 376 |
+
figure_images.append(resolved_path)
|
| 377 |
+
|
| 378 |
+
normalized_metadata = {
|
| 379 |
+
"figure_id": _coerce_to_str(parsed.get("figure_id")),
|
| 380 |
+
"image_path": _coerce_to_str(parsed.get("image_path") or raw_figure_path),
|
| 381 |
+
"description": _coerce_to_str(parsed.get("description")),
|
| 382 |
}
|
| 383 |
+
figure_metadata_json.append(json.dumps(normalized_metadata, ensure_ascii=False))
|
| 384 |
|
| 385 |
normalized_records.append(
|
| 386 |
{
|
|
|
|
| 401 |
"document_with_boxes_image_path": document_with_boxes_image_path,
|
| 402 |
"raw_response_path": _coerce_to_str(record.get("raw_response_path")),
|
| 403 |
"source_image_path": source_image_path,
|
| 404 |
+
"figures": figure_images,
|
| 405 |
+
"figures_metadata": figure_metadata_json,
|
| 406 |
}
|
| 407 |
)
|
| 408 |
|
|
|
|
| 1066 |
normalized: List[Dict[str, Any]] = []
|
| 1067 |
for record in dataset_records:
|
| 1068 |
figures = record.get("figures", []) or []
|
| 1069 |
+
figures_metadata = record.get("figures_metadata", []) or []
|
| 1070 |
+
|
| 1071 |
+
viewer_figures: List[Dict[str, Any]] = []
|
| 1072 |
+
|
| 1073 |
+
if figures_metadata:
|
| 1074 |
+
for metadata in figures_metadata:
|
| 1075 |
+
if isinstance(metadata, str):
|
| 1076 |
+
try:
|
| 1077 |
+
parsed = json.loads(metadata)
|
| 1078 |
+
except Exception:
|
| 1079 |
+
LOGGER.warning(
|
| 1080 |
+
"Failed to parse figures_metadata entry for viewer | sample=%s | value=%r",
|
| 1081 |
+
record.get("sample_id"),
|
| 1082 |
+
metadata,
|
| 1083 |
+
)
|
| 1084 |
+
continue
|
| 1085 |
+
elif isinstance(metadata, dict):
|
| 1086 |
+
parsed = metadata
|
| 1087 |
+
else:
|
| 1088 |
+
continue
|
| 1089 |
+
|
| 1090 |
+
viewer_figures.append(
|
| 1091 |
+
{
|
| 1092 |
+
"figure_id": str(parsed.get("figure_id", "")),
|
| 1093 |
+
"image_path": str(parsed.get("image_path", "")),
|
| 1094 |
+
"image": str(parsed.get("image") or parsed.get("image_path") or ""),
|
| 1095 |
+
"description": parsed.get("description", ""),
|
| 1096 |
+
"metadata_json": json.dumps(parsed, ensure_ascii=False),
|
| 1097 |
+
}
|
| 1098 |
+
)
|
| 1099 |
+
else:
|
| 1100 |
+
for fig in figures:
|
| 1101 |
+
metadata = {
|
| 1102 |
+
"figure_id": _coerce_to_str(fig.get("figure_id")),
|
| 1103 |
+
"image_path": _coerce_to_str(fig.get("image_path")),
|
| 1104 |
+
"description": _coerce_to_str(fig.get("description")),
|
| 1105 |
+
}
|
| 1106 |
+
viewer_figures.append(
|
| 1107 |
+
{
|
| 1108 |
+
"figure_id": metadata["figure_id"],
|
| 1109 |
+
"image_path": metadata["image_path"],
|
| 1110 |
+
"image": _coerce_to_str(fig.get("image") or fig.get("image_path")),
|
| 1111 |
+
"description": metadata["description"],
|
| 1112 |
+
"metadata_json": json.dumps(metadata, ensure_ascii=False),
|
| 1113 |
+
}
|
| 1114 |
+
)
|
| 1115 |
+
|
| 1116 |
normalized.append(
|
| 1117 |
{
|
| 1118 |
"sample_id": str(record.get("sample_id", "")),
|
|
|
|
| 1120 |
"document_markdown_path": str(record.get("document_markdown_path", "")),
|
| 1121 |
"document_markdown_text": record.get("document_markdown_text", ""),
|
| 1122 |
"document_with_boxes_image": record.get("document_with_boxes_image_path"),
|
| 1123 |
+
"figures": viewer_figures,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1124 |
}
|
| 1125 |
)
|
| 1126 |
|