Spaces:
Running
Running
Siyuan Hu
commited on
Commit
Β·
360b5be
1
Parent(s):
2cab1ca
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
|
@@ -1050,6 +1050,114 @@ def debug_compile_output_zip():
|
|
| 1050 |
_write_logs(LOG_PATH, logs)
|
| 1051 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1052 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1053 |
def debug_compile_uploaded_zip(zip_file):
|
| 1054 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 1055 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
|
|
|
| 1050 |
_write_logs(LOG_PATH, logs)
|
| 1051 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1052 |
|
| 1053 |
+
def _find_last_pipeline_zip():
|
| 1054 |
+
try:
|
| 1055 |
+
candidates = []
|
| 1056 |
+
for d in RUNS_DIR.iterdir():
|
| 1057 |
+
try:
|
| 1058 |
+
if d.is_dir():
|
| 1059 |
+
z = d / "output.zip"
|
| 1060 |
+
if z.exists():
|
| 1061 |
+
candidates.append((z.stat().st_mtime, z))
|
| 1062 |
+
except Exception:
|
| 1063 |
+
pass
|
| 1064 |
+
if not candidates:
|
| 1065 |
+
return None
|
| 1066 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 1067 |
+
return candidates[0][1]
|
| 1068 |
+
except Exception:
|
| 1069 |
+
return None
|
| 1070 |
+
|
| 1071 |
+
def debug_compile_last_pipeline_zip():
|
| 1072 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 1073 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 1074 |
+
last_zip = _find_last_pipeline_zip()
|
| 1075 |
+
if not last_zip:
|
| 1076 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 1077 |
+
|
| 1078 |
+
# Prepare workspace
|
| 1079 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 1080 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 1081 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 1082 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 1083 |
+
logs.append(f"Using: {last_zip}")
|
| 1084 |
+
|
| 1085 |
+
# Extract zip
|
| 1086 |
+
try:
|
| 1087 |
+
import zipfile as _zf
|
| 1088 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 1089 |
+
zf.extractall(work_zip_dir)
|
| 1090 |
+
except Exception as e:
|
| 1091 |
+
logs.append(f"β unzip failed: {e}")
|
| 1092 |
+
_write_logs(LOG_PATH, logs)
|
| 1093 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 1094 |
+
|
| 1095 |
+
# Locate tex
|
| 1096 |
+
tex_path = None
|
| 1097 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 1098 |
+
cand = list(work_zip_dir.rglob(name))
|
| 1099 |
+
if cand:
|
| 1100 |
+
tex_path = cand[0]
|
| 1101 |
+
break
|
| 1102 |
+
if tex_path is None:
|
| 1103 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 1104 |
+
if cand:
|
| 1105 |
+
tex_path = cand[0]
|
| 1106 |
+
if tex_path is None:
|
| 1107 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 1108 |
+
_write_logs(LOG_PATH, logs)
|
| 1109 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 1110 |
+
|
| 1111 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 1112 |
+
try:
|
| 1113 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 1114 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 1115 |
+
if src_fonts.exists():
|
| 1116 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 1117 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 1118 |
+
out_dir = dst_fonts / rel
|
| 1119 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 1120 |
+
for fn in files:
|
| 1121 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 1122 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 1123 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 1124 |
+
# Copy repository theme .sty next to tex and at root
|
| 1125 |
+
try:
|
| 1126 |
+
tmpl_dir = ROOT / "template"
|
| 1127 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 1128 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 1129 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 1130 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 1131 |
+
except Exception as e:
|
| 1132 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 1133 |
+
except Exception as e:
|
| 1134 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 1135 |
+
|
| 1136 |
+
# Compile to PDF
|
| 1137 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 1138 |
+
if not pdf_path or not pdf_path.exists():
|
| 1139 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 1140 |
+
_write_logs(LOG_PATH, logs)
|
| 1141 |
+
return (
|
| 1142 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 1143 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 1144 |
+
+ "\n".join(logs)
|
| 1145 |
+
+ "</pre>"
|
| 1146 |
+
)
|
| 1147 |
+
try:
|
| 1148 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 1149 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 1150 |
+
html = (
|
| 1151 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 1152 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 1153 |
+
)
|
| 1154 |
+
_write_logs(LOG_PATH, logs)
|
| 1155 |
+
return html
|
| 1156 |
+
except Exception as e:
|
| 1157 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 1158 |
+
_write_logs(LOG_PATH, logs)
|
| 1159 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1160 |
+
|
| 1161 |
def debug_compile_uploaded_zip(zip_file):
|
| 1162 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 1163 |
logs = [f"π Debug(upload) at {_now_str()}"]
|