Spaces:
Running
Running
Siyuan Hu
commited on
Commit
Β·
7dcb6c1
1
Parent(s):
8aecc6b
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
|
@@ -955,6 +955,114 @@ def debug_compile_last_pipeline_zip():
|
|
| 955 |
_write_logs(LOG_PATH, logs)
|
| 956 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 957 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 958 |
def debug_compile_uploaded_zip(zip_file):
|
| 959 |
"""Compile an uploaded poster zip (user-provided); return preview HTML + PDF path."""
|
| 960 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
|
|
|
| 955 |
_write_logs(LOG_PATH, logs)
|
| 956 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 957 |
|
| 958 |
+
def _find_last_pipeline_zip():
|
| 959 |
+
try:
|
| 960 |
+
candidates = []
|
| 961 |
+
for d in RUNS_DIR.iterdir():
|
| 962 |
+
try:
|
| 963 |
+
if d.is_dir():
|
| 964 |
+
z = d / "output.zip"
|
| 965 |
+
if z.exists():
|
| 966 |
+
candidates.append((z.stat().st_mtime, z))
|
| 967 |
+
except Exception:
|
| 968 |
+
pass
|
| 969 |
+
if not candidates:
|
| 970 |
+
return None
|
| 971 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 972 |
+
return candidates[0][1]
|
| 973 |
+
except Exception:
|
| 974 |
+
return None
|
| 975 |
+
|
| 976 |
+
def debug_compile_last_pipeline_zip():
|
| 977 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 978 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 979 |
+
last_zip = _find_last_pipeline_zip()
|
| 980 |
+
if not last_zip:
|
| 981 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 982 |
+
|
| 983 |
+
# Prepare workspace
|
| 984 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 985 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 986 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 987 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 988 |
+
logs.append(f"Using: {last_zip}")
|
| 989 |
+
|
| 990 |
+
# Extract zip
|
| 991 |
+
try:
|
| 992 |
+
import zipfile as _zf
|
| 993 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 994 |
+
zf.extractall(work_zip_dir)
|
| 995 |
+
except Exception as e:
|
| 996 |
+
logs.append(f"β unzip failed: {e}")
|
| 997 |
+
_write_logs(LOG_PATH, logs)
|
| 998 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 999 |
+
|
| 1000 |
+
# Locate tex
|
| 1001 |
+
tex_path = None
|
| 1002 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 1003 |
+
cand = list(work_zip_dir.rglob(name))
|
| 1004 |
+
if cand:
|
| 1005 |
+
tex_path = cand[0]
|
| 1006 |
+
break
|
| 1007 |
+
if tex_path is None:
|
| 1008 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 1009 |
+
if cand:
|
| 1010 |
+
tex_path = cand[0]
|
| 1011 |
+
if tex_path is None:
|
| 1012 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 1013 |
+
_write_logs(LOG_PATH, logs)
|
| 1014 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 1015 |
+
|
| 1016 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 1017 |
+
try:
|
| 1018 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 1019 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 1020 |
+
if src_fonts.exists():
|
| 1021 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 1022 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 1023 |
+
out_dir = dst_fonts / rel
|
| 1024 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 1025 |
+
for fn in files:
|
| 1026 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 1027 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 1028 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 1029 |
+
# Copy repository theme .sty next to tex and at root
|
| 1030 |
+
try:
|
| 1031 |
+
tmpl_dir = ROOT / "template"
|
| 1032 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 1033 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 1034 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 1035 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 1036 |
+
except Exception as e:
|
| 1037 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 1038 |
+
except Exception as e:
|
| 1039 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 1040 |
+
|
| 1041 |
+
# Compile to PDF
|
| 1042 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 1043 |
+
if not pdf_path or not pdf_path.exists():
|
| 1044 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 1045 |
+
_write_logs(LOG_PATH, logs)
|
| 1046 |
+
return (
|
| 1047 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 1048 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 1049 |
+
+ "\n".join(logs)
|
| 1050 |
+
+ "</pre>"
|
| 1051 |
+
)
|
| 1052 |
+
try:
|
| 1053 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 1054 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 1055 |
+
html = (
|
| 1056 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 1057 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 1058 |
+
)
|
| 1059 |
+
_write_logs(LOG_PATH, logs)
|
| 1060 |
+
return html
|
| 1061 |
+
except Exception as e:
|
| 1062 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 1063 |
+
_write_logs(LOG_PATH, logs)
|
| 1064 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1065 |
+
|
| 1066 |
def debug_compile_uploaded_zip(zip_file):
|
| 1067 |
"""Compile an uploaded poster zip (user-provided); return preview HTML + PDF path."""
|
| 1068 |
logs = [f"π Debug(upload) at {_now_str()}"]
|