Siyuan Hu commited on
Commit
2bc9abf
Β·
1 Parent(s): 9f23495

feat(debug): add UI to compile repo output.zip and uploaded zip; connect handlers (no binary files)

Browse files
Files changed (1) hide show
  1. app.py +175 -0
app.py CHANGED
@@ -3482,6 +3482,181 @@ def debug_compile_uploaded_zip(zip_file):
3482
  _write_logs(LOG_PATH, logs)
3483
  return f"<div>Compiled but preview failed: {e}</div>", None
3484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3485
  # =====================
3486
  # Gradio pipeline function (ISOLATED)
3487
  # =====================
 
3482
  _write_logs(LOG_PATH, logs)
3483
  return f"<div>Compiled but preview failed: {e}</div>", None
3484
 
3485
+ def debug_compile_output_zip():
3486
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
3487
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
3488
+ zip_path = ROOT / "output.zip"
3489
+ if not zip_path.exists():
3490
+ return (
3491
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
3492
+ + f"<div>Expected at: {zip_path}</div>"
3493
+ )
3494
+
3495
+ # Prepare workspace
3496
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
3497
+ work_zip_dir = WORK_DIR / "zip_proj"
3498
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
3499
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
3500
+ logs.append("Unzipping output.zip β†’ zip_proj/")
3501
+
3502
+ # Extract zip
3503
+ try:
3504
+ import zipfile as _zf
3505
+ with _zf.ZipFile(zip_path, 'r') as zf:
3506
+ zf.extractall(work_zip_dir)
3507
+ except Exception as e:
3508
+ logs.append(f"❌ unzip failed: {e}")
3509
+ _write_logs(LOG_PATH, logs)
3510
+ return "<div style='color:#b00'>Unzip failed.</div>"
3511
+
3512
+ # Locate poster_output.tex (fallback to poster.tex)
3513
+ tex_path = None
3514
+ for name in ("poster_output.tex", "poster.tex"):
3515
+ cand = list(work_zip_dir.rglob(name))
3516
+ if cand:
3517
+ tex_path = cand[0]
3518
+ break
3519
+ if tex_path is None:
3520
+ # fallback: any .tex
3521
+ cand = list(work_zip_dir.rglob("*.tex"))
3522
+ if cand:
3523
+ tex_path = cand[0]
3524
+ if tex_path is None:
3525
+ logs.append("❌ No .tex file found in output.zip")
3526
+ _write_logs(LOG_PATH, logs)
3527
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
3528
+
3529
+ # If left_logo missing, disable \logoleft
3530
+ try:
3531
+ logos_dir = tex_path.parent / "logos"
3532
+ has_left = False
3533
+ if logos_dir.exists():
3534
+ for p in logos_dir.iterdir():
3535
+ if p.is_file() and p.stem == "left_logo":
3536
+ has_left = True
3537
+ break
3538
+ if not has_left:
3539
+ txt = tex_path.read_text(encoding="utf-8")
3540
+ if "\\logoleft" in txt:
3541
+ import re as _re
3542
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
3543
+ if new_txt != txt:
3544
+ tex_path.write_text(new_txt, encoding="utf-8")
3545
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
3546
+ except Exception as e:
3547
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
3548
+
3549
+ # Compile to PDF
3550
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
3551
+ if not pdf_path or not pdf_path.exists():
3552
+ logs.append("❌ Failed to compile zip PDF.")
3553
+ _write_logs(LOG_PATH, logs)
3554
+ return (
3555
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
3556
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
3557
+ + "\n".join(logs)
3558
+ + "</pre>"
3559
+ )
3560
+
3561
+ try:
3562
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
3563
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
3564
+ html = (
3565
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
3566
+ + _pdf_to_iframe_html(pdf_path, height="700px")
3567
+ )
3568
+ _write_logs(LOG_PATH, logs)
3569
+ return html
3570
+ except Exception as e:
3571
+ logs.append(f"⚠️ preview failed: {e}")
3572
+ _write_logs(LOG_PATH, logs)
3573
+ return f"<div>Compiled but preview failed: {e}</div>"
3574
+
3575
+ def debug_compile_uploaded_zip(zip_file):
3576
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
3577
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
3578
+ if not zip_file:
3579
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
3580
+ # Prepare workspace
3581
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
3582
+ work_zip_dir = WORK_DIR / "zip_upload"
3583
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
3584
+ # Save uploaded zip
3585
+ up_path = work_zip_dir / "input.zip"
3586
+ try:
3587
+ shutil.copy(zip_file.name, up_path)
3588
+ except Exception as e:
3589
+ logs.append(f"❌ save upload failed: {e}")
3590
+ _write_logs(LOG_PATH, logs)
3591
+ return "<div style='color:#b00'>Save upload failed.</div>"
3592
+ # Extract
3593
+ try:
3594
+ import zipfile as _zf
3595
+ with _zf.ZipFile(up_path, 'r') as zf:
3596
+ zf.extractall(work_zip_dir)
3597
+ except Exception as e:
3598
+ logs.append(f"❌ unzip failed: {e}")
3599
+ _write_logs(LOG_PATH, logs)
3600
+ return "<div style='color:#b00'>Unzip failed.</div>"
3601
+ # Find tex
3602
+ tex_path = None
3603
+ for name in ("poster_output.tex", "poster.tex"):
3604
+ cand = list(work_zip_dir.rglob(name))
3605
+ if cand:
3606
+ tex_path = cand[0]
3607
+ break
3608
+ if tex_path is None:
3609
+ cand = list(work_zip_dir.rglob("*.tex"))
3610
+ if cand:
3611
+ tex_path = cand[0]
3612
+ if tex_path is None:
3613
+ logs.append("❌ No .tex file found in uploaded zip")
3614
+ _write_logs(LOG_PATH, logs)
3615
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
3616
+ # Disable logoleft if missing
3617
+ try:
3618
+ logos_dir = tex_path.parent / "logos"
3619
+ has_left = False
3620
+ if logos_dir.exists():
3621
+ for p in logos_dir.iterdir():
3622
+ if p.is_file() and p.stem == "left_logo":
3623
+ has_left = True
3624
+ break
3625
+ if not has_left:
3626
+ txt = tex_path.read_text(encoding="utf-8")
3627
+ if "\\logoleft" in txt:
3628
+ import re as _re
3629
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
3630
+ if new_txt != txt:
3631
+ tex_path.write_text(new_txt, encoding="utf-8")
3632
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
3633
+ except Exception as e:
3634
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
3635
+ # Compile
3636
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
3637
+ if not pdf_path or not pdf_path.exists():
3638
+ logs.append("❌ Failed to compile uploaded zip PDF.")
3639
+ _write_logs(LOG_PATH, logs)
3640
+ return (
3641
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
3642
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
3643
+ + "\n".join(logs)
3644
+ + "</pre>"
3645
+ )
3646
+ try:
3647
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
3648
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
3649
+ html = (
3650
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
3651
+ + _pdf_to_iframe_html(pdf_path, height="700px")
3652
+ )
3653
+ _write_logs(LOG_PATH, logs)
3654
+ return html
3655
+ except Exception as e:
3656
+ logs.append(f"⚠️ preview failed: {e}")
3657
+ _write_logs(LOG_PATH, logs)
3658
+ return f"<div>Compiled but preview failed: {e}</div>"
3659
+
3660
  # =====================
3661
  # Gradio pipeline function (ISOLATED)
3662
  # =====================