Spaces:
Sleeping
Sleeping
Siyuan Hu
commited on
Commit
·
f839dfd
1
Parent(s):
68969ed
feat(debug): add UI to compile repo output.zip and uploaded zip; connect handlers (no binary files)
Browse files
app.py
CHANGED
|
@@ -2438,6 +2438,181 @@ def debug_compile_uploaded_zip(zip_file):
|
|
| 2438 |
_write_logs(LOG_PATH, logs)
|
| 2439 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 2440 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2441 |
# =====================
|
| 2442 |
# Gradio pipeline function (ISOLATED)
|
| 2443 |
# =====================
|
|
|
|
| 2438 |
_write_logs(LOG_PATH, logs)
|
| 2439 |
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 2440 |
|
| 2441 |
+
def debug_compile_output_zip():
|
| 2442 |
+
"""Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
|
| 2443 |
+
logs = [f"🐞 Debug(real) at {_now_str()}"]
|
| 2444 |
+
zip_path = ROOT / "output.zip"
|
| 2445 |
+
if not zip_path.exists():
|
| 2446 |
+
return (
|
| 2447 |
+
"<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
|
| 2448 |
+
+ f"<div>Expected at: {zip_path}</div>"
|
| 2449 |
+
)
|
| 2450 |
+
|
| 2451 |
+
# Prepare workspace
|
| 2452 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 2453 |
+
work_zip_dir = WORK_DIR / "zip_proj"
|
| 2454 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 2455 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 2456 |
+
logs.append("Unzipping output.zip → zip_proj/")
|
| 2457 |
+
|
| 2458 |
+
# Extract zip
|
| 2459 |
+
try:
|
| 2460 |
+
import zipfile as _zf
|
| 2461 |
+
with _zf.ZipFile(zip_path, 'r') as zf:
|
| 2462 |
+
zf.extractall(work_zip_dir)
|
| 2463 |
+
except Exception as e:
|
| 2464 |
+
logs.append(f"❌ unzip failed: {e}")
|
| 2465 |
+
_write_logs(LOG_PATH, logs)
|
| 2466 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 2467 |
+
|
| 2468 |
+
# Locate poster_output.tex (fallback to poster.tex)
|
| 2469 |
+
tex_path = None
|
| 2470 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 2471 |
+
cand = list(work_zip_dir.rglob(name))
|
| 2472 |
+
if cand:
|
| 2473 |
+
tex_path = cand[0]
|
| 2474 |
+
break
|
| 2475 |
+
if tex_path is None:
|
| 2476 |
+
# fallback: any .tex
|
| 2477 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 2478 |
+
if cand:
|
| 2479 |
+
tex_path = cand[0]
|
| 2480 |
+
if tex_path is None:
|
| 2481 |
+
logs.append("❌ No .tex file found in output.zip")
|
| 2482 |
+
_write_logs(LOG_PATH, logs)
|
| 2483 |
+
return "<div style='color:#b00'>No .tex found in output.zip</div>"
|
| 2484 |
+
|
| 2485 |
+
# If left_logo missing, disable \logoleft
|
| 2486 |
+
try:
|
| 2487 |
+
logos_dir = tex_path.parent / "logos"
|
| 2488 |
+
has_left = False
|
| 2489 |
+
if logos_dir.exists():
|
| 2490 |
+
for p in logos_dir.iterdir():
|
| 2491 |
+
if p.is_file() and p.stem == "left_logo":
|
| 2492 |
+
has_left = True
|
| 2493 |
+
break
|
| 2494 |
+
if not has_left:
|
| 2495 |
+
txt = tex_path.read_text(encoding="utf-8")
|
| 2496 |
+
if "\\logoleft" in txt:
|
| 2497 |
+
import re as _re
|
| 2498 |
+
new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
|
| 2499 |
+
if new_txt != txt:
|
| 2500 |
+
tex_path.write_text(new_txt, encoding="utf-8")
|
| 2501 |
+
logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
|
| 2502 |
+
except Exception as e:
|
| 2503 |
+
logs.append(f"⚠️ left_logo adjust failed: {e}")
|
| 2504 |
+
|
| 2505 |
+
# Compile to PDF
|
| 2506 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 2507 |
+
if not pdf_path or not pdf_path.exists():
|
| 2508 |
+
logs.append("❌ Failed to compile zip PDF.")
|
| 2509 |
+
_write_logs(LOG_PATH, logs)
|
| 2510 |
+
return (
|
| 2511 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 2512 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 2513 |
+
+ "\n".join(logs)
|
| 2514 |
+
+ "</pre>"
|
| 2515 |
+
)
|
| 2516 |
+
|
| 2517 |
+
try:
|
| 2518 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 2519 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 2520 |
+
html = (
|
| 2521 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 2522 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 2523 |
+
)
|
| 2524 |
+
_write_logs(LOG_PATH, logs)
|
| 2525 |
+
return html
|
| 2526 |
+
except Exception as e:
|
| 2527 |
+
logs.append(f"⚠️ preview failed: {e}")
|
| 2528 |
+
_write_logs(LOG_PATH, logs)
|
| 2529 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2530 |
+
|
| 2531 |
+
def debug_compile_uploaded_zip(zip_file):
|
| 2532 |
+
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 2533 |
+
logs = [f"🐞 Debug(upload) at {_now_str()}"]
|
| 2534 |
+
if not zip_file:
|
| 2535 |
+
return "<div style='color:#b00'>Please upload a .zip file first.</div>"
|
| 2536 |
+
# Prepare workspace
|
| 2537 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 2538 |
+
work_zip_dir = WORK_DIR / "zip_upload"
|
| 2539 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 2540 |
+
# Save uploaded zip
|
| 2541 |
+
up_path = work_zip_dir / "input.zip"
|
| 2542 |
+
try:
|
| 2543 |
+
shutil.copy(zip_file.name, up_path)
|
| 2544 |
+
except Exception as e:
|
| 2545 |
+
logs.append(f"❌ save upload failed: {e}")
|
| 2546 |
+
_write_logs(LOG_PATH, logs)
|
| 2547 |
+
return "<div style='color:#b00'>Save upload failed.</div>"
|
| 2548 |
+
# Extract
|
| 2549 |
+
try:
|
| 2550 |
+
import zipfile as _zf
|
| 2551 |
+
with _zf.ZipFile(up_path, 'r') as zf:
|
| 2552 |
+
zf.extractall(work_zip_dir)
|
| 2553 |
+
except Exception as e:
|
| 2554 |
+
logs.append(f"❌ unzip failed: {e}")
|
| 2555 |
+
_write_logs(LOG_PATH, logs)
|
| 2556 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 2557 |
+
# Find tex
|
| 2558 |
+
tex_path = None
|
| 2559 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 2560 |
+
cand = list(work_zip_dir.rglob(name))
|
| 2561 |
+
if cand:
|
| 2562 |
+
tex_path = cand[0]
|
| 2563 |
+
break
|
| 2564 |
+
if tex_path is None:
|
| 2565 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 2566 |
+
if cand:
|
| 2567 |
+
tex_path = cand[0]
|
| 2568 |
+
if tex_path is None:
|
| 2569 |
+
logs.append("❌ No .tex file found in uploaded zip")
|
| 2570 |
+
_write_logs(LOG_PATH, logs)
|
| 2571 |
+
return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
|
| 2572 |
+
# Disable logoleft if missing
|
| 2573 |
+
try:
|
| 2574 |
+
logos_dir = tex_path.parent / "logos"
|
| 2575 |
+
has_left = False
|
| 2576 |
+
if logos_dir.exists():
|
| 2577 |
+
for p in logos_dir.iterdir():
|
| 2578 |
+
if p.is_file() and p.stem == "left_logo":
|
| 2579 |
+
has_left = True
|
| 2580 |
+
break
|
| 2581 |
+
if not has_left:
|
| 2582 |
+
txt = tex_path.read_text(encoding="utf-8")
|
| 2583 |
+
if "\\logoleft" in txt:
|
| 2584 |
+
import re as _re
|
| 2585 |
+
new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
|
| 2586 |
+
if new_txt != txt:
|
| 2587 |
+
tex_path.write_text(new_txt, encoding="utf-8")
|
| 2588 |
+
logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
|
| 2589 |
+
except Exception as e:
|
| 2590 |
+
logs.append(f"⚠️ left_logo adjust failed: {e}")
|
| 2591 |
+
# Compile
|
| 2592 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 2593 |
+
if not pdf_path or not pdf_path.exists():
|
| 2594 |
+
logs.append("❌ Failed to compile uploaded zip PDF.")
|
| 2595 |
+
_write_logs(LOG_PATH, logs)
|
| 2596 |
+
return (
|
| 2597 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 2598 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 2599 |
+
+ "\n".join(logs)
|
| 2600 |
+
+ "</pre>"
|
| 2601 |
+
)
|
| 2602 |
+
try:
|
| 2603 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 2604 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 2605 |
+
html = (
|
| 2606 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 2607 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 2608 |
+
)
|
| 2609 |
+
_write_logs(LOG_PATH, logs)
|
| 2610 |
+
return html
|
| 2611 |
+
except Exception as e:
|
| 2612 |
+
logs.append(f"⚠️ preview failed: {e}")
|
| 2613 |
+
_write_logs(LOG_PATH, logs)
|
| 2614 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2615 |
+
|
| 2616 |
# =====================
|
| 2617 |
# Gradio pipeline function (ISOLATED)
|
| 2618 |
# =====================
|