Florent Gbelidji commited on
Sync DeepSeek OCR HF job code
Browse files- ds_batch_ocr/stages.py +47 -16
ds_batch_ocr/stages.py
CHANGED
|
@@ -41,30 +41,43 @@ def write_jsonl(path: Path, rows: List[Dict[str, Any]]) -> None:
|
|
| 41 |
|
| 42 |
|
| 43 |
def _normalize_figures(figures: Any) -> List[Dict[str, str]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def _to_dict(entry: Any) -> Dict[str, str]:
|
| 45 |
figure_id = ""
|
| 46 |
image_path = ""
|
| 47 |
description = ""
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
image_path = (
|
| 52 |
-
|
| 53 |
-
or
|
| 54 |
-
or
|
| 55 |
or ""
|
| 56 |
)
|
| 57 |
-
description =
|
| 58 |
-
elif hasattr(
|
| 59 |
-
figure_id = getattr(
|
| 60 |
-
image_path = getattr(
|
| 61 |
-
description = getattr(
|
| 62 |
-
elif isinstance(
|
| 63 |
-
figure_id =
|
| 64 |
-
image_path =
|
| 65 |
-
description =
|
| 66 |
else:
|
| 67 |
-
figure_id =
|
| 68 |
|
| 69 |
return {
|
| 70 |
"figure_id": str(figure_id or ""),
|
|
@@ -202,7 +215,25 @@ def _push_dataset_records(
|
|
| 202 |
}
|
| 203 |
)
|
| 204 |
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
normalized_records.append(new_record)
|
| 207 |
|
| 208 |
dataset = Dataset.from_list(normalized_records, features=_dataset_features())
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def _normalize_figures(figures: Any) -> List[Dict[str, str]]:
|
| 44 |
+
def _unwrap(entry: Any) -> Any:
|
| 45 |
+
current = entry
|
| 46 |
+
# peel off single-item wrappers (e.g. [[{...}]]) common in some datasets
|
| 47 |
+
while isinstance(current, (list, tuple)) and len(current) == 1:
|
| 48 |
+
nested = current[0]
|
| 49 |
+
if isinstance(nested, (dict, list, tuple)) or hasattr(nested, "figure_id"):
|
| 50 |
+
current = nested
|
| 51 |
+
else:
|
| 52 |
+
break
|
| 53 |
+
return current
|
| 54 |
+
|
| 55 |
def _to_dict(entry: Any) -> Dict[str, str]:
|
| 56 |
figure_id = ""
|
| 57 |
image_path = ""
|
| 58 |
description = ""
|
| 59 |
|
| 60 |
+
normalized_entry = _unwrap(entry)
|
| 61 |
+
|
| 62 |
+
if isinstance(normalized_entry, dict):
|
| 63 |
+
figure_id = normalized_entry.get("figure_id") or normalized_entry.get("id") or ""
|
| 64 |
image_path = (
|
| 65 |
+
normalized_entry.get("image_path")
|
| 66 |
+
or normalized_entry.get("document_relative_path")
|
| 67 |
+
or normalized_entry.get("path")
|
| 68 |
or ""
|
| 69 |
)
|
| 70 |
+
description = normalized_entry.get("description") or normalized_entry.get("caption") or ""
|
| 71 |
+
elif hasattr(normalized_entry, "figure_id"):
|
| 72 |
+
figure_id = getattr(normalized_entry, "figure_id", "")
|
| 73 |
+
image_path = getattr(normalized_entry, "image_path", "") or getattr(normalized_entry, "document_relative_path", "")
|
| 74 |
+
description = getattr(normalized_entry, "description", "")
|
| 75 |
+
elif isinstance(normalized_entry, (list, tuple)):
|
| 76 |
+
figure_id = normalized_entry[0] if len(normalized_entry) > 0 else ""
|
| 77 |
+
image_path = normalized_entry[1] if len(normalized_entry) > 1 else ""
|
| 78 |
+
description = normalized_entry[2] if len(normalized_entry) > 2 else ""
|
| 79 |
else:
|
| 80 |
+
figure_id = normalized_entry
|
| 81 |
|
| 82 |
return {
|
| 83 |
"figure_id": str(figure_id or ""),
|
|
|
|
| 215 |
}
|
| 216 |
)
|
| 217 |
|
| 218 |
+
clean_figures: List[Dict[str, str]] = []
|
| 219 |
+
for fig in coerced_figures:
|
| 220 |
+
if not isinstance(fig, dict):
|
| 221 |
+
LOGGER.warning(
|
| 222 |
+
"Dropping non-dict figure entry after coercion | type=%s | value=%r",
|
| 223 |
+
type(fig),
|
| 224 |
+
fig,
|
| 225 |
+
)
|
| 226 |
+
continue
|
| 227 |
+
|
| 228 |
+
clean_figures.append(
|
| 229 |
+
{
|
| 230 |
+
"figure_id": str(fig.get("figure_id", "") or ""),
|
| 231 |
+
"image_path": str(fig.get("image_path", "") or ""),
|
| 232 |
+
"description": str(fig.get("description", "") or ""),
|
| 233 |
+
}
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
new_record["figures"] = clean_figures
|
| 237 |
normalized_records.append(new_record)
|
| 238 |
|
| 239 |
dataset = Dataset.from_list(normalized_records, features=_dataset_features())
|