Siyuan Hu commited on
Commit
f9fa176
Β·
1 Parent(s): 6c7c5ff

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
@@ -1365,6 +1365,181 @@ def debug_compile_uploaded_zip(zip_file):
1365
  _write_logs(LOG_PATH, logs)
1366
  return f"<div>Compiled but preview failed: {e}</div>"
1367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1368
  # =====================
1369
  # Gradio pipeline function (ISOLATED)
1370
  # =====================
 
1365
  _write_logs(LOG_PATH, logs)
1366
  return f"<div>Compiled but preview failed: {e}</div>"
1367
 
1368
+ def debug_compile_output_zip():
1369
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
1370
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
1371
+ zip_path = ROOT / "output.zip"
1372
+ if not zip_path.exists():
1373
+ return (
1374
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
1375
+ + f"<div>Expected at: {zip_path}</div>"
1376
+ )
1377
+
1378
+ # Prepare workspace
1379
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1380
+ work_zip_dir = WORK_DIR / "zip_proj"
1381
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1382
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
1383
+ logs.append("Unzipping output.zip β†’ zip_proj/")
1384
+
1385
+ # Extract zip
1386
+ try:
1387
+ import zipfile as _zf
1388
+ with _zf.ZipFile(zip_path, 'r') as zf:
1389
+ zf.extractall(work_zip_dir)
1390
+ except Exception as e:
1391
+ logs.append(f"❌ unzip failed: {e}")
1392
+ _write_logs(LOG_PATH, logs)
1393
+ return "<div style='color:#b00'>Unzip failed.</div>"
1394
+
1395
+ # Locate poster_output.tex (fallback to poster.tex)
1396
+ tex_path = None
1397
+ for name in ("poster_output.tex", "poster.tex"):
1398
+ cand = list(work_zip_dir.rglob(name))
1399
+ if cand:
1400
+ tex_path = cand[0]
1401
+ break
1402
+ if tex_path is None:
1403
+ # fallback: any .tex
1404
+ cand = list(work_zip_dir.rglob("*.tex"))
1405
+ if cand:
1406
+ tex_path = cand[0]
1407
+ if tex_path is None:
1408
+ logs.append("❌ No .tex file found in output.zip")
1409
+ _write_logs(LOG_PATH, logs)
1410
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
1411
+
1412
+ # If left_logo missing, disable \logoleft
1413
+ try:
1414
+ logos_dir = tex_path.parent / "logos"
1415
+ has_left = False
1416
+ if logos_dir.exists():
1417
+ for p in logos_dir.iterdir():
1418
+ if p.is_file() and p.stem == "left_logo":
1419
+ has_left = True
1420
+ break
1421
+ if not has_left:
1422
+ txt = tex_path.read_text(encoding="utf-8")
1423
+ if "\\logoleft" in txt:
1424
+ import re as _re
1425
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
1426
+ if new_txt != txt:
1427
+ tex_path.write_text(new_txt, encoding="utf-8")
1428
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
1429
+ except Exception as e:
1430
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
1431
+
1432
+ # Compile to PDF
1433
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
1434
+ if not pdf_path or not pdf_path.exists():
1435
+ logs.append("❌ Failed to compile zip PDF.")
1436
+ _write_logs(LOG_PATH, logs)
1437
+ return (
1438
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
1439
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
1440
+ + "\n".join(logs)
1441
+ + "</pre>"
1442
+ )
1443
+
1444
+ try:
1445
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
1446
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
1447
+ html = (
1448
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
1449
+ + _pdf_to_iframe_html(pdf_path, height="700px")
1450
+ )
1451
+ _write_logs(LOG_PATH, logs)
1452
+ return html
1453
+ except Exception as e:
1454
+ logs.append(f"⚠️ preview failed: {e}")
1455
+ _write_logs(LOG_PATH, logs)
1456
+ return f"<div>Compiled but preview failed: {e}</div>"
1457
+
1458
+ def debug_compile_uploaded_zip(zip_file):
1459
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
1460
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
1461
+ if not zip_file:
1462
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
1463
+ # Prepare workspace
1464
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1465
+ work_zip_dir = WORK_DIR / "zip_upload"
1466
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1467
+ # Save uploaded zip
1468
+ up_path = work_zip_dir / "input.zip"
1469
+ try:
1470
+ shutil.copy(zip_file.name, up_path)
1471
+ except Exception as e:
1472
+ logs.append(f"❌ save upload failed: {e}")
1473
+ _write_logs(LOG_PATH, logs)
1474
+ return "<div style='color:#b00'>Save upload failed.</div>"
1475
+ # Extract
1476
+ try:
1477
+ import zipfile as _zf
1478
+ with _zf.ZipFile(up_path, 'r') as zf:
1479
+ zf.extractall(work_zip_dir)
1480
+ except Exception as e:
1481
+ logs.append(f"❌ unzip failed: {e}")
1482
+ _write_logs(LOG_PATH, logs)
1483
+ return "<div style='color:#b00'>Unzip failed.</div>"
1484
+ # Find tex
1485
+ tex_path = None
1486
+ for name in ("poster_output.tex", "poster.tex"):
1487
+ cand = list(work_zip_dir.rglob(name))
1488
+ if cand:
1489
+ tex_path = cand[0]
1490
+ break
1491
+ if tex_path is None:
1492
+ cand = list(work_zip_dir.rglob("*.tex"))
1493
+ if cand:
1494
+ tex_path = cand[0]
1495
+ if tex_path is None:
1496
+ logs.append("❌ No .tex file found in uploaded zip")
1497
+ _write_logs(LOG_PATH, logs)
1498
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
1499
+ # Disable logoleft if missing
1500
+ try:
1501
+ logos_dir = tex_path.parent / "logos"
1502
+ has_left = False
1503
+ if logos_dir.exists():
1504
+ for p in logos_dir.iterdir():
1505
+ if p.is_file() and p.stem == "left_logo":
1506
+ has_left = True
1507
+ break
1508
+ if not has_left:
1509
+ txt = tex_path.read_text(encoding="utf-8")
1510
+ if "\\logoleft" in txt:
1511
+ import re as _re
1512
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
1513
+ if new_txt != txt:
1514
+ tex_path.write_text(new_txt, encoding="utf-8")
1515
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
1516
+ except Exception as e:
1517
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
1518
+ # Compile
1519
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
1520
+ if not pdf_path or not pdf_path.exists():
1521
+ logs.append("❌ Failed to compile uploaded zip PDF.")
1522
+ _write_logs(LOG_PATH, logs)
1523
+ return (
1524
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
1525
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
1526
+ + "\n".join(logs)
1527
+ + "</pre>"
1528
+ )
1529
+ try:
1530
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
1531
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
1532
+ html = (
1533
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
1534
+ + _pdf_to_iframe_html(pdf_path, height="700px")
1535
+ )
1536
+ _write_logs(LOG_PATH, logs)
1537
+ return html
1538
+ except Exception as e:
1539
+ logs.append(f"⚠️ preview failed: {e}")
1540
+ _write_logs(LOG_PATH, logs)
1541
+ return f"<div>Compiled but preview failed: {e}</div>"
1542
+
1543
  # =====================
1544
  # Gradio pipeline function (ISOLATED)
1545
  # =====================