Spaces:
Sleeping
Sleeping
Push Bot
commited on
Commit
Β·
338aa8b
1
Parent(s):
43a76a1
Debug: add image preview (first page) with PyMuPDF/pdfium; return PDF path from debug flows; chain .then to render image
Browse files
app.py
CHANGED
|
@@ -596,13 +596,14 @@ def debug_compile():
|
|
| 596 |
return "<div style='color:#555'>Minimal debug disabled. Use 'Test repo output.zip' or 'Test last pipeline zip'.</div>"
|
| 597 |
|
| 598 |
def debug_compile_output_zip():
|
| 599 |
-
"""Compile the repo-root output.zip (a real LaTeX project) and preview
|
| 600 |
# Stage repo output.zip to runs/<id>/output.zip to follow pipeline layout, then delegate
|
| 601 |
zip_path = ROOT / "output.zip"
|
| 602 |
if not zip_path.exists():
|
| 603 |
return (
|
| 604 |
"<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
|
| 605 |
-
+ f"<div>Expected at: {zip_path}</div>"
|
|
|
|
| 606 |
)
|
| 607 |
logs = [f"π Stage(repo zip) at {_now_str()}"]
|
| 608 |
_, WORK_DIR, LOG_PATH, ZIP_PATH = _prepare_workspace(logs)
|
|
@@ -638,7 +639,7 @@ def debug_compile_output_zip():
|
|
| 638 |
except Exception as e:
|
| 639 |
logs.append(f"β unzip failed: {e}")
|
| 640 |
_write_logs(LOG_PATH, logs)
|
| 641 |
-
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 642 |
|
| 643 |
# Locate poster_output.tex (fallback to poster.tex)
|
| 644 |
tex_path = None
|
|
@@ -655,7 +656,7 @@ def debug_compile_output_zip():
|
|
| 655 |
if tex_path is None:
|
| 656 |
logs.append("β No .tex file found in output.zip")
|
| 657 |
_write_logs(LOG_PATH, logs)
|
| 658 |
-
return "<div style='color:#b00'>No .tex found in output.zip</div>"
|
| 659 |
|
| 660 |
# If left_logo missing, disable \logoleft
|
| 661 |
try:
|
|
@@ -728,18 +729,19 @@ def debug_compile_output_zip():
|
|
| 728 |
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 729 |
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 730 |
+ "\n".join(logs)
|
| 731 |
-
+ "</pre>"
|
|
|
|
| 732 |
)
|
| 733 |
|
| 734 |
try:
|
| 735 |
# Use served file path to avoid data: URI issues
|
| 736 |
html = _pdf_preview_html(pdf_path, height="700px")
|
| 737 |
_write_logs(LOG_PATH, logs)
|
| 738 |
-
return html
|
| 739 |
except Exception as e:
|
| 740 |
logs.append(f"β οΈ preview failed: {e}")
|
| 741 |
_write_logs(LOG_PATH, logs)
|
| 742 |
-
return f"<div>Compiled but preview failed: {e}</div>"
|
| 743 |
|
| 744 |
def _find_last_pipeline_zip():
|
| 745 |
try:
|
|
@@ -760,7 +762,7 @@ def _find_last_pipeline_zip():
|
|
| 760 |
return None
|
| 761 |
|
| 762 |
def debug_compile_last_pipeline_zip():
|
| 763 |
-
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview
|
| 764 |
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 765 |
last_zip = _find_last_pipeline_zip()
|
| 766 |
if not last_zip:
|
|
@@ -775,7 +777,7 @@ def debug_compile_last_pipeline_zip():
|
|
| 775 |
logs.append(f"β Auto-stage failed: {e}")
|
| 776 |
return "<div style='color:#b00'>No recent pipeline output.zip found and auto-stage failed.</div>"
|
| 777 |
else:
|
| 778 |
-
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 779 |
|
| 780 |
# Prepare workspace
|
| 781 |
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
|
@@ -792,7 +794,7 @@ def debug_compile_last_pipeline_zip():
|
|
| 792 |
except Exception as e:
|
| 793 |
logs.append(f"β unzip failed: {e}")
|
| 794 |
_write_logs(LOG_PATH, logs)
|
| 795 |
-
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 796 |
|
| 797 |
# Locate tex
|
| 798 |
tex_path = None
|
|
@@ -808,7 +810,7 @@ def debug_compile_last_pipeline_zip():
|
|
| 808 |
if tex_path is None:
|
| 809 |
logs.append("β No .tex file found in last pipeline zip")
|
| 810 |
_write_logs(LOG_PATH, logs)
|
| 811 |
-
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 812 |
|
| 813 |
# Ensure local fonts and theme precedence (same as other debug path)
|
| 814 |
try:
|
|
@@ -844,22 +846,23 @@ def debug_compile_last_pipeline_zip():
|
|
| 844 |
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 845 |
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 846 |
+ "\n".join(logs)
|
| 847 |
-
+ "</pre>"
|
|
|
|
| 848 |
)
|
| 849 |
try:
|
| 850 |
html = _pdf_preview_html(pdf_path, height="700px")
|
| 851 |
_write_logs(LOG_PATH, logs)
|
| 852 |
-
return html
|
| 853 |
except Exception as e:
|
| 854 |
logs.append(f"β οΈ preview failed: {e}")
|
| 855 |
_write_logs(LOG_PATH, logs)
|
| 856 |
-
return f"<div>Compiled but preview failed: {e}</div>"
|
| 857 |
|
| 858 |
def debug_compile_uploaded_zip(zip_file):
|
| 859 |
-
"""Compile an uploaded poster zip (user-provided)
|
| 860 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
| 861 |
if not zip_file:
|
| 862 |
-
return "<div style='color:#b00'>Please upload a .zip file first.</div>"
|
| 863 |
# Prepare workspace
|
| 864 |
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 865 |
work_zip_dir = WORK_DIR / "zip_upload"
|
|
@@ -871,7 +874,7 @@ def debug_compile_uploaded_zip(zip_file):
|
|
| 871 |
except Exception as e:
|
| 872 |
logs.append(f"β save upload failed: {e}")
|
| 873 |
_write_logs(LOG_PATH, logs)
|
| 874 |
-
return "<div style='color:#b00'>Save upload failed.</div>"
|
| 875 |
# Extract
|
| 876 |
try:
|
| 877 |
import zipfile as _zf
|
|
@@ -880,7 +883,7 @@ def debug_compile_uploaded_zip(zip_file):
|
|
| 880 |
except Exception as e:
|
| 881 |
logs.append(f"β unzip failed: {e}")
|
| 882 |
_write_logs(LOG_PATH, logs)
|
| 883 |
-
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 884 |
# Find tex
|
| 885 |
tex_path = None
|
| 886 |
for name in ("poster_output.tex", "poster.tex"):
|
|
@@ -895,7 +898,7 @@ def debug_compile_uploaded_zip(zip_file):
|
|
| 895 |
if tex_path is None:
|
| 896 |
logs.append("β No .tex file found in uploaded zip")
|
| 897 |
_write_logs(LOG_PATH, logs)
|
| 898 |
-
return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
|
| 899 |
# Disable logoleft if missing; also ensure local fonts and override theme to use them
|
| 900 |
try:
|
| 901 |
logos_dir = tex_path.parent / "logos"
|
|
@@ -947,16 +950,17 @@ def debug_compile_uploaded_zip(zip_file):
|
|
| 947 |
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 948 |
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 949 |
+ "\n".join(logs)
|
| 950 |
-
+ "</pre>"
|
|
|
|
| 951 |
)
|
| 952 |
try:
|
| 953 |
html = _pdf_preview_html(pdf_path, height="700px")
|
| 954 |
_write_logs(LOG_PATH, logs)
|
| 955 |
-
return html
|
| 956 |
except Exception as e:
|
| 957 |
logs.append(f"β οΈ preview failed: {e}")
|
| 958 |
_write_logs(LOG_PATH, logs)
|
| 959 |
-
return f"<div>Compiled but preview failed: {e}</div>"
|
| 960 |
|
| 961 |
def debug_compile_output_zip():
|
| 962 |
"""Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
|
|
@@ -1452,8 +1456,10 @@ This work, developed in collaboration with [TVG@Oxford](https://torrvision.com/i
|
|
| 1452 |
outputs=[logs_out, pdf_out, pdf_file_out, zip_out, overleaf_out],
|
| 1453 |
)
|
| 1454 |
_run_evt.then(fn=preview_image_from_pdf, inputs=[pdf_file_out], outputs=[img_out])
|
| 1455 |
-
debug_zip_btn.click(fn=debug_compile_output_zip, inputs=[], outputs=[debug_zip_out])
|
| 1456 |
-
|
|
|
|
|
|
|
| 1457 |
|
| 1458 |
if __name__ == "__main__":
|
| 1459 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 596 |
return "<div style='color:#555'>Minimal debug disabled. Use 'Test repo output.zip' or 'Test last pipeline zip'.</div>"
|
| 597 |
|
| 598 |
def debug_compile_output_zip():
|
| 599 |
+
"""Compile the repo-root output.zip (a real LaTeX project) and return preview HTML + PDF path."""
|
| 600 |
# Stage repo output.zip to runs/<id>/output.zip to follow pipeline layout, then delegate
|
| 601 |
zip_path = ROOT / "output.zip"
|
| 602 |
if not zip_path.exists():
|
| 603 |
return (
|
| 604 |
"<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
|
| 605 |
+
+ f"<div>Expected at: {zip_path}</div>",
|
| 606 |
+
None,
|
| 607 |
)
|
| 608 |
logs = [f"π Stage(repo zip) at {_now_str()}"]
|
| 609 |
_, WORK_DIR, LOG_PATH, ZIP_PATH = _prepare_workspace(logs)
|
|
|
|
| 639 |
except Exception as e:
|
| 640 |
logs.append(f"β unzip failed: {e}")
|
| 641 |
_write_logs(LOG_PATH, logs)
|
| 642 |
+
return "<div style='color:#b00'>Unzip failed.</div>", None
|
| 643 |
|
| 644 |
# Locate poster_output.tex (fallback to poster.tex)
|
| 645 |
tex_path = None
|
|
|
|
| 656 |
if tex_path is None:
|
| 657 |
logs.append("β No .tex file found in output.zip")
|
| 658 |
_write_logs(LOG_PATH, logs)
|
| 659 |
+
return "<div style='color:#b00'>No .tex found in output.zip</div>", None
|
| 660 |
|
| 661 |
# If left_logo missing, disable \logoleft
|
| 662 |
try:
|
|
|
|
| 729 |
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 730 |
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 731 |
+ "\n".join(logs)
|
| 732 |
+
+ "</pre>",
|
| 733 |
+
None,
|
| 734 |
)
|
| 735 |
|
| 736 |
try:
|
| 737 |
# Use served file path to avoid data: URI issues
|
| 738 |
html = _pdf_preview_html(pdf_path, height="700px")
|
| 739 |
_write_logs(LOG_PATH, logs)
|
| 740 |
+
return html, str(pdf_path)
|
| 741 |
except Exception as e:
|
| 742 |
logs.append(f"β οΈ preview failed: {e}")
|
| 743 |
_write_logs(LOG_PATH, logs)
|
| 744 |
+
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 745 |
|
| 746 |
def _find_last_pipeline_zip():
|
| 747 |
try:
|
|
|
|
| 762 |
return None
|
| 763 |
|
| 764 |
def debug_compile_last_pipeline_zip():
|
| 765 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and return preview HTML + PDF path."""
|
| 766 |
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 767 |
last_zip = _find_last_pipeline_zip()
|
| 768 |
if not last_zip:
|
|
|
|
| 777 |
logs.append(f"β Auto-stage failed: {e}")
|
| 778 |
return "<div style='color:#b00'>No recent pipeline output.zip found and auto-stage failed.</div>"
|
| 779 |
else:
|
| 780 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>", None
|
| 781 |
|
| 782 |
# Prepare workspace
|
| 783 |
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
|
|
|
| 794 |
except Exception as e:
|
| 795 |
logs.append(f"β unzip failed: {e}")
|
| 796 |
_write_logs(LOG_PATH, logs)
|
| 797 |
+
return "<div style='color:#b00'>Unzip failed.</div>", None
|
| 798 |
|
| 799 |
# Locate tex
|
| 800 |
tex_path = None
|
|
|
|
| 810 |
if tex_path is None:
|
| 811 |
logs.append("β No .tex file found in last pipeline zip")
|
| 812 |
_write_logs(LOG_PATH, logs)
|
| 813 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>", None
|
| 814 |
|
| 815 |
# Ensure local fonts and theme precedence (same as other debug path)
|
| 816 |
try:
|
|
|
|
| 846 |
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 847 |
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 848 |
+ "\n".join(logs)
|
| 849 |
+
+ "</pre>",
|
| 850 |
+
None,
|
| 851 |
)
|
| 852 |
try:
|
| 853 |
html = _pdf_preview_html(pdf_path, height="700px")
|
| 854 |
_write_logs(LOG_PATH, logs)
|
| 855 |
+
return html, str(pdf_path)
|
| 856 |
except Exception as e:
|
| 857 |
logs.append(f"β οΈ preview failed: {e}")
|
| 858 |
_write_logs(LOG_PATH, logs)
|
| 859 |
+
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 860 |
|
| 861 |
def debug_compile_uploaded_zip(zip_file):
|
| 862 |
+
"""Compile an uploaded poster zip (user-provided); return preview HTML + PDF path."""
|
| 863 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
| 864 |
if not zip_file:
|
| 865 |
+
return "<div style='color:#b00'>Please upload a .zip file first.</div>", None
|
| 866 |
# Prepare workspace
|
| 867 |
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 868 |
work_zip_dir = WORK_DIR / "zip_upload"
|
|
|
|
| 874 |
except Exception as e:
|
| 875 |
logs.append(f"β save upload failed: {e}")
|
| 876 |
_write_logs(LOG_PATH, logs)
|
| 877 |
+
return "<div style='color:#b00'>Save upload failed.</div>", None
|
| 878 |
# Extract
|
| 879 |
try:
|
| 880 |
import zipfile as _zf
|
|
|
|
| 883 |
except Exception as e:
|
| 884 |
logs.append(f"β unzip failed: {e}")
|
| 885 |
_write_logs(LOG_PATH, logs)
|
| 886 |
+
return "<div style='color:#b00'>Unzip failed.</div>", None
|
| 887 |
# Find tex
|
| 888 |
tex_path = None
|
| 889 |
for name in ("poster_output.tex", "poster.tex"):
|
|
|
|
| 898 |
if tex_path is None:
|
| 899 |
logs.append("β No .tex file found in uploaded zip")
|
| 900 |
_write_logs(LOG_PATH, logs)
|
| 901 |
+
return "<div style='color:#b00'>No .tex found in uploaded zip</div>", None
|
| 902 |
# Disable logoleft if missing; also ensure local fonts and override theme to use them
|
| 903 |
try:
|
| 904 |
logos_dir = tex_path.parent / "logos"
|
|
|
|
| 950 |
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 951 |
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 952 |
+ "\n".join(logs)
|
| 953 |
+
+ "</pre>",
|
| 954 |
+
None,
|
| 955 |
)
|
| 956 |
try:
|
| 957 |
html = _pdf_preview_html(pdf_path, height="700px")
|
| 958 |
_write_logs(LOG_PATH, logs)
|
| 959 |
+
return html, str(pdf_path)
|
| 960 |
except Exception as e:
|
| 961 |
logs.append(f"β οΈ preview failed: {e}")
|
| 962 |
_write_logs(LOG_PATH, logs)
|
| 963 |
+
return f"<div>Compiled but preview failed: {e}</div>", None
|
| 964 |
|
| 965 |
def debug_compile_output_zip():
|
| 966 |
"""Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
|
|
|
|
| 1456 |
outputs=[logs_out, pdf_out, pdf_file_out, zip_out, overleaf_out],
|
| 1457 |
)
|
| 1458 |
_run_evt.then(fn=preview_image_from_pdf, inputs=[pdf_file_out], outputs=[img_out])
|
| 1459 |
+
_dz = debug_zip_btn.click(fn=debug_compile_output_zip, inputs=[], outputs=[debug_zip_out, debug_zip_pdfpath])
|
| 1460 |
+
_dz.then(fn=preview_image_from_pdf, inputs=[debug_zip_pdfpath], outputs=[debug_zip_img])
|
| 1461 |
+
_dl = debug_last_btn.click(fn=debug_compile_last_pipeline_zip, inputs=[], outputs=[debug_last_out, debug_last_pdfpath])
|
| 1462 |
+
_dl.then(fn=preview_image_from_pdf, inputs=[debug_last_pdfpath], outputs=[debug_last_img])
|
| 1463 |
|
| 1464 |
if __name__ == "__main__":
|
| 1465 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|