JS6969 commited on
Commit
fe7adce
·
verified ·
1 Parent(s): 0728db3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -223,6 +223,26 @@ def load_journal() -> dict:
223
  # ------------------------------
224
  # 5) Small utilities (thumbs, resize, prefix/suffix, names)
225
  # ------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  def sanitize_basename(s: str) -> str:
227
  s = (s or "").strip() or "forgecaptions"
228
  return re.sub(r"[^A-Za-z0-9._-]+", "_", s)[:120]
@@ -477,7 +497,7 @@ def _rows_to_table(rows: List[dict]) -> list:
477
  return [[r.get("filename",""), r.get("caption","")] for r in (rows or [])]
478
 
479
  def _table_to_rows(table_value: Any, rows: List[dict]) -> List[dict]:
480
- tbl = table_value or []
481
  new = []
482
  for i, r in enumerate(rows or []):
483
  r = dict(r)
@@ -488,7 +508,7 @@ def _table_to_rows(table_value: Any, rows: List[dict]) -> List[dict]:
488
  return new
489
 
490
  def export_csv_from_table(table_value: Any, dataset_name: str) -> str:
491
- data = table_value or []
492
  name = sanitize_basename(dataset_name)
493
  out = f"/tmp/{name}_{int(time.time())}.csv"
494
  with open(out, "w", newline="", encoding="utf-8") as f:
@@ -520,7 +540,7 @@ def export_excel_with_thumbs(table_value: Any, session_rows: List[dict], thumb_p
520
  raise RuntimeError("Excel export requires 'openpyxl' in requirements.txt.") from e
521
 
522
  caption_by_file = {}
523
- for row in (table_value or []):
524
  if not row:
525
  continue
526
  fn = str(row[0]) if len(row) > 0 else ""
@@ -560,7 +580,7 @@ def export_txt_zip(table_value: Any, dataset_name: str) -> str:
560
  """
561
  Create one .txt per caption, zip them.
562
  """
563
- data = table_value or []
564
  # wipe old
565
  for fn in os.listdir(TXT_EXPORT_DIR):
566
  try:
 
223
  # ------------------------------
224
  # 5) Small utilities (thumbs, resize, prefix/suffix, names)
225
  # ------------------------------
226
+ # put near your other small utils
227
+ def _normalize_table_value(table_value):
228
+ """Return a list-of-lists no matter what Gradio gives us."""
229
+ if table_value is None:
230
+ return []
231
+ # handle pandas.DataFrame without importing pandas explicitly
232
+ if hasattr(table_value, "to_dict") and hasattr(table_value, "values"):
233
+ try:
234
+ return table_value.reset_index(drop=True).values.tolist()
235
+ except Exception:
236
+ try:
237
+ return table_value.values.tolist()
238
+ except Exception:
239
+ return []
240
+ # sometimes Gradio hands a dict with "data"
241
+ if isinstance(table_value, dict) and "data" in table_value:
242
+ return table_value["data"] or []
243
+ # already a list (or something list-like)
244
+ return table_value
245
+
246
  def sanitize_basename(s: str) -> str:
247
  s = (s or "").strip() or "forgecaptions"
248
  return re.sub(r"[^A-Za-z0-9._-]+", "_", s)[:120]
 
497
  return [[r.get("filename",""), r.get("caption","")] for r in (rows or [])]
498
 
499
  def _table_to_rows(table_value: Any, rows: List[dict]) -> List[dict]:
500
+ tbl = _normalize_table_value(table_value)
501
  new = []
502
  for i, r in enumerate(rows or []):
503
  r = dict(r)
 
508
  return new
509
 
510
  def export_csv_from_table(table_value: Any, dataset_name: str) -> str:
511
+ data = _normalize_table_value(table_value)
512
  name = sanitize_basename(dataset_name)
513
  out = f"/tmp/{name}_{int(time.time())}.csv"
514
  with open(out, "w", newline="", encoding="utf-8") as f:
 
540
  raise RuntimeError("Excel export requires 'openpyxl' in requirements.txt.") from e
541
 
542
  caption_by_file = {}
543
+ for row in _normalize_table_value(table_value):
544
  if not row:
545
  continue
546
  fn = str(row[0]) if len(row) > 0 else ""
 
580
  """
581
  Create one .txt per caption, zip them.
582
  """
583
+ data = _normalize_table_value(table_value)
584
  # wipe old
585
  for fn in os.listdir(TXT_EXPORT_DIR):
586
  try: