Siyuan Hu commited on
Commit
de944c5
·
1 Parent(s): 2c8e548

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
@@ -1865,6 +1865,181 @@ def debug_compile_uploaded_zip(zip_file):
1865
  _write_logs(LOG_PATH, logs)
1866
  return f"<div>Compiled but preview failed: {e}</div>"
1867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1868
  # =====================
1869
  # Gradio pipeline function (ISOLATED)
1870
  # =====================
 
1865
  _write_logs(LOG_PATH, logs)
1866
  return f"<div>Compiled but preview failed: {e}</div>"
1867
 
1868
+ def debug_compile_output_zip():
1869
+ """Compile the repo-root output.zip (a real LaTeX project) and preview the resulting PDF."""
1870
+ logs = [f"🐞 Debug(real) at {_now_str()}"]
1871
+ zip_path = ROOT / "output.zip"
1872
+ if not zip_path.exists():
1873
+ return (
1874
+ "<div style='color:#b00'><b>output.zip not found at repo root.</b></div>"
1875
+ + f"<div>Expected at: {zip_path}</div>"
1876
+ )
1877
+
1878
+ # Prepare workspace
1879
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1880
+ work_zip_dir = WORK_DIR / "zip_proj"
1881
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1882
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
1883
+ logs.append("Unzipping output.zip → zip_proj/")
1884
+
1885
+ # Extract zip
1886
+ try:
1887
+ import zipfile as _zf
1888
+ with _zf.ZipFile(zip_path, 'r') as zf:
1889
+ zf.extractall(work_zip_dir)
1890
+ except Exception as e:
1891
+ logs.append(f"❌ unzip failed: {e}")
1892
+ _write_logs(LOG_PATH, logs)
1893
+ return "<div style='color:#b00'>Unzip failed.</div>"
1894
+
1895
+ # Locate poster_output.tex (fallback to poster.tex)
1896
+ tex_path = None
1897
+ for name in ("poster_output.tex", "poster.tex"):
1898
+ cand = list(work_zip_dir.rglob(name))
1899
+ if cand:
1900
+ tex_path = cand[0]
1901
+ break
1902
+ if tex_path is None:
1903
+ # fallback: any .tex
1904
+ cand = list(work_zip_dir.rglob("*.tex"))
1905
+ if cand:
1906
+ tex_path = cand[0]
1907
+ if tex_path is None:
1908
+ logs.append("❌ No .tex file found in output.zip")
1909
+ _write_logs(LOG_PATH, logs)
1910
+ return "<div style='color:#b00'>No .tex found in output.zip</div>"
1911
+
1912
+ # If left_logo missing, disable \logoleft
1913
+ try:
1914
+ logos_dir = tex_path.parent / "logos"
1915
+ has_left = False
1916
+ if logos_dir.exists():
1917
+ for p in logos_dir.iterdir():
1918
+ if p.is_file() and p.stem == "left_logo":
1919
+ has_left = True
1920
+ break
1921
+ if not has_left:
1922
+ txt = tex_path.read_text(encoding="utf-8")
1923
+ if "\\logoleft" in txt:
1924
+ import re as _re
1925
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
1926
+ if new_txt != txt:
1927
+ tex_path.write_text(new_txt, encoding="utf-8")
1928
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in zip project.")
1929
+ except Exception as e:
1930
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
1931
+
1932
+ # Compile to PDF
1933
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
1934
+ if not pdf_path or not pdf_path.exists():
1935
+ logs.append("❌ Failed to compile zip PDF.")
1936
+ _write_logs(LOG_PATH, logs)
1937
+ return (
1938
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
1939
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
1940
+ + "\n".join(logs)
1941
+ + "</pre>"
1942
+ )
1943
+
1944
+ try:
1945
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
1946
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
1947
+ html = (
1948
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
1949
+ + _pdf_to_iframe_html(pdf_path, height="700px")
1950
+ )
1951
+ _write_logs(LOG_PATH, logs)
1952
+ return html
1953
+ except Exception as e:
1954
+ logs.append(f"⚠️ preview failed: {e}")
1955
+ _write_logs(LOG_PATH, logs)
1956
+ return f"<div>Compiled but preview failed: {e}</div>"
1957
+
1958
+ def debug_compile_uploaded_zip(zip_file):
1959
+ """Compile an uploaded poster zip (user-provided) and preview PDF."""
1960
+ logs = [f"🐞 Debug(upload) at {_now_str()}"]
1961
+ if not zip_file:
1962
+ return "<div style='color:#b00'>Please upload a .zip file first.</div>"
1963
+ # Prepare workspace
1964
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1965
+ work_zip_dir = WORK_DIR / "zip_upload"
1966
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1967
+ # Save uploaded zip
1968
+ up_path = work_zip_dir / "input.zip"
1969
+ try:
1970
+ shutil.copy(zip_file.name, up_path)
1971
+ except Exception as e:
1972
+ logs.append(f"❌ save upload failed: {e}")
1973
+ _write_logs(LOG_PATH, logs)
1974
+ return "<div style='color:#b00'>Save upload failed.</div>"
1975
+ # Extract
1976
+ try:
1977
+ import zipfile as _zf
1978
+ with _zf.ZipFile(up_path, 'r') as zf:
1979
+ zf.extractall(work_zip_dir)
1980
+ except Exception as e:
1981
+ logs.append(f"❌ unzip failed: {e}")
1982
+ _write_logs(LOG_PATH, logs)
1983
+ return "<div style='color:#b00'>Unzip failed.</div>"
1984
+ # Find tex
1985
+ tex_path = None
1986
+ for name in ("poster_output.tex", "poster.tex"):
1987
+ cand = list(work_zip_dir.rglob(name))
1988
+ if cand:
1989
+ tex_path = cand[0]
1990
+ break
1991
+ if tex_path is None:
1992
+ cand = list(work_zip_dir.rglob("*.tex"))
1993
+ if cand:
1994
+ tex_path = cand[0]
1995
+ if tex_path is None:
1996
+ logs.append("❌ No .tex file found in uploaded zip")
1997
+ _write_logs(LOG_PATH, logs)
1998
+ return "<div style='color:#b00'>No .tex found in uploaded zip</div>"
1999
+ # Disable logoleft if missing
2000
+ try:
2001
+ logos_dir = tex_path.parent / "logos"
2002
+ has_left = False
2003
+ if logos_dir.exists():
2004
+ for p in logos_dir.iterdir():
2005
+ if p.is_file() and p.stem == "left_logo":
2006
+ has_left = True
2007
+ break
2008
+ if not has_left:
2009
+ txt = tex_path.read_text(encoding="utf-8")
2010
+ if "\\logoleft" in txt:
2011
+ import re as _re
2012
+ new_txt = _re.sub(r"^\\\s*logoleft\s*\{.*?\}\s*$", lambda m: "%" + m.group(0), txt, flags=_re.MULTILINE)
2013
+ if new_txt != txt:
2014
+ tex_path.write_text(new_txt, encoding="utf-8")
2015
+ logs.append("ℹ️ No left_logo found; disabled \\logoleft in uploaded project.")
2016
+ except Exception as e:
2017
+ logs.append(f"⚠️ left_logo adjust failed: {e}")
2018
+ # Compile
2019
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2020
+ if not pdf_path or not pdf_path.exists():
2021
+ logs.append("❌ Failed to compile uploaded zip PDF.")
2022
+ _write_logs(LOG_PATH, logs)
2023
+ return (
2024
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2025
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2026
+ + "\n".join(logs)
2027
+ + "</pre>"
2028
+ )
2029
+ try:
2030
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2031
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2032
+ html = (
2033
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2034
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2035
+ )
2036
+ _write_logs(LOG_PATH, logs)
2037
+ return html
2038
+ except Exception as e:
2039
+ logs.append(f"⚠️ preview failed: {e}")
2040
+ _write_logs(LOG_PATH, logs)
2041
+ return f"<div>Compiled but preview failed: {e}</div>"
2042
+
2043
  # =====================
2044
  # Gradio pipeline function (ISOLATED)
2045
  # =====================