Florent Gbelidji commited on
Sync DeepSeek OCR HF job code
Browse files- ds_batch_ocr/hf_io.py +1 -0
- ds_batch_ocr/stages.py +6 -26
ds_batch_ocr/hf_io.py
CHANGED
|
@@ -155,6 +155,7 @@ def maybe_upload_dataset(
|
|
| 155 |
path_in_repo: str,
|
| 156 |
commit_message: Optional[str],
|
| 157 |
revision: Optional[str],
|
|
|
|
| 158 |
) -> None:
|
| 159 |
if not repo_id:
|
| 160 |
LOGGER.info("No dataset repo provided; skipping upload.")
|
|
|
|
| 155 |
path_in_repo: str,
|
| 156 |
commit_message: Optional[str],
|
| 157 |
revision: Optional[str],
|
| 158 |
+
exclude_files: Optional[List[str]] = None,
|
| 159 |
) -> None:
|
| 160 |
if not repo_id:
|
| 161 |
LOGGER.info("No dataset repo provided; skipping upload.")
|
ds_batch_ocr/stages.py
CHANGED
|
@@ -53,31 +53,6 @@ def _dataset_features() -> Features:
|
|
| 53 |
def _dataset_path(base_dir: Path) -> Path:
|
| 54 |
return base_dir / DATASET_FILENAME
|
| 55 |
|
| 56 |
-
|
| 57 |
-
def _build_dataset_records_iter(documents: Iterable[Dict[str, Any]]) -> Iterable[Dict[str, Any]]:
|
| 58 |
-
for doc in documents:
|
| 59 |
-
document_with_boxes_path = doc.get("document_with_boxes_path")
|
| 60 |
-
document_with_boxes_relpath = str(document_with_boxes_path)
|
| 61 |
-
|
| 62 |
-
yield {
|
| 63 |
-
"sample_id": str(doc.get("sample_id")),
|
| 64 |
-
"dataset_index": int(doc.get("dataset_index") or 0),
|
| 65 |
-
"source_image_path": str(doc.get("source_image_path") or ""),
|
| 66 |
-
"document_with_boxes_image_path": document_with_boxes_relpath,
|
| 67 |
-
"document_markdown_text": doc.get("document_markdown_text") or "",
|
| 68 |
-
"extracted_figures": doc.get("extracted_figures") or [],
|
| 69 |
-
"extracted_figures_metadata": json.dumps(doc.get("extracted_figures_metadata") or []),
|
| 70 |
-
"document_markdown_path": str(doc.get("document_path") or ""),
|
| 71 |
-
"document_final_markdown_path": str(doc.get("document_final_markdown_path") or ""),
|
| 72 |
-
"document_final_markdown_text": doc.get("document_final_markdown_text") or "",
|
| 73 |
-
"raw_response_path": str(doc.get("raw_response_path") or ""),
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def _build_dataset_records(documents: Iterable[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 78 |
-
return list(_build_dataset_records_iter(documents))
|
| 79 |
-
|
| 80 |
-
|
| 81 |
def _push_dataset_records(
|
| 82 |
records_files: List[str],
|
| 83 |
output_dir: Path,
|
|
@@ -282,6 +257,7 @@ def run_stage_extract(settings: ExtractSettings) -> None:
|
|
| 282 |
if hasattr(image_obj, "close"):
|
| 283 |
image_obj.close()
|
| 284 |
|
|
|
|
| 285 |
if batch_document_dicts:
|
| 286 |
batch_file = documents_batches_dir / f"batch_{batch_index:05d}.json"
|
| 287 |
write_json(batch_file, batch_document_dicts)
|
|
@@ -296,7 +272,8 @@ def run_stage_extract(settings: ExtractSettings) -> None:
|
|
| 296 |
for idx, sample in enumerate(sample_iterator):
|
| 297 |
if settings.max_samples is not None and idx >= settings.max_samples:
|
| 298 |
break
|
| 299 |
-
|
|
|
|
| 300 |
sample_id = f"sample_{idx:05d}"
|
| 301 |
sample_dir = settings.output_dir / sample_id
|
| 302 |
print(f"Sample directory: {str(sample_dir)}")
|
|
@@ -376,6 +353,9 @@ def run_stage_extract(settings: ExtractSettings) -> None:
|
|
| 376 |
|
| 377 |
dataset = load_dataset("json", data_files=document_batch_files, features=_dataset_features())
|
| 378 |
|
|
|
|
|
|
|
|
|
|
| 379 |
token = os.environ.get("HF_TOKEN", None)
|
| 380 |
|
| 381 |
maybe_upload_dataset(
|
|
|
|
| 53 |
def _dataset_path(base_dir: Path) -> Path:
|
| 54 |
return base_dir / DATASET_FILENAME
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def _push_dataset_records(
|
| 57 |
records_files: List[str],
|
| 58 |
output_dir: Path,
|
|
|
|
| 257 |
if hasattr(image_obj, "close"):
|
| 258 |
image_obj.close()
|
| 259 |
|
| 260 |
+
# Write batch to file
|
| 261 |
if batch_document_dicts:
|
| 262 |
batch_file = documents_batches_dir / f"batch_{batch_index:05d}.json"
|
| 263 |
write_json(batch_file, batch_document_dicts)
|
|
|
|
| 272 |
for idx, sample in enumerate(sample_iterator):
|
| 273 |
if settings.max_samples is not None and idx >= settings.max_samples:
|
| 274 |
break
|
| 275 |
+
|
| 276 |
+
# Create one folder for each samples
|
| 277 |
sample_id = f"sample_{idx:05d}"
|
| 278 |
sample_dir = settings.output_dir / sample_id
|
| 279 |
print(f"Sample directory: {str(sample_dir)}")
|
|
|
|
| 353 |
|
| 354 |
dataset = load_dataset("json", data_files=document_batch_files, features=_dataset_features())
|
| 355 |
|
| 356 |
+
# Remove batch files before upload - they're already loaded into the dataset
|
| 357 |
+
shutil.rmtree(documents_batches_dir)
|
| 358 |
+
|
| 359 |
token = os.environ.get("HF_TOKEN", None)
|
| 360 |
|
| 361 |
maybe_upload_dataset(
|