Pointf5ive commited on
Commit
b9de0c7
Β·
1 Parent(s): e7b8c1d

Fix save-page scope dtype crash and prefill OCR selectors from ingest scope

Browse files
Files changed (1) hide show
  1. smoke_signal_tab.py +53 -7
smoke_signal_tab.py CHANGED
@@ -76,6 +76,18 @@ MANIFEST_COLUMNS = [
76
  "story_pages_exclude",
77
  "safe_title",
78
  ]
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
 
81
  def _load_banner_image_css() -> str:
@@ -747,18 +759,29 @@ def sha256_file(path: Path) -> str:
747
  def load_manifest_df() -> pd.DataFrame:
748
  if not MANIFEST_CSV.exists():
749
  return pd.DataFrame(columns=MANIFEST_COLUMNS)
750
- df = pd.read_csv(MANIFEST_CSV)
751
  for col in MANIFEST_COLUMNS:
752
  if col not in df.columns:
753
  df[col] = ""
 
 
 
 
 
754
  return df[MANIFEST_COLUMNS]
755
 
756
 
757
  def save_manifest_df(df: pd.DataFrame) -> None:
 
758
  for col in MANIFEST_COLUMNS:
759
- if col not in df.columns:
760
- df[col] = ""
761
- df[MANIFEST_COLUMNS].to_csv(MANIFEST_CSV, index=False)
 
 
 
 
 
762
 
763
 
764
  def next_book_id(df: pd.DataFrame) -> str:
@@ -994,12 +1017,31 @@ def _normalize_saved_spec(value) -> str:
994
  return raw
995
 
996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
997
  def save_book_scope(book_id: str, include_spec: str, exclude_spec: str, safe_title: str) -> tuple:
998
  df = load_manifest_df()
999
  bid = (book_id or "").strip()
1000
- if df.empty or not bid:
1001
- return _ingest_status_html("idle"), df, "Book ID is required."
1002
- if bid not in df["book_id"].values:
 
 
 
 
 
1003
  return _ingest_status_html("idle"), df, f"Book ID {bid} not found."
1004
 
1005
  try:
@@ -2677,6 +2719,10 @@ def smoke_signal_tab():
2677
  outputs=[ocr_status, ocr_log],
2678
  show_progress="hidden",
2679
  )
 
 
 
 
2680
 
2681
  # ── STEP 4: REVIEW ────────────────────────────────────────────────
2682
  with gr.TabItem("β‘£ Review", id="ss-review") as review_tab:
 
76
  "story_pages_exclude",
77
  "safe_title",
78
  ]
79
+ MANIFEST_TEXT_COLUMNS = {
80
+ "book_id",
81
+ "filename",
82
+ "sha256",
83
+ "rights_class",
84
+ "status",
85
+ "acquisition_date",
86
+ "notes",
87
+ "story_pages_include",
88
+ "story_pages_exclude",
89
+ "safe_title",
90
+ }
91
 
92
 
93
  def _load_banner_image_css() -> str:
 
759
  def load_manifest_df() -> pd.DataFrame:
760
  if not MANIFEST_CSV.exists():
761
  return pd.DataFrame(columns=MANIFEST_COLUMNS)
762
+ df = pd.read_csv(MANIFEST_CSV, dtype=str, keep_default_na=False)
763
  for col in MANIFEST_COLUMNS:
764
  if col not in df.columns:
765
  df[col] = ""
766
+ for col in MANIFEST_TEXT_COLUMNS:
767
+ if col in df.columns:
768
+ df[col] = df[col].fillna("").astype(str)
769
+ if "page_count" in df.columns:
770
+ df["page_count"] = df["page_count"].fillna("").astype(str)
771
  return df[MANIFEST_COLUMNS]
772
 
773
 
774
  def save_manifest_df(df: pd.DataFrame) -> None:
775
+ out = df.copy()
776
  for col in MANIFEST_COLUMNS:
777
+ if col not in out.columns:
778
+ out[col] = ""
779
+ for col in MANIFEST_TEXT_COLUMNS:
780
+ if col in out.columns:
781
+ out[col] = out[col].fillna("").astype(str)
782
+ if "page_count" in out.columns:
783
+ out["page_count"] = out["page_count"].fillna("").astype(str)
784
+ out[MANIFEST_COLUMNS].to_csv(MANIFEST_CSV, index=False)
785
 
786
 
787
  def next_book_id(df: pd.DataFrame) -> str:
 
1017
  return raw
1018
 
1019
 
1020
+ def _default_ocr_scope_values() -> tuple[str, str]:
1021
+ """
1022
+ Prefill OCR page selectors from saved ingest scope.
1023
+ If multiple books exist, use the latest manifest row as default.
1024
+ """
1025
+ df = load_manifest_df()
1026
+ if df.empty:
1027
+ return "", ""
1028
+ row = df.iloc[-1]
1029
+ include_val = _normalize_saved_spec(row.get("story_pages_include", ""))
1030
+ exclude_val = _normalize_saved_spec(row.get("story_pages_exclude", ""))
1031
+ return include_val, exclude_val
1032
+
1033
+
1034
  def save_book_scope(book_id: str, include_spec: str, exclude_spec: str, safe_title: str) -> tuple:
1035
  df = load_manifest_df()
1036
  bid = (book_id or "").strip()
1037
+ if df.empty:
1038
+ return _ingest_status_html("idle"), df, "No books in manifest yet."
1039
+ if not bid:
1040
+ if len(df) == 1:
1041
+ bid = str(df.iloc[0]["book_id"])
1042
+ else:
1043
+ bid = str(df.iloc[-1]["book_id"])
1044
+ if bid not in df["book_id"].astype(str).values:
1045
  return _ingest_status_html("idle"), df, f"Book ID {bid} not found."
1046
 
1047
  try:
 
2719
  outputs=[ocr_status, ocr_log],
2720
  show_progress="hidden",
2721
  )
2722
+ ocr_tab.select(
2723
+ _default_ocr_scope_values,
2724
+ outputs=[ocr_page_selection, ocr_page_exclusion],
2725
+ )
2726
 
2727
  # ── STEP 4: REVIEW ────────────────────────────────────────────────
2728
  with gr.TabItem("β‘£ Review", id="ss-review") as review_tab: