Spaces:
Sleeping
Sleeping
Siyuan Hu
commited on
Commit
Β·
23f541f
1
Parent(s):
2bc4d1c
feat(debug): add 'Test last pipeline zip' to compile most recent runs/*/output.zip with local fonts + template precedence; widen TEXINPUTS
Browse files
app.py
CHANGED
|
@@ -1103,6 +1103,114 @@ def debug_compile_output_zip():
|
|
| 1103 |
_write_logs(LOG_PATH, logs)
|
| 1104 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1106 |
def debug_compile_uploaded_zip(zip_file):
|
| 1107 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 1108 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
|
|
|
| 1103 |
_write_logs(LOG_PATH, logs)
|
| 1104 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1105 |
|
| 1106 |
+
def _find_last_pipeline_zip():
|
| 1107 |
+
try:
|
| 1108 |
+
candidates = []
|
| 1109 |
+
for d in RUNS_DIR.iterdir():
|
| 1110 |
+
try:
|
| 1111 |
+
if d.is_dir():
|
| 1112 |
+
z = d / "output.zip"
|
| 1113 |
+
if z.exists():
|
| 1114 |
+
candidates.append((z.stat().st_mtime, z))
|
| 1115 |
+
except Exception:
|
| 1116 |
+
pass
|
| 1117 |
+
if not candidates:
|
| 1118 |
+
return None
|
| 1119 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 1120 |
+
return candidates[0][1]
|
| 1121 |
+
except Exception:
|
| 1122 |
+
return None
|
| 1123 |
+
|
| 1124 |
+
def debug_compile_last_pipeline_zip():
|
| 1125 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 1126 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 1127 |
+
last_zip = _find_last_pipeline_zip()
|
| 1128 |
+
if not last_zip:
|
| 1129 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 1130 |
+
|
| 1131 |
+
# Prepare workspace
|
| 1132 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 1133 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 1134 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 1135 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 1136 |
+
logs.append(f"Using: {last_zip}")
|
| 1137 |
+
|
| 1138 |
+
# Extract zip
|
| 1139 |
+
try:
|
| 1140 |
+
import zipfile as _zf
|
| 1141 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 1142 |
+
zf.extractall(work_zip_dir)
|
| 1143 |
+
except Exception as e:
|
| 1144 |
+
logs.append(f"β unzip failed: {e}")
|
| 1145 |
+
_write_logs(LOG_PATH, logs)
|
| 1146 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 1147 |
+
|
| 1148 |
+
# Locate tex
|
| 1149 |
+
tex_path = None
|
| 1150 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 1151 |
+
cand = list(work_zip_dir.rglob(name))
|
| 1152 |
+
if cand:
|
| 1153 |
+
tex_path = cand[0]
|
| 1154 |
+
break
|
| 1155 |
+
if tex_path is None:
|
| 1156 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 1157 |
+
if cand:
|
| 1158 |
+
tex_path = cand[0]
|
| 1159 |
+
if tex_path is None:
|
| 1160 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 1161 |
+
_write_logs(LOG_PATH, logs)
|
| 1162 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 1163 |
+
|
| 1164 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 1165 |
+
try:
|
| 1166 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 1167 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 1168 |
+
if src_fonts.exists():
|
| 1169 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 1170 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 1171 |
+
out_dir = dst_fonts / rel
|
| 1172 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 1173 |
+
for fn in files:
|
| 1174 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 1175 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 1176 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 1177 |
+
# Copy repository theme .sty next to tex and at root
|
| 1178 |
+
try:
|
| 1179 |
+
tmpl_dir = ROOT / "template"
|
| 1180 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 1181 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 1182 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 1183 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 1184 |
+
except Exception as e:
|
| 1185 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 1186 |
+
except Exception as e:
|
| 1187 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 1188 |
+
|
| 1189 |
+
# Compile to PDF
|
| 1190 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 1191 |
+
if not pdf_path or not pdf_path.exists():
|
| 1192 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 1193 |
+
_write_logs(LOG_PATH, logs)
|
| 1194 |
+
return (
|
| 1195 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 1196 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 1197 |
+
+ "\n".join(logs)
|
| 1198 |
+
+ "</pre>"
|
| 1199 |
+
)
|
| 1200 |
+
try:
|
| 1201 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 1202 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 1203 |
+
html = (
|
| 1204 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 1205 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 1206 |
+
)
|
| 1207 |
+
_write_logs(LOG_PATH, logs)
|
| 1208 |
+
return html
|
| 1209 |
+
except Exception as e:
|
| 1210 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 1211 |
+
_write_logs(LOG_PATH, logs)
|
| 1212 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1213 |
+
|
| 1214 |
def debug_compile_uploaded_zip(zip_file):
|
| 1215 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 1216 |
logs = [f"π Debug(upload) at {_now_str()}"]
|