Siyuan Hu commited on
Commit
ae14754
Β·
1 Parent(s): 8c20ff6

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
@@ -2185,6 +2185,181 @@ def debug_compile_uploaded_zip(zip_file):
2185
  _write_logs(LOG_PATH, logs)
2186
  return f"<div>Compiled but preview failed: {e}</div>", None
2187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2188
  # =====================
2189
  # Gradio pipeline function (ISOLATED)
2190
  # =====================
 
2185
  _write_logs(LOG_PATH, logs)
2186
  return f"<div>Compiled but preview failed: {e}</div>", None
2187
 
2188
+ def debug_compile_output_zip():
2189
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
2190
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
2191
+ zip_path = ROOT / "output.zip"
2192
+ if not zip_path.exists():
2193
+ return (
2194
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
2195
+ + f"<div>Expected at: {zip_path}</div>"
2196
+ )
2197
+
2198
+ # Prepare workspace
2199
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2200
+ work_zip_dir = WORK_DIR / "zip_proj"
2201
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2202
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
2203
+ logs.append("Unzipping output.zip β†’ zip_proj/")
2204
+
2205
+ # Extract zip
2206
+ try:
2207
+ import zipfile as _zf
2208
+ with _zf.ZipFile(zip_path, 'r') as zf:
2209
+ zf.extractall(work_zip_dir)
2210
+ except Exception as e:
2211
+ logs.append(f"❌ unzip failed: {e}")
2212
+ _write_logs(LOG_PATH, logs)
2213
+ return "<div style='color:#b00'>Unzip failed.</div>"
2214
+
2215
+ # Locate poster_output.tex (fallback to poster.tex)
2216
+ tex_path = None
2217
+ for name in ("poster_output.tex", "poster.tex"):
2218
+ cand = list(work_zip_dir.rglob(name))
2219
+ if cand:
2220
+ tex_path = cand[0]
2221
+ break
2222
+ if tex_path is None:
2223
+ # fallback: any .tex
2224
+ cand = list(work_zip_dir.rglob("*.tex"))
2225
+ if cand:
2226
+ tex_path = cand[0]
2227
+ if tex_path is None:
2228
+ logs.append("❌ No .tex file found in output.zip")
2229
+ _write_logs(LOG_PATH, logs)
2230
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
2231
+
2232
+ # If left_logo missing, disable \logoleft
2233
+ try:
2234
+ logos_dir = tex_path.parent / "logos"
2235
+ has_left = False
2236
+ if logos_dir.exists():
2237
+ for p in logos_dir.iterdir():
2238
+ if p.is_file() and p.stem == "left_logo":
2239
+ has_left = True
2240
+ break
2241
+ if not has_left:
2242
+ txt = tex_path.read_text(encoding="utf-8")
2243
+ if "\\logoleft" in txt:
2244
+ import re as _re
2245
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2246
+ if new_txt != txt:
2247
+ tex_path.write_text(new_txt, encoding="utf-8")
2248
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
2249
+ except Exception as e:
2250
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2251
+
2252
+ # Compile to PDF
2253
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2254
+ if not pdf_path or not pdf_path.exists():
2255
+ logs.append("❌ Failed to compile zip PDF.")
2256
+ _write_logs(LOG_PATH, logs)
2257
+ return (
2258
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2259
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2260
+ + "\n".join(logs)
2261
+ + "</pre>"
2262
+ )
2263
+
2264
+ try:
2265
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2266
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2267
+ html = (
2268
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2269
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2270
+ )
2271
+ _write_logs(LOG_PATH, logs)
2272
+ return html
2273
+ except Exception as e:
2274
+ logs.append(f"⚠️ preview failed: {e}")
2275
+ _write_logs(LOG_PATH, logs)
2276
+ return f"<div>Compiled but preview failed: {e}</div>"
2277
+
2278
+ def debug_compile_uploaded_zip(zip_file):
2279
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
2280
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
2281
+ if not zip_file:
2282
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
2283
+ # Prepare workspace
2284
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2285
+ work_zip_dir = WORK_DIR / "zip_upload"
2286
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2287
+ # Save uploaded zip
2288
+ up_path = work_zip_dir / "input.zip"
2289
+ try:
2290
+ shutil.copy(zip_file.name, up_path)
2291
+ except Exception as e:
2292
+ logs.append(f"❌ save upload failed: {e}")
2293
+ _write_logs(LOG_PATH, logs)
2294
+ return "<div style='color:#b00'>Save upload failed.</div>"
2295
+ # Extract
2296
+ try:
2297
+ import zipfile as _zf
2298
+ with _zf.ZipFile(up_path, 'r') as zf:
2299
+ zf.extractall(work_zip_dir)
2300
+ except Exception as e:
2301
+ logs.append(f"❌ unzip failed: {e}")
2302
+ _write_logs(LOG_PATH, logs)
2303
+ return "<div style='color:#b00'>Unzip failed.</div>"
2304
+ # Find tex
2305
+ tex_path = None
2306
+ for name in ("poster_output.tex", "poster.tex"):
2307
+ cand = list(work_zip_dir.rglob(name))
2308
+ if cand:
2309
+ tex_path = cand[0]
2310
+ break
2311
+ if tex_path is None:
2312
+ cand = list(work_zip_dir.rglob("*.tex"))
2313
+ if cand:
2314
+ tex_path = cand[0]
2315
+ if tex_path is None:
2316
+ logs.append("❌ No .tex file found in uploaded zip")
2317
+ _write_logs(LOG_PATH, logs)
2318
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
2319
+ # Disable logoleft if missing
2320
+ try:
2321
+ logos_dir = tex_path.parent / "logos"
2322
+ has_left = False
2323
+ if logos_dir.exists():
2324
+ for p in logos_dir.iterdir():
2325
+ if p.is_file() and p.stem == "left_logo":
2326
+ has_left = True
2327
+ break
2328
+ if not has_left:
2329
+ txt = tex_path.read_text(encoding="utf-8")
2330
+ if "\\logoleft" in txt:
2331
+ import re as _re
2332
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2333
+ if new_txt != txt:
2334
+ tex_path.write_text(new_txt, encoding="utf-8")
2335
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
2336
+ except Exception as e:
2337
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2338
+ # Compile
2339
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2340
+ if not pdf_path or not pdf_path.exists():
2341
+ logs.append("❌ Failed to compile uploaded zip PDF.")
2342
+ _write_logs(LOG_PATH, logs)
2343
+ return (
2344
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2345
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2346
+ + "\n".join(logs)
2347
+ + "</pre>"
2348
+ )
2349
+ try:
2350
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2351
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2352
+ html = (
2353
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2354
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2355
+ )
2356
+ _write_logs(LOG_PATH, logs)
2357
+ return html
2358
+ except Exception as e:
2359
+ logs.append(f"⚠️ preview failed: {e}")
2360
+ _write_logs(LOG_PATH, logs)
2361
+ return f"<div>Compiled but preview failed: {e}</div>"
2362
+
2363
  # =====================
2364
  # Gradio pipeline function (ISOLATED)
2365
  # =====================