Florent Gbelidji commited on
Sync DeepSeek OCR HF job code
Browse files- ds_batch_ocr/stages.py +43 -9
ds_batch_ocr/stages.py
CHANGED
|
@@ -7,7 +7,7 @@ from pathlib import Path
|
|
| 7 |
from typing import Any, Dict, List, Optional
|
| 8 |
|
| 9 |
import shutil
|
| 10 |
-
from datasets import Dataset, Features, Sequence, Value, load_dataset
|
| 11 |
from PIL import Image, ImageOps
|
| 12 |
from torch.utils.data import DataLoader
|
| 13 |
|
|
@@ -61,6 +61,27 @@ def _coerce_to_str(value: Any) -> str:
|
|
| 61 |
return str(value)
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def _normalize_figures(figures: Any) -> List[Dict[str, str]]:
|
| 65 |
if not figures:
|
| 66 |
return []
|
|
@@ -182,9 +203,9 @@ def _dataset_features() -> Features:
|
|
| 182 |
"document_markdown_text": Value("string"),
|
| 183 |
"document_final_markdown_path": Value("string"),
|
| 184 |
"document_final_markdown_text": Value("string"),
|
| 185 |
-
"document_with_boxes_image_path":
|
| 186 |
"raw_response_path": Value("string"),
|
| 187 |
-
"source_image_path":
|
| 188 |
# "figures": Sequence(
|
| 189 |
# {
|
| 190 |
# "figure_id": Value("string"),
|
|
@@ -205,6 +226,12 @@ def _build_dataset_records(documents: List[Dict[str, Any]]) -> List[Dict[str, An
|
|
| 205 |
for doc in documents:
|
| 206 |
figures = _normalize_figures(doc.get("figures", []))
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
records.append(
|
| 209 |
{
|
| 210 |
"sample_id": doc.get("sample_id", ""),
|
|
@@ -213,7 +240,7 @@ def _build_dataset_records(documents: List[Dict[str, Any]]) -> List[Dict[str, An
|
|
| 213 |
"document_markdown_text": doc.get("document_markdown_text") or "",
|
| 214 |
"document_final_markdown_path": doc.get("document_final_markdown_path") or "",
|
| 215 |
"document_final_markdown_text": doc.get("document_final_markdown_text") or "",
|
| 216 |
-
"document_with_boxes_image_path":
|
| 217 |
"raw_response_path": doc.get("raw_response_path", ""),
|
| 218 |
"source_image_path": doc.get("source_image_path", ""),
|
| 219 |
# "figures": figures,
|
|
@@ -259,6 +286,16 @@ def _push_dataset_records(
|
|
| 259 |
)
|
| 260 |
dataset_index = 0
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
normalized_records.append(
|
| 263 |
{
|
| 264 |
"sample_id": sample_id,
|
|
@@ -275,12 +312,9 @@ def _push_dataset_records(
|
|
| 275 |
"document_final_markdown_text": _coerce_to_str(
|
| 276 |
record.get("document_final_markdown_text")
|
| 277 |
),
|
| 278 |
-
"document_with_boxes_image_path":
|
| 279 |
-
record.get("document_with_boxes_image_path")
|
| 280 |
-
or record.get("document_with_boxes_path")
|
| 281 |
-
),
|
| 282 |
"raw_response_path": _coerce_to_str(record.get("raw_response_path")),
|
| 283 |
-
"source_image_path":
|
| 284 |
#"figures": _normalize_figures(record.get("figures", [])),
|
| 285 |
}
|
| 286 |
)
|
|
|
|
| 7 |
from typing import Any, Dict, List, Optional
|
| 8 |
|
| 9 |
import shutil
|
| 10 |
+
from datasets import Dataset, Features, Sequence, Value, load_dataset, Image as HfImage
|
| 11 |
from PIL import Image, ImageOps
|
| 12 |
from torch.utils.data import DataLoader
|
| 13 |
|
|
|
|
| 61 |
return str(value)
|
| 62 |
|
| 63 |
|
| 64 |
+
def _resolve_image_path(base_dir: Path, value: Any) -> str:
|
| 65 |
+
path_str = _coerce_to_str(value)
|
| 66 |
+
if not path_str:
|
| 67 |
+
return ""
|
| 68 |
+
|
| 69 |
+
path = Path(path_str)
|
| 70 |
+
if not path.is_absolute():
|
| 71 |
+
path = base_dir / path
|
| 72 |
+
|
| 73 |
+
if path.suffix.lower() != ".png":
|
| 74 |
+
png_candidate = path.with_suffix(".png")
|
| 75 |
+
if png_candidate.exists():
|
| 76 |
+
path = png_candidate
|
| 77 |
+
|
| 78 |
+
if not path.exists():
|
| 79 |
+
LOGGER.warning("Image asset missing when preparing dataset | path=%s", path)
|
| 80 |
+
return path.as_posix()
|
| 81 |
+
|
| 82 |
+
return path.as_posix()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
def _normalize_figures(figures: Any) -> List[Dict[str, str]]:
|
| 86 |
if not figures:
|
| 87 |
return []
|
|
|
|
| 203 |
"document_markdown_text": Value("string"),
|
| 204 |
"document_final_markdown_path": Value("string"),
|
| 205 |
"document_final_markdown_text": Value("string"),
|
| 206 |
+
"document_with_boxes_image_path": HfImage(),
|
| 207 |
"raw_response_path": Value("string"),
|
| 208 |
+
"source_image_path": HfImage(),
|
| 209 |
# "figures": Sequence(
|
| 210 |
# {
|
| 211 |
# "figure_id": Value("string"),
|
|
|
|
| 226 |
for doc in documents:
|
| 227 |
figures = _normalize_figures(doc.get("figures", []))
|
| 228 |
|
| 229 |
+
document_with_boxes_relpath = _coerce_to_str(doc.get("document_with_boxes_path"))
|
| 230 |
+
if document_with_boxes_relpath:
|
| 231 |
+
relpath_obj = Path(document_with_boxes_relpath)
|
| 232 |
+
if relpath_obj.suffix.lower() != ".png":
|
| 233 |
+
document_with_boxes_relpath = relpath_obj.with_suffix(".png").as_posix()
|
| 234 |
+
|
| 235 |
records.append(
|
| 236 |
{
|
| 237 |
"sample_id": doc.get("sample_id", ""),
|
|
|
|
| 240 |
"document_markdown_text": doc.get("document_markdown_text") or "",
|
| 241 |
"document_final_markdown_path": doc.get("document_final_markdown_path") or "",
|
| 242 |
"document_final_markdown_text": doc.get("document_final_markdown_text") or "",
|
| 243 |
+
"document_with_boxes_image_path": document_with_boxes_relpath,
|
| 244 |
"raw_response_path": doc.get("raw_response_path", ""),
|
| 245 |
"source_image_path": doc.get("source_image_path", ""),
|
| 246 |
# "figures": figures,
|
|
|
|
| 286 |
)
|
| 287 |
dataset_index = 0
|
| 288 |
|
| 289 |
+
document_with_boxes_image_path = _resolve_image_path(
|
| 290 |
+
output_dir,
|
| 291 |
+
record.get("document_with_boxes_image_path")
|
| 292 |
+
or record.get("document_with_boxes_path")
|
| 293 |
+
)
|
| 294 |
+
source_image_path = _resolve_image_path(
|
| 295 |
+
output_dir,
|
| 296 |
+
record.get("source_image_path")
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
normalized_records.append(
|
| 300 |
{
|
| 301 |
"sample_id": sample_id,
|
|
|
|
| 312 |
"document_final_markdown_text": _coerce_to_str(
|
| 313 |
record.get("document_final_markdown_text")
|
| 314 |
),
|
| 315 |
+
"document_with_boxes_image_path": document_with_boxes_image_path,
|
|
|
|
|
|
|
|
|
|
| 316 |
"raw_response_path": _coerce_to_str(record.get("raw_response_path")),
|
| 317 |
+
"source_image_path": source_image_path,
|
| 318 |
#"figures": _normalize_figures(record.get("figures", [])),
|
| 319 |
}
|
| 320 |
)
|