Spaces:
Sleeping
Sleeping
Siyuan Hu
commited on
Commit
·
a3ffcf5
1
Parent(s):
1160de8
feat(debug): add UI to compile repo output.zip and uploaded zip; connect handlers (no binary files)
Browse files
app.py
CHANGED
|
@@ -3765,6 +3765,181 @@ def debug_compile_uploaded_zip(zip_file):
|
|
| 3765 |
_write_logs(LOG_PATH, logs)
|
| 3766 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 3767 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3768 |
# =====================
|
| 3769 |
# Gradio pipeline function (ISOLATED)
|
| 3770 |
# =====================
|
|
|
|
| 3765 |
_write_logs(LOG_PATH, logs)
|
| 3766 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 3767 |
|
| 3768 |
+
def debug_compile_output_zip():
|
| 3769 |
+
"""Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
|
| 3770 |
+
logs = [f"🐞 Debug(real) at {_now_str()}"]
|
| 3771 |
+
zip_path = ROOT / "output.zip"
|
| 3772 |
+
if not zip_path.exists():
|
| 3773 |
+
return (
|
| 3774 |
+
"<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
|
| 3775 |
+
+ f"<div>Expected at: {zip_path}</div>"
|
| 3776 |
+
)
|
| 3777 |
+
|
| 3778 |
+
# Prepare workspace
|
| 3779 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 3780 |
+
work_zip_dir = WORK_DIR / "zip_proj"
|
| 3781 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 3782 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 3783 |
+
logs.append("Unzipping output.zip → zip_proj/")
|
| 3784 |
+
|
| 3785 |
+
# Extract zip
|
| 3786 |
+
try:
|
| 3787 |
+
import zipfile as _zf
|
| 3788 |
+
with _zf.ZipFile(zip_path, 'r') as zf:
|
| 3789 |
+
zf.extractall(work_zip_dir)
|
| 3790 |
+
except Exception as e:
|
| 3791 |
+
logs.append(f"❌ unzip failed: {e}")
|
| 3792 |
+
_write_logs(LOG_PATH, logs)
|
| 3793 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 3794 |
+
|
| 3795 |
+
# Locate poster_output.tex (fallback to poster.tex)
|
| 3796 |
+
tex_path = None
|
| 3797 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 3798 |
+
cand = list(work_zip_dir.rglob(name))
|
| 3799 |
+
if cand:
|
| 3800 |
+
tex_path = cand[0]
|
| 3801 |
+
break
|
| 3802 |
+
if tex_path is None:
|
| 3803 |
+
# fallback: any .tex
|
| 3804 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 3805 |
+
if cand:
|
| 3806 |
+
tex_path = cand[0]
|
| 3807 |
+
if tex_path is None:
|
| 3808 |
+
logs.append("❌ No .tex file found in output.zip")
|
| 3809 |
+
_write_logs(LOG_PATH, logs)
|
| 3810 |
+
return "<div style='color:#b00'>No .tex found in output.zip</div>"
|
| 3811 |
+
|
| 3812 |
+
# If left_logo missing, disable \logoleft
|
| 3813 |
+
try:
|
| 3814 |
+
logos_dir = tex_path.parent / "logos"
|
| 3815 |
+
has_left = False
|
| 3816 |
+
if logos_dir.exists():
|
| 3817 |
+
for p in logos_dir.iterdir():
|
| 3818 |
+
if p.is_file() and p.stem == "left_logo":
|
| 3819 |
+
has_left = True
|
| 3820 |
+
break
|
| 3821 |
+
if not has_left:
|
| 3822 |
+
txt = tex_path.read_text(encoding="utf-8")
|
| 3823 |
+
if "\\logoleft" in txt:
|
| 3824 |
+
import re as _re
|
| 3825 |
+
new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
|
| 3826 |
+
if new_txt != txt:
|
| 3827 |
+
tex_path.write_text(new_txt, encoding="utf-8")
|
| 3828 |
+
logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
|
| 3829 |
+
except Exception as e:
|
| 3830 |
+
logs.append(f"⚠️ left_logo adjust failed: {e}")
|
| 3831 |
+
|
| 3832 |
+
# Compile to PDF
|
| 3833 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 3834 |
+
if not pdf_path or not pdf_path.exists():
|
| 3835 |
+
logs.append("❌ Failed to compile zip PDF.")
|
| 3836 |
+
_write_logs(LOG_PATH, logs)
|
| 3837 |
+
return (
|
| 3838 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 3839 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 3840 |
+
+ "\n".join(logs)
|
| 3841 |
+
+ "</pre>"
|
| 3842 |
+
)
|
| 3843 |
+
|
| 3844 |
+
try:
|
| 3845 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 3846 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 3847 |
+
html = (
|
| 3848 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 3849 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 3850 |
+
)
|
| 3851 |
+
_write_logs(LOG_PATH, logs)
|
| 3852 |
+
return html
|
| 3853 |
+
except Exception as e:
|
| 3854 |
+
logs.append(f"⚠️ preview failed: {e}")
|
| 3855 |
+
_write_logs(LOG_PATH, logs)
|
| 3856 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 3857 |
+
|
| 3858 |
+
def debug_compile_uploaded_zip(zip_file):
|
| 3859 |
+
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 3860 |
+
logs = [f"🐞 Debug(upload) at {_now_str()}"]
|
| 3861 |
+
if not zip_file:
|
| 3862 |
+
return "<div style='color:#b00'>Please upload a .zip file first.</div>"
|
| 3863 |
+
# Prepare workspace
|
| 3864 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 3865 |
+
work_zip_dir = WORK_DIR / "zip_upload"
|
| 3866 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 3867 |
+
# Save uploaded zip
|
| 3868 |
+
up_path = work_zip_dir / "input.zip"
|
| 3869 |
+
try:
|
| 3870 |
+
shutil.copy(zip_file.name, up_path)
|
| 3871 |
+
except Exception as e:
|
| 3872 |
+
logs.append(f"❌ save upload failed: {e}")
|
| 3873 |
+
_write_logs(LOG_PATH, logs)
|
| 3874 |
+
return "<div style='color:#b00'>Save upload failed.</div>"
|
| 3875 |
+
# Extract
|
| 3876 |
+
try:
|
| 3877 |
+
import zipfile as _zf
|
| 3878 |
+
with _zf.ZipFile(up_path, 'r') as zf:
|
| 3879 |
+
zf.extractall(work_zip_dir)
|
| 3880 |
+
except Exception as e:
|
| 3881 |
+
logs.append(f"❌ unzip failed: {e}")
|
| 3882 |
+
_write_logs(LOG_PATH, logs)
|
| 3883 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 3884 |
+
# Find tex
|
| 3885 |
+
tex_path = None
|
| 3886 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 3887 |
+
cand = list(work_zip_dir.rglob(name))
|
| 3888 |
+
if cand:
|
| 3889 |
+
tex_path = cand[0]
|
| 3890 |
+
break
|
| 3891 |
+
if tex_path is None:
|
| 3892 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 3893 |
+
if cand:
|
| 3894 |
+
tex_path = cand[0]
|
| 3895 |
+
if tex_path is None:
|
| 3896 |
+
logs.append("❌ No .tex file found in uploaded zip")
|
| 3897 |
+
_write_logs(LOG_PATH, logs)
|
| 3898 |
+
return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
|
| 3899 |
+
# Disable logoleft if missing
|
| 3900 |
+
try:
|
| 3901 |
+
logos_dir = tex_path.parent / "logos"
|
| 3902 |
+
has_left = False
|
| 3903 |
+
if logos_dir.exists():
|
| 3904 |
+
for p in logos_dir.iterdir():
|
| 3905 |
+
if p.is_file() and p.stem == "left_logo":
|
| 3906 |
+
has_left = True
|
| 3907 |
+
break
|
| 3908 |
+
if not has_left:
|
| 3909 |
+
txt = tex_path.read_text(encoding="utf-8")
|
| 3910 |
+
if "\\logoleft" in txt:
|
| 3911 |
+
import re as _re
|
| 3912 |
+
new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
|
| 3913 |
+
if new_txt != txt:
|
| 3914 |
+
tex_path.write_text(new_txt, encoding="utf-8")
|
| 3915 |
+
logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
|
| 3916 |
+
except Exception as e:
|
| 3917 |
+
logs.append(f"⚠️ left_logo adjust failed: {e}")
|
| 3918 |
+
# Compile
|
| 3919 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 3920 |
+
if not pdf_path or not pdf_path.exists():
|
| 3921 |
+
logs.append("❌ Failed to compile uploaded zip PDF.")
|
| 3922 |
+
_write_logs(LOG_PATH, logs)
|
| 3923 |
+
return (
|
| 3924 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 3925 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 3926 |
+
+ "\n".join(logs)
|
| 3927 |
+
+ "</pre>"
|
| 3928 |
+
)
|
| 3929 |
+
try:
|
| 3930 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 3931 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 3932 |
+
html = (
|
| 3933 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 3934 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 3935 |
+
)
|
| 3936 |
+
_write_logs(LOG_PATH, logs)
|
| 3937 |
+
return html
|
| 3938 |
+
except Exception as e:
|
| 3939 |
+
logs.append(f"⚠️ preview failed: {e}")
|
| 3940 |
+
_write_logs(LOG_PATH, logs)
|
| 3941 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 3942 |
+
|
| 3943 |
# =====================
|
| 3944 |
# Gradio pipeline function (ISOLATED)
|
| 3945 |
# =====================
|