Spaces:
Sleeping
Sleeping
Pointf5ive commited on
Commit ·
e38e791
1
Parent(s): 55f755c
Fix ingest upload path resolution to prevent empty manifest
Browse files- smoke_signal_tab.py +64 -5
smoke_signal_tab.py
CHANGED
|
@@ -1034,6 +1034,60 @@ def _resolve_book_row(df: pd.DataFrame, selector: str) -> Optional[pd.Series]:
|
|
| 1034 |
return None
|
| 1035 |
|
| 1036 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1037 |
def _clean_page_spec(spec: str) -> str:
|
| 1038 |
raw = (spec or "").strip().lower()
|
| 1039 |
if raw in ("", "all", "*", "none", "-"):
|
|
@@ -1120,9 +1174,9 @@ def ingest_pdfs(files, rights_class: str, notes: str, book_code_hint: str = "",
|
|
| 1120 |
auto_profile_ids = []
|
| 1121 |
|
| 1122 |
for file in files:
|
| 1123 |
-
path
|
| 1124 |
-
if
|
| 1125 |
-
log.append(log_line(f"⚠
|
| 1126 |
continue
|
| 1127 |
|
| 1128 |
file_hash = sha256_file(path)
|
|
@@ -1160,7 +1214,12 @@ def ingest_pdfs(files, rights_class: str, notes: str, book_code_hint: str = "",
|
|
| 1160 |
# Copy to source_pdfs
|
| 1161 |
dest = SOURCE_DIR / path.name
|
| 1162 |
import shutil
|
| 1163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1164 |
|
| 1165 |
# Page count
|
| 1166 |
page_count = None
|
|
@@ -1189,7 +1248,7 @@ def ingest_pdfs(files, rights_class: str, notes: str, book_code_hint: str = "",
|
|
| 1189 |
"safe_title": _safe_title_slug(Path(path.name).stem),
|
| 1190 |
}])
|
| 1191 |
df = pd.concat([df, new_row], ignore_index=True)
|
| 1192 |
-
log.append(log_line(f"✓ Registered {book_id} — {path.name} ({page_count or '?'} pages)"))
|
| 1193 |
new_count += 1
|
| 1194 |
auto_profile_ids.append(book_id)
|
| 1195 |
|
|
|
|
| 1034 |
return None
|
| 1035 |
|
| 1036 |
|
| 1037 |
+
def _resolve_uploaded_file_path(file_obj) -> tuple[Optional[Path], str]:
|
| 1038 |
+
"""
|
| 1039 |
+
Robustly resolve an uploaded file path across Gradio runtime object shapes.
|
| 1040 |
+
Returns (path, debug_hint).
|
| 1041 |
+
"""
|
| 1042 |
+
candidates: list[str] = []
|
| 1043 |
+
if file_obj is None:
|
| 1044 |
+
return None, "upload item is None"
|
| 1045 |
+
|
| 1046 |
+
# Candidate 1: direct string form (often full temp path in Gradio)
|
| 1047 |
+
try:
|
| 1048 |
+
s = str(file_obj).strip()
|
| 1049 |
+
if s:
|
| 1050 |
+
candidates.append(s)
|
| 1051 |
+
except Exception:
|
| 1052 |
+
pass
|
| 1053 |
+
|
| 1054 |
+
# Candidate 2: .name attribute (file-like wrappers)
|
| 1055 |
+
try:
|
| 1056 |
+
n = getattr(file_obj, "name", "")
|
| 1057 |
+
n = str(n).strip()
|
| 1058 |
+
if n:
|
| 1059 |
+
candidates.append(n)
|
| 1060 |
+
except Exception:
|
| 1061 |
+
pass
|
| 1062 |
+
|
| 1063 |
+
# Candidate 3: explicit path attr used by some wrappers
|
| 1064 |
+
try:
|
| 1065 |
+
p = getattr(file_obj, "path", "")
|
| 1066 |
+
p = str(p).strip()
|
| 1067 |
+
if p:
|
| 1068 |
+
candidates.append(p)
|
| 1069 |
+
except Exception:
|
| 1070 |
+
pass
|
| 1071 |
+
|
| 1072 |
+
# Deduplicate in order
|
| 1073 |
+
seen = set()
|
| 1074 |
+
uniq = []
|
| 1075 |
+
for c in candidates:
|
| 1076 |
+
if c not in seen:
|
| 1077 |
+
uniq.append(c)
|
| 1078 |
+
seen.add(c)
|
| 1079 |
+
|
| 1080 |
+
for c in uniq:
|
| 1081 |
+
try:
|
| 1082 |
+
path = Path(c)
|
| 1083 |
+
if path.exists():
|
| 1084 |
+
return path, f"resolved from '{c}'"
|
| 1085 |
+
except Exception:
|
| 1086 |
+
continue
|
| 1087 |
+
|
| 1088 |
+
return None, f"no existing path in candidates={uniq!r}"
|
| 1089 |
+
|
| 1090 |
+
|
| 1091 |
def _clean_page_spec(spec: str) -> str:
|
| 1092 |
raw = (spec or "").strip().lower()
|
| 1093 |
if raw in ("", "all", "*", "none", "-"):
|
|
|
|
| 1174 |
auto_profile_ids = []
|
| 1175 |
|
| 1176 |
for file in files:
|
| 1177 |
+
path, resolve_hint = _resolve_uploaded_file_path(file)
|
| 1178 |
+
if path is None:
|
| 1179 |
+
log.append(log_line(f"⚠ Upload path unresolved: {resolve_hint}"))
|
| 1180 |
continue
|
| 1181 |
|
| 1182 |
file_hash = sha256_file(path)
|
|
|
|
| 1214 |
# Copy to source_pdfs
|
| 1215 |
dest = SOURCE_DIR / path.name
|
| 1216 |
import shutil
|
| 1217 |
+
try:
|
| 1218 |
+
if path.resolve() != dest.resolve():
|
| 1219 |
+
shutil.copy2(path, dest)
|
| 1220 |
+
except Exception as e:
|
| 1221 |
+
log.append(log_line(f"✗ Failed to copy {path.name}: {e}"))
|
| 1222 |
+
continue
|
| 1223 |
|
| 1224 |
# Page count
|
| 1225 |
page_count = None
|
|
|
|
| 1248 |
"safe_title": _safe_title_slug(Path(path.name).stem),
|
| 1249 |
}])
|
| 1250 |
df = pd.concat([df, new_row], ignore_index=True)
|
| 1251 |
+
log.append(log_line(f"✓ Registered {book_id} — {path.name} ({page_count or '?'} pages) [{resolve_hint}]"))
|
| 1252 |
new_count += 1
|
| 1253 |
auto_profile_ids.append(book_id)
|
| 1254 |
|