Spaces:
Running
Running
Siyuan Hu
commited on
Commit
Β·
d3f4e5f
1
Parent(s):
81ef9fb
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
|
@@ -2938,6 +2938,114 @@ def debug_compile_output_zip():
|
|
| 2938 |
_write_logs(LOG_PATH, logs)
|
| 2939 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2940 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2941 |
def debug_compile_uploaded_zip(zip_file):
|
| 2942 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 2943 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
|
|
|
| 2938 |
_write_logs(LOG_PATH, logs)
|
| 2939 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2940 |
|
| 2941 |
+
def _find_last_pipeline_zip():
|
| 2942 |
+
try:
|
| 2943 |
+
candidates = []
|
| 2944 |
+
for d in RUNS_DIR.iterdir():
|
| 2945 |
+
try:
|
| 2946 |
+
if d.is_dir():
|
| 2947 |
+
z = d / "output.zip"
|
| 2948 |
+
if z.exists():
|
| 2949 |
+
candidates.append((z.stat().st_mtime, z))
|
| 2950 |
+
except Exception:
|
| 2951 |
+
pass
|
| 2952 |
+
if not candidates:
|
| 2953 |
+
return None
|
| 2954 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 2955 |
+
return candidates[0][1]
|
| 2956 |
+
except Exception:
|
| 2957 |
+
return None
|
| 2958 |
+
|
| 2959 |
+
def debug_compile_last_pipeline_zip():
|
| 2960 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 2961 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 2962 |
+
last_zip = _find_last_pipeline_zip()
|
| 2963 |
+
if not last_zip:
|
| 2964 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 2965 |
+
|
| 2966 |
+
# Prepare workspace
|
| 2967 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 2968 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 2969 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 2970 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 2971 |
+
logs.append(f"Using: {last_zip}")
|
| 2972 |
+
|
| 2973 |
+
# Extract zip
|
| 2974 |
+
try:
|
| 2975 |
+
import zipfile as _zf
|
| 2976 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 2977 |
+
zf.extractall(work_zip_dir)
|
| 2978 |
+
except Exception as e:
|
| 2979 |
+
logs.append(f"β unzip failed: {e}")
|
| 2980 |
+
_write_logs(LOG_PATH, logs)
|
| 2981 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 2982 |
+
|
| 2983 |
+
# Locate tex
|
| 2984 |
+
tex_path = None
|
| 2985 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 2986 |
+
cand = list(work_zip_dir.rglob(name))
|
| 2987 |
+
if cand:
|
| 2988 |
+
tex_path = cand[0]
|
| 2989 |
+
break
|
| 2990 |
+
if tex_path is None:
|
| 2991 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 2992 |
+
if cand:
|
| 2993 |
+
tex_path = cand[0]
|
| 2994 |
+
if tex_path is None:
|
| 2995 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 2996 |
+
_write_logs(LOG_PATH, logs)
|
| 2997 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 2998 |
+
|
| 2999 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 3000 |
+
try:
|
| 3001 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 3002 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 3003 |
+
if src_fonts.exists():
|
| 3004 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 3005 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 3006 |
+
out_dir = dst_fonts / rel
|
| 3007 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 3008 |
+
for fn in files:
|
| 3009 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 3010 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 3011 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 3012 |
+
# Copy repository theme .sty next to tex and at root
|
| 3013 |
+
try:
|
| 3014 |
+
tmpl_dir = ROOT / "template"
|
| 3015 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 3016 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 3017 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 3018 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 3019 |
+
except Exception as e:
|
| 3020 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 3021 |
+
except Exception as e:
|
| 3022 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 3023 |
+
|
| 3024 |
+
# Compile to PDF
|
| 3025 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 3026 |
+
if not pdf_path or not pdf_path.exists():
|
| 3027 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 3028 |
+
_write_logs(LOG_PATH, logs)
|
| 3029 |
+
return (
|
| 3030 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 3031 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 3032 |
+
+ "\n".join(logs)
|
| 3033 |
+
+ "</pre>"
|
| 3034 |
+
)
|
| 3035 |
+
try:
|
| 3036 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 3037 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 3038 |
+
html = (
|
| 3039 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 3040 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 3041 |
+
)
|
| 3042 |
+
_write_logs(LOG_PATH, logs)
|
| 3043 |
+
return html
|
| 3044 |
+
except Exception as e:
|
| 3045 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 3046 |
+
_write_logs(LOG_PATH, logs)
|
| 3047 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 3048 |
+
|
| 3049 |
def debug_compile_uploaded_zip(zip_file):
|
| 3050 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 3051 |
logs = [f"π Debug(upload) at {_now_str()}"]
|