Spaces:
Running
Running
Siyuan Hu
commited on
Commit
Β·
0176918
1
Parent(s):
f9fa176
feat(debug): add 'Test last pipeline zip' to compile most recent runs/*/output.zip with local fonts + template precedence; widen TEXINPUTS
Browse files
app.py
CHANGED
|
@@ -1455,6 +1455,114 @@ def debug_compile_output_zip():
|
|
| 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()}"]
|
|
|
|
| 1455 |
_write_logs(LOG_PATH, logs)
|
| 1456 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1457 |
|
| 1458 |
+
def _find_last_pipeline_zip():
|
| 1459 |
+
try:
|
| 1460 |
+
candidates = []
|
| 1461 |
+
for d in RUNS_DIR.iterdir():
|
| 1462 |
+
try:
|
| 1463 |
+
if d.is_dir():
|
| 1464 |
+
z = d / "output.zip"
|
| 1465 |
+
if z.exists():
|
| 1466 |
+
candidates.append((z.stat().st_mtime, z))
|
| 1467 |
+
except Exception:
|
| 1468 |
+
pass
|
| 1469 |
+
if not candidates:
|
| 1470 |
+
return None
|
| 1471 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 1472 |
+
return candidates[0][1]
|
| 1473 |
+
except Exception:
|
| 1474 |
+
return None
|
| 1475 |
+
|
| 1476 |
+
def debug_compile_last_pipeline_zip():
|
| 1477 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 1478 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 1479 |
+
last_zip = _find_last_pipeline_zip()
|
| 1480 |
+
if not last_zip:
|
| 1481 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 1482 |
+
|
| 1483 |
+
# Prepare workspace
|
| 1484 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 1485 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 1486 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 1487 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 1488 |
+
logs.append(f"Using: {last_zip}")
|
| 1489 |
+
|
| 1490 |
+
# Extract zip
|
| 1491 |
+
try:
|
| 1492 |
+
import zipfile as _zf
|
| 1493 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 1494 |
+
zf.extractall(work_zip_dir)
|
| 1495 |
+
except Exception as e:
|
| 1496 |
+
logs.append(f"β unzip failed: {e}")
|
| 1497 |
+
_write_logs(LOG_PATH, logs)
|
| 1498 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 1499 |
+
|
| 1500 |
+
# Locate tex
|
| 1501 |
+
tex_path = None
|
| 1502 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 1503 |
+
cand = list(work_zip_dir.rglob(name))
|
| 1504 |
+
if cand:
|
| 1505 |
+
tex_path = cand[0]
|
| 1506 |
+
break
|
| 1507 |
+
if tex_path is None:
|
| 1508 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 1509 |
+
if cand:
|
| 1510 |
+
tex_path = cand[0]
|
| 1511 |
+
if tex_path is None:
|
| 1512 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 1513 |
+
_write_logs(LOG_PATH, logs)
|
| 1514 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 1515 |
+
|
| 1516 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 1517 |
+
try:
|
| 1518 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 1519 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 1520 |
+
if src_fonts.exists():
|
| 1521 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 1522 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 1523 |
+
out_dir = dst_fonts / rel
|
| 1524 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 1525 |
+
for fn in files:
|
| 1526 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 1527 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 1528 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 1529 |
+
# Copy repository theme .sty next to tex and at root
|
| 1530 |
+
try:
|
| 1531 |
+
tmpl_dir = ROOT / "template"
|
| 1532 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 1533 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 1534 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 1535 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 1536 |
+
except Exception as e:
|
| 1537 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 1538 |
+
except Exception as e:
|
| 1539 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 1540 |
+
|
| 1541 |
+
# Compile to PDF
|
| 1542 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 1543 |
+
if not pdf_path or not pdf_path.exists():
|
| 1544 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 1545 |
+
_write_logs(LOG_PATH, logs)
|
| 1546 |
+
return (
|
| 1547 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 1548 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 1549 |
+
+ "\n".join(logs)
|
| 1550 |
+
+ "</pre>"
|
| 1551 |
+
)
|
| 1552 |
+
try:
|
| 1553 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 1554 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 1555 |
+
html = (
|
| 1556 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 1557 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 1558 |
+
)
|
| 1559 |
+
_write_logs(LOG_PATH, logs)
|
| 1560 |
+
return html
|
| 1561 |
+
except Exception as e:
|
| 1562 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 1563 |
+
_write_logs(LOG_PATH, logs)
|
| 1564 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 1565 |
+
|
| 1566 |
def debug_compile_uploaded_zip(zip_file):
|
| 1567 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 1568 |
logs = [f"π Debug(upload) at {_now_str()}"]
|