Spaces:
Sleeping
Sleeping
Siyuan Hu
commited on
Commit
Β·
5cd4b13
1
Parent(s):
0925a06
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
|
@@ -3223,6 +3223,114 @@ def debug_compile_output_zip():
|
|
| 3223 |
_write_logs(LOG_PATH, logs)
|
| 3224 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 3225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3226 |
def debug_compile_uploaded_zip(zip_file):
|
| 3227 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 3228 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
|
|
|
| 3223 |
_write_logs(LOG_PATH, logs)
|
| 3224 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 3225 |
|
| 3226 |
+
def _find_last_pipeline_zip():
|
| 3227 |
+
try:
|
| 3228 |
+
candidates = []
|
| 3229 |
+
for d in RUNS_DIR.iterdir():
|
| 3230 |
+
try:
|
| 3231 |
+
if d.is_dir():
|
| 3232 |
+
z = d / "output.zip"
|
| 3233 |
+
if z.exists():
|
| 3234 |
+
candidates.append((z.stat().st_mtime, z))
|
| 3235 |
+
except Exception:
|
| 3236 |
+
pass
|
| 3237 |
+
if not candidates:
|
| 3238 |
+
return None
|
| 3239 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 3240 |
+
return candidates[0][1]
|
| 3241 |
+
except Exception:
|
| 3242 |
+
return None
|
| 3243 |
+
|
| 3244 |
+
def debug_compile_last_pipeline_zip():
|
| 3245 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 3246 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 3247 |
+
last_zip = _find_last_pipeline_zip()
|
| 3248 |
+
if not last_zip:
|
| 3249 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 3250 |
+
|
| 3251 |
+
# Prepare workspace
|
| 3252 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 3253 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 3254 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 3255 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 3256 |
+
logs.append(f"Using: {last_zip}")
|
| 3257 |
+
|
| 3258 |
+
# Extract zip
|
| 3259 |
+
try:
|
| 3260 |
+
import zipfile as _zf
|
| 3261 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 3262 |
+
zf.extractall(work_zip_dir)
|
| 3263 |
+
except Exception as e:
|
| 3264 |
+
logs.append(f"β unzip failed: {e}")
|
| 3265 |
+
_write_logs(LOG_PATH, logs)
|
| 3266 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 3267 |
+
|
| 3268 |
+
# Locate tex
|
| 3269 |
+
tex_path = None
|
| 3270 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 3271 |
+
cand = list(work_zip_dir.rglob(name))
|
| 3272 |
+
if cand:
|
| 3273 |
+
tex_path = cand[0]
|
| 3274 |
+
break
|
| 3275 |
+
if tex_path is None:
|
| 3276 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 3277 |
+
if cand:
|
| 3278 |
+
tex_path = cand[0]
|
| 3279 |
+
if tex_path is None:
|
| 3280 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 3281 |
+
_write_logs(LOG_PATH, logs)
|
| 3282 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 3283 |
+
|
| 3284 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 3285 |
+
try:
|
| 3286 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 3287 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 3288 |
+
if src_fonts.exists():
|
| 3289 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 3290 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 3291 |
+
out_dir = dst_fonts / rel
|
| 3292 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 3293 |
+
for fn in files:
|
| 3294 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 3295 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 3296 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 3297 |
+
# Copy repository theme .sty next to tex and at root
|
| 3298 |
+
try:
|
| 3299 |
+
tmpl_dir = ROOT / "template"
|
| 3300 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 3301 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 3302 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 3303 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 3304 |
+
except Exception as e:
|
| 3305 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 3306 |
+
except Exception as e:
|
| 3307 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 3308 |
+
|
| 3309 |
+
# Compile to PDF
|
| 3310 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 3311 |
+
if not pdf_path or not pdf_path.exists():
|
| 3312 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 3313 |
+
_write_logs(LOG_PATH, logs)
|
| 3314 |
+
return (
|
| 3315 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 3316 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 3317 |
+
+ "\n".join(logs)
|
| 3318 |
+
+ "</pre>"
|
| 3319 |
+
)
|
| 3320 |
+
try:
|
| 3321 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 3322 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 3323 |
+
html = (
|
| 3324 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 3325 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 3326 |
+
)
|
| 3327 |
+
_write_logs(LOG_PATH, logs)
|
| 3328 |
+
return html
|
| 3329 |
+
except Exception as e:
|
| 3330 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 3331 |
+
_write_logs(LOG_PATH, logs)
|
| 3332 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 3333 |
+
|
| 3334 |
def debug_compile_uploaded_zip(zip_file):
|
| 3335 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 3336 |
logs = [f"π Debug(upload) at {_now_str()}"]
|