Siyuan Hu commited on
Commit
f269530
Β·
1 Parent(s): 8cf2e79

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