Siyuan Hu commited on
Commit
0925a06
·
1 Parent(s): 5ccd12a

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
@@ -3133,6 +3133,181 @@ def debug_compile_uploaded_zip(zip_file):
3133
  _write_logs(LOG_PATH, logs)
3134
  return f"<div>Compiled but preview failed: {e}</div>", None
3135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3136
  # =====================
3137
  # Gradio pipeline function (ISOLATED)
3138
  # =====================
 
3133
  _write_logs(LOG_PATH, logs)
3134
  return f"<div>Compiled but preview failed: {e}</div>", None
3135
 
3136
+ def debug_compile_output_zip():
3137
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
3138
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
3139
+ zip_path = ROOT / "output.zip"
3140
+ if not zip_path.exists():
3141
+ return (
3142
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
3143
+ + f"<div>Expected at: {zip_path}</div>"
3144
+ )
3145
+
3146
+ # Prepare workspace
3147
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
3148
+ work_zip_dir = WORK_DIR / "zip_proj"
3149
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
3150
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
3151
+ logs.append("Unzipping output.zip → zip_proj/")
3152
+
3153
+ # Extract zip
3154
+ try:
3155
+ import zipfile as _zf
3156
+ with _zf.ZipFile(zip_path, 'r') as zf:
3157
+ zf.extractall(work_zip_dir)
3158
+ except Exception as e:
3159
+ logs.append(f"❌ unzip failed: {e}")
3160
+ _write_logs(LOG_PATH, logs)
3161
+ return "<div style='color:#b00'>Unzip failed.</div>"
3162
+
3163
+ # Locate poster_output.tex (fallback to poster.tex)
3164
+ tex_path = None
3165
+ for name in ("poster_output.tex", "poster.tex"):
3166
+ cand = list(work_zip_dir.rglob(name))
3167
+ if cand:
3168
+ tex_path = cand[0]
3169
+ break
3170
+ if tex_path is None:
3171
+ # fallback: any .tex
3172
+ cand = list(work_zip_dir.rglob("*.tex"))
3173
+ if cand:
3174
+ tex_path = cand[0]
3175
+ if tex_path is None:
3176
+ logs.append("❌ No .tex file found in output.zip")
3177
+ _write_logs(LOG_PATH, logs)
3178
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
3179
+
3180
+ # If left_logo missing, disable \logoleft
3181
+ try:
3182
+ logos_dir = tex_path.parent / "logos"
3183
+ has_left = False
3184
+ if logos_dir.exists():
3185
+ for p in logos_dir.iterdir():
3186
+ if p.is_file() and p.stem == "left_logo":
3187
+ has_left = True
3188
+ break
3189
+ if not has_left:
3190
+ txt = tex_path.read_text(encoding="utf-8")
3191
+ if "\\logoleft" in txt:
3192
+ import re as _re
3193
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
3194
+ if new_txt != txt:
3195
+ tex_path.write_text(new_txt, encoding="utf-8")
3196
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
3197
+ except Exception as e:
3198
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
3199
+
3200
+ # Compile to PDF
3201
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
3202
+ if not pdf_path or not pdf_path.exists():
3203
+ logs.append("❌ Failed to compile zip PDF.")
3204
+ _write_logs(LOG_PATH, logs)
3205
+ return (
3206
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
3207
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
3208
+ + "\n".join(logs)
3209
+ + "</pre>"
3210
+ )
3211
+
3212
+ try:
3213
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
3214
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
3215
+ html = (
3216
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
3217
+ + _pdf_to_iframe_html(pdf_path, height="700px")
3218
+ )
3219
+ _write_logs(LOG_PATH, logs)
3220
+ return html
3221
+ except Exception as e:
3222
+ logs.append(f"⚠️ preview failed: {e}")
3223
+ _write_logs(LOG_PATH, logs)
3224
+ return f"<div>Compiled but preview failed: {e}</div>"
3225
+
3226
+ def debug_compile_uploaded_zip(zip_file):
3227
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
3228
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
3229
+ if not zip_file:
3230
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
3231
+ # Prepare workspace
3232
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
3233
+ work_zip_dir = WORK_DIR / "zip_upload"
3234
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
3235
+ # Save uploaded zip
3236
+ up_path = work_zip_dir / "input.zip"
3237
+ try:
3238
+ shutil.copy(zip_file.name, up_path)
3239
+ except Exception as e:
3240
+ logs.append(f"❌ save upload failed: {e}")
3241
+ _write_logs(LOG_PATH, logs)
3242
+ return "<div style='color:#b00'>Save upload failed.</div>"
3243
+ # Extract
3244
+ try:
3245
+ import zipfile as _zf
3246
+ with _zf.ZipFile(up_path, 'r') as zf:
3247
+ zf.extractall(work_zip_dir)
3248
+ except Exception as e:
3249
+ logs.append(f"❌ unzip failed: {e}")
3250
+ _write_logs(LOG_PATH, logs)
3251
+ return "<div style='color:#b00'>Unzip failed.</div>"
3252
+ # Find tex
3253
+ tex_path = None
3254
+ for name in ("poster_output.tex", "poster.tex"):
3255
+ cand = list(work_zip_dir.rglob(name))
3256
+ if cand:
3257
+ tex_path = cand[0]
3258
+ break
3259
+ if tex_path is None:
3260
+ cand = list(work_zip_dir.rglob("*.tex"))
3261
+ if cand:
3262
+ tex_path = cand[0]
3263
+ if tex_path is None:
3264
+ logs.append("❌ No .tex file found in uploaded zip")
3265
+ _write_logs(LOG_PATH, logs)
3266
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
3267
+ # Disable logoleft if missing
3268
+ try:
3269
+ logos_dir = tex_path.parent / "logos"
3270
+ has_left = False
3271
+ if logos_dir.exists():
3272
+ for p in logos_dir.iterdir():
3273
+ if p.is_file() and p.stem == "left_logo":
3274
+ has_left = True
3275
+ break
3276
+ if not has_left:
3277
+ txt = tex_path.read_text(encoding="utf-8")
3278
+ if "\\logoleft" in txt:
3279
+ import re as _re
3280
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
3281
+ if new_txt != txt:
3282
+ tex_path.write_text(new_txt, encoding="utf-8")
3283
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
3284
+ except Exception as e:
3285
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
3286
+ # Compile
3287
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
3288
+ if not pdf_path or not pdf_path.exists():
3289
+ logs.append("❌ Failed to compile uploaded zip PDF.")
3290
+ _write_logs(LOG_PATH, logs)
3291
+ return (
3292
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
3293
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
3294
+ + "\n".join(logs)
3295
+ + "</pre>"
3296
+ )
3297
+ try:
3298
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
3299
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
3300
+ html = (
3301
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
3302
+ + _pdf_to_iframe_html(pdf_path, height="700px")
3303
+ )
3304
+ _write_logs(LOG_PATH, logs)
3305
+ return html
3306
+ except Exception as e:
3307
+ logs.append(f"⚠️ preview failed: {e}")
3308
+ _write_logs(LOG_PATH, logs)
3309
+ return f"<div>Compiled but preview failed: {e}</div>"
3310
+
3311
  # =====================
3312
  # Gradio pipeline function (ISOLATED)
3313
  # =====================