Florent Gbelidji commited on
Sync DeepSeek OCR HF job code
Browse files- ds_batch_ocr/stages.py +52 -28
ds_batch_ocr/stages.py
CHANGED
|
@@ -138,13 +138,11 @@ def _dataset_features() -> Features:
|
|
| 138 |
"source_image_path": HfImage(),
|
| 139 |
"document_with_boxes_image_path": HfImage(),
|
| 140 |
"document_markdown_text": Value("string"),
|
| 141 |
-
"figures":
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
}
|
| 147 |
-
),
|
| 148 |
"document_markdown_path": Value("string"),
|
| 149 |
"document_final_markdown_path": Value("string"),
|
| 150 |
"document_final_markdown_text": Value("string"),
|
|
@@ -157,30 +155,54 @@ def _dataset_path(base_dir: Path) -> Path:
|
|
| 157 |
return base_dir / DATASET_FILENAME
|
| 158 |
|
| 159 |
|
| 160 |
-
def
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
|
| 165 |
-
|
| 166 |
-
for figure in
|
| 167 |
if not isinstance(figure, dict):
|
| 168 |
continue
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
yield {
|
| 185 |
"sample_id": str(doc.get("sample_id")),
|
| 186 |
"dataset_index": int(doc.get("dataset_index") or 0),
|
|
@@ -230,6 +252,9 @@ def _push_dataset_records(
|
|
| 230 |
else:
|
| 231 |
data = dict(record)
|
| 232 |
|
|
|
|
|
|
|
|
|
|
| 233 |
yield data
|
| 234 |
|
| 235 |
write_jsonl_iter(dataset_path, _record_iterator())
|
|
@@ -255,8 +280,7 @@ def _load_dataset_records(path: Path) -> List[Dict[str, Any]]:
|
|
| 255 |
if not line:
|
| 256 |
continue
|
| 257 |
record = json.loads(line)
|
| 258 |
-
|
| 259 |
-
record["figures"] = []
|
| 260 |
records.append(record)
|
| 261 |
return records
|
| 262 |
|
|
|
|
| 138 |
"source_image_path": HfImage(),
|
| 139 |
"document_with_boxes_image_path": HfImage(),
|
| 140 |
"document_markdown_text": Value("string"),
|
| 141 |
+
"figures": {
|
| 142 |
+
"figure_id": Sequence(Value("string")),
|
| 143 |
+
"image_path": Sequence(Value("string")),
|
| 144 |
+
"description": Sequence(Value("string")),
|
| 145 |
+
},
|
|
|
|
|
|
|
| 146 |
"document_markdown_path": Value("string"),
|
| 147 |
"document_final_markdown_path": Value("string"),
|
| 148 |
"document_final_markdown_text": Value("string"),
|
|
|
|
| 155 |
return base_dir / DATASET_FILENAME
|
| 156 |
|
| 157 |
|
| 158 |
+
def _figures_to_columnar(figures: Optional[Iterable[Dict[str, Any]]]) -> Dict[str, List[str]]:
|
| 159 |
+
ids: List[str] = []
|
| 160 |
+
paths: List[str] = []
|
| 161 |
+
descriptions: List[str] = []
|
| 162 |
|
| 163 |
+
if figures:
|
| 164 |
+
for figure in figures:
|
| 165 |
if not isinstance(figure, dict):
|
| 166 |
continue
|
| 167 |
|
| 168 |
+
ids.append(str(figure.get("figure_id") or figure.get("id") or ""))
|
| 169 |
+
paths.append(str(figure.get("image_path") or ""))
|
| 170 |
+
descriptions.append(str(figure.get("description") or ""))
|
| 171 |
|
| 172 |
+
return {
|
| 173 |
+
"figure_id": ids,
|
| 174 |
+
"image_path": paths,
|
| 175 |
+
"description": descriptions,
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def _figures_from_columnar(figures: Optional[Dict[str, Any]]) -> List[Dict[str, str]]:
|
| 180 |
+
if not isinstance(figures, dict):
|
| 181 |
+
return []
|
| 182 |
+
|
| 183 |
+
ids = list(figures.get("figure_id") or [])
|
| 184 |
+
paths = list(figures.get("image_path") or [])
|
| 185 |
+
descriptions = list(figures.get("description") or [])
|
| 186 |
+
|
| 187 |
+
length = max(len(ids), len(paths), len(descriptions))
|
| 188 |
+
result: List[Dict[str, str]] = []
|
| 189 |
+
for idx in range(length):
|
| 190 |
+
result.append(
|
| 191 |
+
{
|
| 192 |
+
"figure_id": str(ids[idx]) if idx < len(ids) else "",
|
| 193 |
+
"image_path": str(paths[idx]) if idx < len(paths) else "",
|
| 194 |
+
"description": str(descriptions[idx]) if idx < len(descriptions) else "",
|
| 195 |
+
}
|
| 196 |
+
)
|
| 197 |
+
return result
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def _build_dataset_records_iter(documents: Iterable[Dict[str, Any]]) -> Iterable[Dict[str, Any]]:
|
| 201 |
+
for doc in documents:
|
| 202 |
+
document_with_boxes_path = doc.get("document_with_boxes_path")
|
| 203 |
+
document_with_boxes_relpath = str(document_with_boxes_path)
|
| 204 |
+
|
| 205 |
+
figure_entries = _figures_to_columnar(doc.get("figures"))
|
| 206 |
yield {
|
| 207 |
"sample_id": str(doc.get("sample_id")),
|
| 208 |
"dataset_index": int(doc.get("dataset_index") or 0),
|
|
|
|
| 252 |
else:
|
| 253 |
data = dict(record)
|
| 254 |
|
| 255 |
+
if "figures" in data and not isinstance(data["figures"], dict):
|
| 256 |
+
data["figures"] = _figures_to_columnar(data.get("figures"))
|
| 257 |
+
|
| 258 |
yield data
|
| 259 |
|
| 260 |
write_jsonl_iter(dataset_path, _record_iterator())
|
|
|
|
| 280 |
if not line:
|
| 281 |
continue
|
| 282 |
record = json.loads(line)
|
| 283 |
+
record["figures"] = _figures_from_columnar(record.get("figures"))
|
|
|
|
| 284 |
records.append(record)
|
| 285 |
return records
|
| 286 |
|