Siyuan Hu commited on
Commit
1e24d8d
·
1 Parent(s): ed6413c

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