Siyuan Hu commited on
Commit
81ef9fb
Β·
1 Parent(s): 9a8bc43

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
@@ -2848,6 +2848,181 @@ def debug_compile_uploaded_zip(zip_file):
2848
  _write_logs(LOG_PATH, logs)
2849
  return f"<div>Compiled but preview failed: {e}</div>", None
2850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2851
  # =====================
2852
  # Gradio pipeline function (ISOLATED)
2853
  # =====================
 
2848
  _write_logs(LOG_PATH, logs)
2849
  return f"<div>Compiled but preview failed: {e}</div>", None
2850
 
2851
+ def debug_compile_output_zip():
2852
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
2853
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
2854
+ zip_path = ROOT / "output.zip"
2855
+ if not zip_path.exists():
2856
+ return (
2857
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
2858
+ + f"<div>Expected at: {zip_path}</div>"
2859
+ )
2860
+
2861
+ # Prepare workspace
2862
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2863
+ work_zip_dir = WORK_DIR / "zip_proj"
2864
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2865
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
2866
+ logs.append("Unzipping output.zip β†’ zip_proj/")
2867
+
2868
+ # Extract zip
2869
+ try:
2870
+ import zipfile as _zf
2871
+ with _zf.ZipFile(zip_path, 'r') as zf:
2872
+ zf.extractall(work_zip_dir)
2873
+ except Exception as e:
2874
+ logs.append(f"❌ unzip failed: {e}")
2875
+ _write_logs(LOG_PATH, logs)
2876
+ return "<div style='color:#b00'>Unzip failed.</div>"
2877
+
2878
+ # Locate poster_output.tex (fallback to poster.tex)
2879
+ tex_path = None
2880
+ for name in ("poster_output.tex", "poster.tex"):
2881
+ cand = list(work_zip_dir.rglob(name))
2882
+ if cand:
2883
+ tex_path = cand[0]
2884
+ break
2885
+ if tex_path is None:
2886
+ # fallback: any .tex
2887
+ cand = list(work_zip_dir.rglob("*.tex"))
2888
+ if cand:
2889
+ tex_path = cand[0]
2890
+ if tex_path is None:
2891
+ logs.append("❌ No .tex file found in output.zip")
2892
+ _write_logs(LOG_PATH, logs)
2893
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
2894
+
2895
+ # If left_logo missing, disable \logoleft
2896
+ try:
2897
+ logos_dir = tex_path.parent / "logos"
2898
+ has_left = False
2899
+ if logos_dir.exists():
2900
+ for p in logos_dir.iterdir():
2901
+ if p.is_file() and p.stem == "left_logo":
2902
+ has_left = True
2903
+ break
2904
+ if not has_left:
2905
+ txt = tex_path.read_text(encoding="utf-8")
2906
+ if "\\logoleft" in txt:
2907
+ import re as _re
2908
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2909
+ if new_txt != txt:
2910
+ tex_path.write_text(new_txt, encoding="utf-8")
2911
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
2912
+ except Exception as e:
2913
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2914
+
2915
+ # Compile to PDF
2916
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2917
+ if not pdf_path or not pdf_path.exists():
2918
+ logs.append("❌ Failed to compile zip PDF.")
2919
+ _write_logs(LOG_PATH, logs)
2920
+ return (
2921
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2922
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2923
+ + "\n".join(logs)
2924
+ + "</pre>"
2925
+ )
2926
+
2927
+ try:
2928
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2929
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2930
+ html = (
2931
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2932
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2933
+ )
2934
+ _write_logs(LOG_PATH, logs)
2935
+ return html
2936
+ except Exception as e:
2937
+ logs.append(f"⚠️ preview failed: {e}")
2938
+ _write_logs(LOG_PATH, logs)
2939
+ return f"<div>Compiled but preview failed: {e}</div>"
2940
+
2941
+ def debug_compile_uploaded_zip(zip_file):
2942
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
2943
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
2944
+ if not zip_file:
2945
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
2946
+ # Prepare workspace
2947
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2948
+ work_zip_dir = WORK_DIR / "zip_upload"
2949
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2950
+ # Save uploaded zip
2951
+ up_path = work_zip_dir / "input.zip"
2952
+ try:
2953
+ shutil.copy(zip_file.name, up_path)
2954
+ except Exception as e:
2955
+ logs.append(f"❌ save upload failed: {e}")
2956
+ _write_logs(LOG_PATH, logs)
2957
+ return "<div style='color:#b00'>Save upload failed.</div>"
2958
+ # Extract
2959
+ try:
2960
+ import zipfile as _zf
2961
+ with _zf.ZipFile(up_path, 'r') as zf:
2962
+ zf.extractall(work_zip_dir)
2963
+ except Exception as e:
2964
+ logs.append(f"❌ unzip failed: {e}")
2965
+ _write_logs(LOG_PATH, logs)
2966
+ return "<div style='color:#b00'>Unzip failed.</div>"
2967
+ # Find tex
2968
+ tex_path = None
2969
+ for name in ("poster_output.tex", "poster.tex"):
2970
+ cand = list(work_zip_dir.rglob(name))
2971
+ if cand:
2972
+ tex_path = cand[0]
2973
+ break
2974
+ if tex_path is None:
2975
+ cand = list(work_zip_dir.rglob("*.tex"))
2976
+ if cand:
2977
+ tex_path = cand[0]
2978
+ if tex_path is None:
2979
+ logs.append("❌ No .tex file found in uploaded zip")
2980
+ _write_logs(LOG_PATH, logs)
2981
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
2982
+ # Disable logoleft if missing
2983
+ try:
2984
+ logos_dir = tex_path.parent / "logos"
2985
+ has_left = False
2986
+ if logos_dir.exists():
2987
+ for p in logos_dir.iterdir():
2988
+ if p.is_file() and p.stem == "left_logo":
2989
+ has_left = True
2990
+ break
2991
+ if not has_left:
2992
+ txt = tex_path.read_text(encoding="utf-8")
2993
+ if "\\logoleft" in txt:
2994
+ import re as _re
2995
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2996
+ if new_txt != txt:
2997
+ tex_path.write_text(new_txt, encoding="utf-8")
2998
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
2999
+ except Exception as e:
3000
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
3001
+ # Compile
3002
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
3003
+ if not pdf_path or not pdf_path.exists():
3004
+ logs.append("❌ Failed to compile uploaded zip PDF.")
3005
+ _write_logs(LOG_PATH, logs)
3006
+ return (
3007
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
3008
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
3009
+ + "\n".join(logs)
3010
+ + "</pre>"
3011
+ )
3012
+ try:
3013
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
3014
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
3015
+ html = (
3016
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
3017
+ + _pdf_to_iframe_html(pdf_path, height="700px")
3018
+ )
3019
+ _write_logs(LOG_PATH, logs)
3020
+ return html
3021
+ except Exception as e:
3022
+ logs.append(f"⚠️ preview failed: {e}")
3023
+ _write_logs(LOG_PATH, logs)
3024
+ return f"<div>Compiled but preview failed: {e}</div>"
3025
+
3026
  # =====================
3027
  # Gradio pipeline function (ISOLATED)
3028
  # =====================