Florent Gbelidji commited on
Commit
5cacb35
·
verified ·
1 Parent(s): da6c772

Sync DeepSeek OCR HF job code

Browse files
Files changed (1) hide show
  1. ds_batch_ocr/stages.py +90 -4
ds_batch_ocr/stages.py CHANGED
@@ -23,7 +23,7 @@ from .document import (
23
  write_jsonl,
24
  write_text,
25
  )
26
- from .hf_io import maybe_upload_dataset, resolve_stage_dir
27
 
28
  deps = bootstrap()
29
  load_dataset = deps["load_dataset"]
@@ -542,7 +542,7 @@ def run_stage_assemble(settings: AssembleSettings) -> None:
542
  "image_path": (
543
  Path(sample_id) / "figures" / target_fig_path.name
544
  ).as_posix(),
545
- "description": description_entry.get("description"),
546
  }
547
  )
548
 
@@ -560,7 +560,8 @@ def run_stage_assemble(settings: AssembleSettings) -> None:
560
  {
561
  "sample_id": sample_id,
562
  "dataset_index": document.get("dataset_index"),
563
- "document_markdown": final_doc_rel_path,
 
564
  "figures": copied_figures,
565
  }
566
  )
@@ -575,13 +576,23 @@ def run_stage_assemble(settings: AssembleSettings) -> None:
575
  write_json(settings.output_dir / "manifest.json", aggregate)
576
  write_jsonl(settings.output_dir / "dataset.jsonl", dataset_records)
577
 
 
 
 
578
  maybe_upload_dataset(
579
  output_dir=settings.output_dir,
580
  repo_id=settings.dataset_repo_id,
581
  repo_type=settings.dataset_repo_type,
582
  path_in_repo=settings.dataset_path_in_repo,
583
- commit_message=settings.dataset_commit_message,
 
 
 
 
 
 
584
  revision=settings.dataset_branch,
 
585
  )
586
  LOGGER.info(
587
  "Assemble stage complete | documents=%s | failures=%s",
@@ -635,6 +646,81 @@ def __now_iso() -> str:
635
 
636
  return datetime.utcnow().isoformat() + "Z"
637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
638
  __all__ = [
639
  "run_stage_extract",
640
  "run_stage_describe",
 
23
  write_jsonl,
24
  write_text,
25
  )
26
+ from .hf_io import maybe_upload_dataset, resolve_stage_dir, env_or_none
27
 
28
  deps = bootstrap()
29
  load_dataset = deps["load_dataset"]
 
542
  "image_path": (
543
  Path(sample_id) / "figures" / target_fig_path.name
544
  ).as_posix(),
545
+ "description": description_entry.get("description") or "",
546
  }
547
  )
548
 
 
560
  {
561
  "sample_id": sample_id,
562
  "dataset_index": document.get("dataset_index"),
563
+ "document_markdown_path": final_doc_rel_path,
564
+ "document_markdown_text": enriched_markdown,
565
  "figures": copied_figures,
566
  }
567
  )
 
576
  write_json(settings.output_dir / "manifest.json", aggregate)
577
  write_jsonl(settings.output_dir / "dataset.jsonl", dataset_records)
578
 
579
+ assemble_commit = settings.dataset_commit_message or (
580
+ f"Upload assemble stage outputs {__now_iso()}"
581
+ )
582
  maybe_upload_dataset(
583
  output_dir=settings.output_dir,
584
  repo_id=settings.dataset_repo_id,
585
  repo_type=settings.dataset_repo_type,
586
  path_in_repo=settings.dataset_path_in_repo,
587
+ commit_message=assemble_commit,
588
+ revision=settings.dataset_branch,
589
+ )
590
+ publish_dataset_viewer_assets(
591
+ dataset_records=dataset_records,
592
+ repo_id=settings.dataset_repo_id,
593
+ repo_type=settings.dataset_repo_type,
594
  revision=settings.dataset_branch,
595
+ commit_message=f"{assemble_commit} [dataset viewer]",
596
  )
597
  LOGGER.info(
598
  "Assemble stage complete | documents=%s | failures=%s",
 
646
 
647
  return datetime.utcnow().isoformat() + "Z"
648
 
649
+
650
+ def publish_dataset_viewer_assets(
651
+ *,
652
+ dataset_records: List[Dict[str, Any]],
653
+ repo_id: Optional[str],
654
+ repo_type: str,
655
+ revision: Optional[str],
656
+ commit_message: str,
657
+ ) -> None:
658
+ if not repo_id or repo_type.lower() != "dataset":
659
+ return
660
+ if not dataset_records:
661
+ LOGGER.debug("No dataset records to publish for %s", repo_id)
662
+ return
663
+
664
+ try:
665
+ from datasets import Dataset, Features, Sequence, Value # type: ignore
666
+ except Exception as exc: # pragma: no cover - defensive logging
667
+ LOGGER.warning("Datasets library unavailable; skipping viewer dataset publish: %s", exc)
668
+ return
669
+
670
+ normalized: List[Dict[str, Any]] = []
671
+ for record in dataset_records:
672
+ figures = record.get("figures", []) or []
673
+ normalized.append(
674
+ {
675
+ "sample_id": str(record.get("sample_id", "")),
676
+ "dataset_index": int(record.get("dataset_index") or 0),
677
+ "document_markdown_path": str(record.get("document_markdown_path", "")),
678
+ "document_markdown_text": record.get("document_markdown_text", ""),
679
+ "figures": [
680
+ {
681
+ "figure_id": str(fig.get("figure_id", "")),
682
+ "image_path": str(fig.get("image_path", "")),
683
+ "description": fig.get("description", ""),
684
+ }
685
+ for fig in figures
686
+ ],
687
+ }
688
+ )
689
+
690
+ features = Features(
691
+ {
692
+ "sample_id": Value("string"),
693
+ "dataset_index": Value("int64"),
694
+ "document_markdown_path": Value("string"),
695
+ "document_markdown_text": Value("string"),
696
+ "figures": Sequence(
697
+ {
698
+ "figure_id": Value("string"),
699
+ "image_path": Value("string"),
700
+ "description": Value("string"),
701
+ }
702
+ ),
703
+ }
704
+ )
705
+
706
+ dataset = Dataset.from_list(normalized, features=features)
707
+ token = env_or_none("HF_TOKEN")
708
+ try:
709
+ dataset.push_to_hub(
710
+ repo_id=repo_id,
711
+ token=token,
712
+ split="train",
713
+ revision=revision,
714
+ commit_message=commit_message,
715
+ )
716
+ LOGGER.info(
717
+ "Published assembled dataset viewer table | repo=%s | records=%s",
718
+ repo_id,
719
+ len(normalized),
720
+ )
721
+ except Exception as exc: # pragma: no cover - defensive logging
722
+ LOGGER.exception("Failed to publish assembled dataset viewer assets: %s", exc)
723
+
724
  __all__ = [
725
  "run_stage_extract",
726
  "run_stage_describe",