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