Florent Gbelidji commited on
Commit
25c89c2
·
verified ·
1 Parent(s): f435d98

Sync DeepSeek OCR HF job code

Browse files
Files changed (1) hide show
  1. 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
- if isinstance(entry, dict):
50
- figure_id = entry.get("figure_id") or entry.get("id") or ""
 
 
51
  image_path = (
52
- entry.get("image_path")
53
- or entry.get("document_relative_path")
54
- or entry.get("path")
55
  or ""
56
  )
57
- description = entry.get("description") or entry.get("caption") or ""
58
- elif hasattr(entry, "figure_id"):
59
- figure_id = getattr(entry, "figure_id", "")
60
- image_path = getattr(entry, "image_path", "") or getattr(entry, "document_relative_path", "")
61
- description = getattr(entry, "description", "")
62
- elif isinstance(entry, (list, tuple)):
63
- figure_id = entry[0] if len(entry) > 0 else ""
64
- image_path = entry[1] if len(entry) > 1 else ""
65
- description = entry[2] if len(entry) > 2 else ""
66
  else:
67
- figure_id = entry
68
 
69
  return {
70
  "figure_id": str(figure_id or ""),
@@ -202,7 +215,25 @@ def _push_dataset_records(
202
  }
203
  )
204
 
205
- new_record["figures"] = coerced_figures
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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())