Siyuan Hu commited on
Commit
02328c0
·
1 Parent(s): 9660b84

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