Spaces:
Sleeping
Sleeping
Siyuan Hu
commited on
Commit
Β·
87a6296
1
Parent(s):
adbef73
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
|
@@ -2622,6 +2622,114 @@ def debug_compile_output_zip():
|
|
| 2622 |
_write_logs(LOG_PATH, logs)
|
| 2623 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2624 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2625 |
def debug_compile_uploaded_zip(zip_file):
|
| 2626 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 2627 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
|
|
|
| 2622 |
_write_logs(LOG_PATH, logs)
|
| 2623 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2624 |
|
| 2625 |
+
def _find_last_pipeline_zip():
|
| 2626 |
+
try:
|
| 2627 |
+
candidates = []
|
| 2628 |
+
for d in RUNS_DIR.iterdir():
|
| 2629 |
+
try:
|
| 2630 |
+
if d.is_dir():
|
| 2631 |
+
z = d / "output.zip"
|
| 2632 |
+
if z.exists():
|
| 2633 |
+
candidates.append((z.stat().st_mtime, z))
|
| 2634 |
+
except Exception:
|
| 2635 |
+
pass
|
| 2636 |
+
if not candidates:
|
| 2637 |
+
return None
|
| 2638 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 2639 |
+
return candidates[0][1]
|
| 2640 |
+
except Exception:
|
| 2641 |
+
return None
|
| 2642 |
+
|
| 2643 |
+
def debug_compile_last_pipeline_zip():
|
| 2644 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 2645 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 2646 |
+
last_zip = _find_last_pipeline_zip()
|
| 2647 |
+
if not last_zip:
|
| 2648 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 2649 |
+
|
| 2650 |
+
# Prepare workspace
|
| 2651 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 2652 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 2653 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 2654 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 2655 |
+
logs.append(f"Using: {last_zip}")
|
| 2656 |
+
|
| 2657 |
+
# Extract zip
|
| 2658 |
+
try:
|
| 2659 |
+
import zipfile as _zf
|
| 2660 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 2661 |
+
zf.extractall(work_zip_dir)
|
| 2662 |
+
except Exception as e:
|
| 2663 |
+
logs.append(f"β unzip failed: {e}")
|
| 2664 |
+
_write_logs(LOG_PATH, logs)
|
| 2665 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 2666 |
+
|
| 2667 |
+
# Locate tex
|
| 2668 |
+
tex_path = None
|
| 2669 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 2670 |
+
cand = list(work_zip_dir.rglob(name))
|
| 2671 |
+
if cand:
|
| 2672 |
+
tex_path = cand[0]
|
| 2673 |
+
break
|
| 2674 |
+
if tex_path is None:
|
| 2675 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 2676 |
+
if cand:
|
| 2677 |
+
tex_path = cand[0]
|
| 2678 |
+
if tex_path is None:
|
| 2679 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 2680 |
+
_write_logs(LOG_PATH, logs)
|
| 2681 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 2682 |
+
|
| 2683 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 2684 |
+
try:
|
| 2685 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 2686 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 2687 |
+
if src_fonts.exists():
|
| 2688 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 2689 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 2690 |
+
out_dir = dst_fonts / rel
|
| 2691 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 2692 |
+
for fn in files:
|
| 2693 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 2694 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 2695 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 2696 |
+
# Copy repository theme .sty next to tex and at root
|
| 2697 |
+
try:
|
| 2698 |
+
tmpl_dir = ROOT / "template"
|
| 2699 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 2700 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 2701 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 2702 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 2703 |
+
except Exception as e:
|
| 2704 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 2705 |
+
except Exception as e:
|
| 2706 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 2707 |
+
|
| 2708 |
+
# Compile to PDF
|
| 2709 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 2710 |
+
if not pdf_path or not pdf_path.exists():
|
| 2711 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 2712 |
+
_write_logs(LOG_PATH, logs)
|
| 2713 |
+
return (
|
| 2714 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 2715 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 2716 |
+
+ "\n".join(logs)
|
| 2717 |
+
+ "</pre>"
|
| 2718 |
+
)
|
| 2719 |
+
try:
|
| 2720 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 2721 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 2722 |
+
html = (
|
| 2723 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 2724 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 2725 |
+
)
|
| 2726 |
+
_write_logs(LOG_PATH, logs)
|
| 2727 |
+
return html
|
| 2728 |
+
except Exception as e:
|
| 2729 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 2730 |
+
_write_logs(LOG_PATH, logs)
|
| 2731 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2732 |
+
|
| 2733 |
def debug_compile_uploaded_zip(zip_file):
|
| 2734 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 2735 |
logs = [f"π Debug(upload) at {_now_str()}"]
|