Siyuan Hu commited on
Commit
43b6fc7
·
1 Parent(s): 18ca210

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
@@ -2786,6 +2786,181 @@ def debug_compile_uploaded_zip(zip_file):
2786
  _write_logs(LOG_PATH, logs)
2787
  return f"<div>Compiled but preview failed: {e}</div>", None
2788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2789
  # =====================
2790
  # Gradio pipeline function (ISOLATED)
2791
  # =====================
 
2786
  _write_logs(LOG_PATH, logs)
2787
  return f"<div>Compiled but preview failed: {e}</div>", None
2788
 
2789
+ def debug_compile_output_zip():
2790
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
2791
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
2792
+ zip_path = ROOT / "output.zip"
2793
+ if not zip_path.exists():
2794
+ return (
2795
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
2796
+ + f"<div>Expected at: {zip_path}</div>"
2797
+ )
2798
+
2799
+ # Prepare workspace
2800
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2801
+ work_zip_dir = WORK_DIR / "zip_proj"
2802
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2803
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
2804
+ logs.append("Unzipping output.zip → zip_proj/")
2805
+
2806
+ # Extract zip
2807
+ try:
2808
+ import zipfile as _zf
2809
+ with _zf.ZipFile(zip_path, 'r') as zf:
2810
+ zf.extractall(work_zip_dir)
2811
+ except Exception as e:
2812
+ logs.append(f"❌ unzip failed: {e}")
2813
+ _write_logs(LOG_PATH, logs)
2814
+ return "<div style='color:#b00'>Unzip failed.</div>"
2815
+
2816
+ # Locate poster_output.tex (fallback to poster.tex)
2817
+ tex_path = None
2818
+ for name in ("poster_output.tex", "poster.tex"):
2819
+ cand = list(work_zip_dir.rglob(name))
2820
+ if cand:
2821
+ tex_path = cand[0]
2822
+ break
2823
+ if tex_path is None:
2824
+ # fallback: any .tex
2825
+ cand = list(work_zip_dir.rglob("*.tex"))
2826
+ if cand:
2827
+ tex_path = cand[0]
2828
+ if tex_path is None:
2829
+ logs.append("❌ No .tex file found in output.zip")
2830
+ _write_logs(LOG_PATH, logs)
2831
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
2832
+
2833
+ # If left_logo missing, disable \logoleft
2834
+ try:
2835
+ logos_dir = tex_path.parent / "logos"
2836
+ has_left = False
2837
+ if logos_dir.exists():
2838
+ for p in logos_dir.iterdir():
2839
+ if p.is_file() and p.stem == "left_logo":
2840
+ has_left = True
2841
+ break
2842
+ if not has_left:
2843
+ txt = tex_path.read_text(encoding="utf-8")
2844
+ if "\\logoleft" in txt:
2845
+ import re as _re
2846
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2847
+ if new_txt != txt:
2848
+ tex_path.write_text(new_txt, encoding="utf-8")
2849
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
2850
+ except Exception as e:
2851
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2852
+
2853
+ # Compile to PDF
2854
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2855
+ if not pdf_path or not pdf_path.exists():
2856
+ logs.append("❌ Failed to compile zip PDF.")
2857
+ _write_logs(LOG_PATH, logs)
2858
+ return (
2859
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2860
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2861
+ + "\n".join(logs)
2862
+ + "</pre>"
2863
+ )
2864
+
2865
+ try:
2866
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2867
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2868
+ html = (
2869
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2870
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2871
+ )
2872
+ _write_logs(LOG_PATH, logs)
2873
+ return html
2874
+ except Exception as e:
2875
+ logs.append(f"⚠️ preview failed: {e}")
2876
+ _write_logs(LOG_PATH, logs)
2877
+ return f"<div>Compiled but preview failed: {e}</div>"
2878
+
2879
+ def debug_compile_uploaded_zip(zip_file):
2880
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
2881
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
2882
+ if not zip_file:
2883
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
2884
+ # Prepare workspace
2885
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2886
+ work_zip_dir = WORK_DIR / "zip_upload"
2887
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2888
+ # Save uploaded zip
2889
+ up_path = work_zip_dir / "input.zip"
2890
+ try:
2891
+ shutil.copy(zip_file.name, up_path)
2892
+ except Exception as e:
2893
+ logs.append(f"❌ save upload failed: {e}")
2894
+ _write_logs(LOG_PATH, logs)
2895
+ return "<div style='color:#b00'>Save upload failed.</div>"
2896
+ # Extract
2897
+ try:
2898
+ import zipfile as _zf
2899
+ with _zf.ZipFile(up_path, 'r') as zf:
2900
+ zf.extractall(work_zip_dir)
2901
+ except Exception as e:
2902
+ logs.append(f"❌ unzip failed: {e}")
2903
+ _write_logs(LOG_PATH, logs)
2904
+ return "<div style='color:#b00'>Unzip failed.</div>"
2905
+ # Find tex
2906
+ tex_path = None
2907
+ for name in ("poster_output.tex", "poster.tex"):
2908
+ cand = list(work_zip_dir.rglob(name))
2909
+ if cand:
2910
+ tex_path = cand[0]
2911
+ break
2912
+ if tex_path is None:
2913
+ cand = list(work_zip_dir.rglob("*.tex"))
2914
+ if cand:
2915
+ tex_path = cand[0]
2916
+ if tex_path is None:
2917
+ logs.append("❌ No .tex file found in uploaded zip")
2918
+ _write_logs(LOG_PATH, logs)
2919
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
2920
+ # Disable logoleft if missing
2921
+ try:
2922
+ logos_dir = tex_path.parent / "logos"
2923
+ has_left = False
2924
+ if logos_dir.exists():
2925
+ for p in logos_dir.iterdir():
2926
+ if p.is_file() and p.stem == "left_logo":
2927
+ has_left = True
2928
+ break
2929
+ if not has_left:
2930
+ txt = tex_path.read_text(encoding="utf-8")
2931
+ if "\\logoleft" in txt:
2932
+ import re as _re
2933
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2934
+ if new_txt != txt:
2935
+ tex_path.write_text(new_txt, encoding="utf-8")
2936
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
2937
+ except Exception as e:
2938
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2939
+ # Compile
2940
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2941
+ if not pdf_path or not pdf_path.exists():
2942
+ logs.append("❌ Failed to compile uploaded zip PDF.")
2943
+ _write_logs(LOG_PATH, logs)
2944
+ return (
2945
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2946
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2947
+ + "\n".join(logs)
2948
+ + "</pre>"
2949
+ )
2950
+ try:
2951
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2952
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2953
+ html = (
2954
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2955
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2956
+ )
2957
+ _write_logs(LOG_PATH, logs)
2958
+ return html
2959
+ except Exception as e:
2960
+ logs.append(f"⚠️ preview failed: {e}")
2961
+ _write_logs(LOG_PATH, logs)
2962
+ return f"<div>Compiled but preview failed: {e}</div>"
2963
+
2964
  # =====================
2965
  # Gradio pipeline function (ISOLATED)
2966
  # =====================