Spaces:
Sleeping
Sleeping
Siyuan Hu
commited on
Commit
·
2bc4d1c
1
Parent(s):
8cbabef
feat(debug): add UI to compile repo output.zip and uploaded zip; connect handlers (no binary files)
Browse files
app.py
CHANGED
|
@@ -1013,6 +1013,181 @@ def debug_compile_uploaded_zip(zip_file):
|
|
| 1013 |
_write_logs(LOG_PATH, logs)
|
| 1014 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 1015 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1016 |
# =====================
|
| 1017 |
# Gradio pipeline function (ISOLATED)
|
| 1018 |
# =====================
|
|
|
|
| 1013 |
_write_logs(LOG_PATH, logs)
|
| 1014 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 1015 |
|
| 1016 |
+
def debug_compile_output_zip():
|
| 1017 |
+
"""Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
|
| 1018 |
+
logs = [f"🐞 Debug(real) at {_now_str()}"]
|
| 1019 |
+
zip_path = ROOT / "output.zip"
|
| 1020 |
+
if not zip_path.exists():
|
| 1021 |
+
return (
|
| 1022 |
+
"<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
|
| 1023 |
+
+ f"<div>Expected at: {zip_path}</div>"
|
| 1024 |
+
)
|
| 1025 |
+
|
| 1026 |
+
# Prepare workspace
|
| 1027 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 1028 |
+
work_zip_dir = WORK_DIR / "zip_proj"
|
| 1029 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 1030 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 1031 |
+
logs.append("Unzipping output.zip → zip_proj/")
|
| 1032 |
+
|
| 1033 |
+
# Extract zip
|
| 1034 |
+
try:
|
| 1035 |
+
import zipfile as _zf
|
| 1036 |
+
with _zf.ZipFile(zip_path, 'r') as zf:
|
| 1037 |
+
zf.extractall(work_zip_dir)
|
| 1038 |
+
except Exception as e:
|
| 1039 |
+
logs.append(f"❌ unzip failed: {e}")
|
| 1040 |
+
_write_logs(LOG_PATH, logs)
|
| 1041 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 1042 |
+
|
| 1043 |
+
# Locate poster_output.tex (fallback to poster.tex)
|
| 1044 |
+
tex_path = None
|
| 1045 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 1046 |
+
cand = list(work_zip_dir.rglob(name))
|
| 1047 |
+
if cand:
|
| 1048 |
+
tex_path = cand[0]
|
| 1049 |
+
break
|
| 1050 |
+
if tex_path is None:
|
| 1051 |
+
# fallback: any .tex
|
| 1052 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 1053 |
+
if cand:
|
| 1054 |
+
tex_path = cand[0]
|
| 1055 |
+
if tex_path is None:
|
| 1056 |
+
logs.append("❌ No .tex file found in output.zip")
|
| 1057 |
+
_write_logs(LOG_PATH, logs)
|
| 1058 |
+
return "<div style='color:#b00'>No .tex found in output.zip</div>"
|
| 1059 |
+
|
| 1060 |
+
# If left_logo missing, disable \logoleft
|
| 1061 |
+
try:
|
| 1062 |
+
logos_dir = tex_path.parent / "logos"
|
| 1063 |
+
has_left = False
|
| 1064 |
+
if logos_dir.exists():
|
| 1065 |
+
for p in logos_dir.iterdir():
|
| 1066 |
+
if p.is_file() and p.stem == "left_logo":
|
| 1067 |
+
has_left = True
|
| 1068 |
+
break
|
| 1069 |
+
if not has_left:
|
| 1070 |
+
txt = tex_path.read_text(encoding="utf-8")
|
| 1071 |
+
if "\\logoleft" in txt:
|
| 1072 |
+
import re as _re
|
| 1073 |
+
new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
|
| 1074 |
+
if new_txt != txt:
|
| 1075 |
+
tex_path.write_text(new_txt, encoding="utf-8")
|
| 1076 |
+
logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
|
| 1077 |
+
except Exception as e:
|
| 1078 |
+
logs.append(f"⚠️ left_logo adjust failed: {e}")
|
| 1079 |
+
|
| 1080 |
+
# Compile to PDF
|
| 1081 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 1082 |
+
if not pdf_path or not pdf_path.exists():
|
| 1083 |
+
logs.append("❌ Failed to compile zip PDF.")
|
| 1084 |
+
_write_logs(LOG_PATH, logs)
|
| 1085 |
+
return (
|
| 1086 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 1087 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 1088 |
+
+ "\n".join(logs)
|
| 1089 |
+
+ "</pre>"
|
| 1090 |
+
)
|
| 1091 |
+
|
| 1092 |
+
try:
|
| 1093 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 1094 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 1095 |
+
html = (
|
| 1096 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 1097 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 1098 |
+
)
|
| 1099 |
+
_write_logs(LOG_PATH, logs)
|
| 1100 |
+
return html
|
| 1101 |
+
except Exception as e:
|
| 1102 |
+
logs.append(f"⚠️ preview failed: {e}")
|
| 1103 |
+
_write_logs(LOG_PATH, logs)
|
| 1104 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1105 |
+
|
| 1106 |
+
def debug_compile_uploaded_zip(zip_file):
|
| 1107 |
+
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 1108 |
+
logs = [f"🐞 Debug(upload) at {_now_str()}"]
|
| 1109 |
+
if not zip_file:
|
| 1110 |
+
return "<div style='color:#b00'>Please upload a .zip file first.</div>"
|
| 1111 |
+
# Prepare workspace
|
| 1112 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 1113 |
+
work_zip_dir = WORK_DIR / "zip_upload"
|
| 1114 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 1115 |
+
# Save uploaded zip
|
| 1116 |
+
up_path = work_zip_dir / "input.zip"
|
| 1117 |
+
try:
|
| 1118 |
+
shutil.copy(zip_file.name, up_path)
|
| 1119 |
+
except Exception as e:
|
| 1120 |
+
logs.append(f"❌ save upload failed: {e}")
|
| 1121 |
+
_write_logs(LOG_PATH, logs)
|
| 1122 |
+
return "<div style='color:#b00'>Save upload failed.</div>"
|
| 1123 |
+
# Extract
|
| 1124 |
+
try:
|
| 1125 |
+
import zipfile as _zf
|
| 1126 |
+
with _zf.ZipFile(up_path, 'r') as zf:
|
| 1127 |
+
zf.extractall(work_zip_dir)
|
| 1128 |
+
except Exception as e:
|
| 1129 |
+
logs.append(f"❌ unzip failed: {e}")
|
| 1130 |
+
_write_logs(LOG_PATH, logs)
|
| 1131 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 1132 |
+
# Find tex
|
| 1133 |
+
tex_path = None
|
| 1134 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 1135 |
+
cand = list(work_zip_dir.rglob(name))
|
| 1136 |
+
if cand:
|
| 1137 |
+
tex_path = cand[0]
|
| 1138 |
+
break
|
| 1139 |
+
if tex_path is None:
|
| 1140 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 1141 |
+
if cand:
|
| 1142 |
+
tex_path = cand[0]
|
| 1143 |
+
if tex_path is None:
|
| 1144 |
+
logs.append("❌ No .tex file found in uploaded zip")
|
| 1145 |
+
_write_logs(LOG_PATH, logs)
|
| 1146 |
+
return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
|
| 1147 |
+
# Disable logoleft if missing
|
| 1148 |
+
try:
|
| 1149 |
+
logos_dir = tex_path.parent / "logos"
|
| 1150 |
+
has_left = False
|
| 1151 |
+
if logos_dir.exists():
|
| 1152 |
+
for p in logos_dir.iterdir():
|
| 1153 |
+
if p.is_file() and p.stem == "left_logo":
|
| 1154 |
+
has_left = True
|
| 1155 |
+
break
|
| 1156 |
+
if not has_left:
|
| 1157 |
+
txt = tex_path.read_text(encoding="utf-8")
|
| 1158 |
+
if "\\logoleft" in txt:
|
| 1159 |
+
import re as _re
|
| 1160 |
+
new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
|
| 1161 |
+
if new_txt != txt:
|
| 1162 |
+
tex_path.write_text(new_txt, encoding="utf-8")
|
| 1163 |
+
logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
|
| 1164 |
+
except Exception as e:
|
| 1165 |
+
logs.append(f"⚠️ left_logo adjust failed: {e}")
|
| 1166 |
+
# Compile
|
| 1167 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 1168 |
+
if not pdf_path or not pdf_path.exists():
|
| 1169 |
+
logs.append("❌ Failed to compile uploaded zip PDF.")
|
| 1170 |
+
_write_logs(LOG_PATH, logs)
|
| 1171 |
+
return (
|
| 1172 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 1173 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 1174 |
+
+ "\n".join(logs)
|
| 1175 |
+
+ "</pre>"
|
| 1176 |
+
)
|
| 1177 |
+
try:
|
| 1178 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 1179 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 1180 |
+
html = (
|
| 1181 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 1182 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 1183 |
+
)
|
| 1184 |
+
_write_logs(LOG_PATH, logs)
|
| 1185 |
+
return html
|
| 1186 |
+
except Exception as e:
|
| 1187 |
+
logs.append(f"⚠️ preview failed: {e}")
|
| 1188 |
+
_write_logs(LOG_PATH, logs)
|
| 1189 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1190 |
+
|
| 1191 |
# =====================
|
| 1192 |
# Gradio pipeline function (ISOLATED)
|
| 1193 |
# =====================
|