Siyuan Hu commited on
Commit
adbef73
Β·
1 Parent(s): 0f29c3b

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
@@ -2532,6 +2532,181 @@ def debug_compile_uploaded_zip(zip_file):
2532
  _write_logs(LOG_PATH, logs)
2533
  return f"<div>Compiled but preview failed: {e}</div>", None
2534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2535
  # =====================
2536
  # Gradio pipeline function (ISOLATED)
2537
  # =====================
 
2532
  _write_logs(LOG_PATH, logs)
2533
  return f"<div>Compiled but preview failed: {e}</div>", None
2534
 
2535
+ def debug_compile_output_zip():
2536
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
2537
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
2538
+ zip_path = ROOT / "output.zip"
2539
+ if not zip_path.exists():
2540
+ return (
2541
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
2542
+ + f"<div>Expected at: {zip_path}</div>"
2543
+ )
2544
+
2545
+ # Prepare workspace
2546
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2547
+ work_zip_dir = WORK_DIR / "zip_proj"
2548
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2549
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
2550
+ logs.append("Unzipping output.zip β†’ zip_proj/")
2551
+
2552
+ # Extract zip
2553
+ try:
2554
+ import zipfile as _zf
2555
+ with _zf.ZipFile(zip_path, 'r') as zf:
2556
+ zf.extractall(work_zip_dir)
2557
+ except Exception as e:
2558
+ logs.append(f"❌ unzip failed: {e}")
2559
+ _write_logs(LOG_PATH, logs)
2560
+ return "<div style='color:#b00'>Unzip failed.</div>"
2561
+
2562
+ # Locate poster_output.tex (fallback to poster.tex)
2563
+ tex_path = None
2564
+ for name in ("poster_output.tex", "poster.tex"):
2565
+ cand = list(work_zip_dir.rglob(name))
2566
+ if cand:
2567
+ tex_path = cand[0]
2568
+ break
2569
+ if tex_path is None:
2570
+ # fallback: any .tex
2571
+ cand = list(work_zip_dir.rglob("*.tex"))
2572
+ if cand:
2573
+ tex_path = cand[0]
2574
+ if tex_path is None:
2575
+ logs.append("❌ No .tex file found in output.zip")
2576
+ _write_logs(LOG_PATH, logs)
2577
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
2578
+
2579
+ # If left_logo missing, disable \logoleft
2580
+ try:
2581
+ logos_dir = tex_path.parent / "logos"
2582
+ has_left = False
2583
+ if logos_dir.exists():
2584
+ for p in logos_dir.iterdir():
2585
+ if p.is_file() and p.stem == "left_logo":
2586
+ has_left = True
2587
+ break
2588
+ if not has_left:
2589
+ txt = tex_path.read_text(encoding="utf-8")
2590
+ if "\\logoleft" in txt:
2591
+ import re as _re
2592
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2593
+ if new_txt != txt:
2594
+ tex_path.write_text(new_txt, encoding="utf-8")
2595
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
2596
+ except Exception as e:
2597
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2598
+
2599
+ # Compile to PDF
2600
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2601
+ if not pdf_path or not pdf_path.exists():
2602
+ logs.append("❌ Failed to compile zip PDF.")
2603
+ _write_logs(LOG_PATH, logs)
2604
+ return (
2605
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2606
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2607
+ + "\n".join(logs)
2608
+ + "</pre>"
2609
+ )
2610
+
2611
+ try:
2612
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2613
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2614
+ html = (
2615
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2616
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2617
+ )
2618
+ _write_logs(LOG_PATH, logs)
2619
+ return html
2620
+ except Exception as e:
2621
+ logs.append(f"⚠️ preview failed: {e}")
2622
+ _write_logs(LOG_PATH, logs)
2623
+ return f"<div>Compiled but preview failed: {e}</div>"
2624
+
2625
+ def debug_compile_uploaded_zip(zip_file):
2626
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
2627
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
2628
+ if not zip_file:
2629
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
2630
+ # Prepare workspace
2631
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2632
+ work_zip_dir = WORK_DIR / "zip_upload"
2633
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2634
+ # Save uploaded zip
2635
+ up_path = work_zip_dir / "input.zip"
2636
+ try:
2637
+ shutil.copy(zip_file.name, up_path)
2638
+ except Exception as e:
2639
+ logs.append(f"❌ save upload failed: {e}")
2640
+ _write_logs(LOG_PATH, logs)
2641
+ return "<div style='color:#b00'>Save upload failed.</div>"
2642
+ # Extract
2643
+ try:
2644
+ import zipfile as _zf
2645
+ with _zf.ZipFile(up_path, 'r') as zf:
2646
+ zf.extractall(work_zip_dir)
2647
+ except Exception as e:
2648
+ logs.append(f"❌ unzip failed: {e}")
2649
+ _write_logs(LOG_PATH, logs)
2650
+ return "<div style='color:#b00'>Unzip failed.</div>"
2651
+ # Find tex
2652
+ tex_path = None
2653
+ for name in ("poster_output.tex", "poster.tex"):
2654
+ cand = list(work_zip_dir.rglob(name))
2655
+ if cand:
2656
+ tex_path = cand[0]
2657
+ break
2658
+ if tex_path is None:
2659
+ cand = list(work_zip_dir.rglob("*.tex"))
2660
+ if cand:
2661
+ tex_path = cand[0]
2662
+ if tex_path is None:
2663
+ logs.append("❌ No .tex file found in uploaded zip")
2664
+ _write_logs(LOG_PATH, logs)
2665
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
2666
+ # Disable logoleft if missing
2667
+ try:
2668
+ logos_dir = tex_path.parent / "logos"
2669
+ has_left = False
2670
+ if logos_dir.exists():
2671
+ for p in logos_dir.iterdir():
2672
+ if p.is_file() and p.stem == "left_logo":
2673
+ has_left = True
2674
+ break
2675
+ if not has_left:
2676
+ txt = tex_path.read_text(encoding="utf-8")
2677
+ if "\\logoleft" in txt:
2678
+ import re as _re
2679
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2680
+ if new_txt != txt:
2681
+ tex_path.write_text(new_txt, encoding="utf-8")
2682
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
2683
+ except Exception as e:
2684
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2685
+ # Compile
2686
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2687
+ if not pdf_path or not pdf_path.exists():
2688
+ logs.append("❌ Failed to compile uploaded zip PDF.")
2689
+ _write_logs(LOG_PATH, logs)
2690
+ return (
2691
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2692
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2693
+ + "\n".join(logs)
2694
+ + "</pre>"
2695
+ )
2696
+ try:
2697
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2698
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2699
+ html = (
2700
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2701
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2702
+ )
2703
+ _write_logs(LOG_PATH, logs)
2704
+ return html
2705
+ except Exception as e:
2706
+ logs.append(f"⚠️ preview failed: {e}")
2707
+ _write_logs(LOG_PATH, logs)
2708
+ return f"<div>Compiled but preview failed: {e}</div>"
2709
+
2710
  # =====================
2711
  # Gradio pipeline function (ISOLATED)
2712
  # =====================