potamides commited on
Commit
f838f7e
1 Parent(s): fb47f3a

fix: switch to custom docker image to get up-to-date texlive

Browse files
Files changed (3) hide show
  1. Dockerfile +94 -0
  2. README.md +1 -3
  3. app.py +17 -114
Dockerfile ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Custom docker image for AutomaTikZ, based on the default HF Spaces GPU image.
2
+ # The default image installs a tex live version that is too old for our needs,
3
+ # so we install the latest one oureselves. The modifications stay close to the
4
+ # source image, so, should the official image be updated to a newer ubuntu
5
+ # version, it could be changed back to a gradio space without much effort.
6
+
7
+ ## system setup
8
+ # -----------------------------------------------------------------------------
9
+ FROM docker.io/nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04@sha256:69cd988555eabe116f76acc754b363eee75f37674c23adb2b523f5fa32543984
10
+
11
+ ARG DEBIAN_FRONTEND=noninteractive
12
+
13
+ RUN apt-get update && apt-get install -y \
14
+ git \
15
+ make \
16
+ build-essential \
17
+ libssl-dev \
18
+ zlib1g-dev \
19
+ libbz2-dev \
20
+ libreadline-dev \
21
+ libsqlite3-dev \
22
+ wget \
23
+ curl \
24
+ llvm \
25
+ libncursesw5-dev \
26
+ xz-utils \
27
+ tk-dev \
28
+ libxml2-dev \
29
+ libxmlsec1-dev \
30
+ libffi-dev \
31
+ liblzma-dev \
32
+ ffmpeg \
33
+ libsm6 \
34
+ libxext6 \
35
+ cmake \
36
+ libgl1-mesa-glx \
37
+ sudo
38
+
39
+ ## setup user
40
+ # -----------------------------------------------------------------------------
41
+ RUN useradd -m -G users -u 1000 user
42
+
43
+ ENV HOME=/home/user \
44
+ PATH=/home/user/.local/bin:$PATH
45
+
46
+ WORKDIR $HOME/app
47
+
48
+ ## custom dependencies
49
+ # -----------------------------------------------------------------------------
50
+
51
+ # Install an up-to-date tex live version
52
+ ENV PATH=/opt/texbin:$PATH
53
+
54
+ RUN curl -LO https://github.com/scottkosty/install-tl-ubuntu/raw/master/install-tl-ubuntu \
55
+ && chmod +x ./install-tl-ubuntu \
56
+ && ./install-tl-ubuntu \
57
+ && rm ./install-tl-ubuntu
58
+
59
+ RUN --mount=target=/root/packages.txt,source=packages.txt \
60
+ apt-get update \
61
+ && sed '/texlive*/d' /root/packages.txt \
62
+ | xargs -r apt-get install -y \
63
+ && rm -rf /var/lib/apt/lists/*
64
+
65
+ # Install pyenv
66
+ ENV PYENV_ROOT=/opt/pyenv
67
+ ENV PATH=$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
68
+
69
+ RUN curl https://pyenv.run | bash \
70
+ && chown -R root:users $PYENV_ROOT \
71
+ && chmod -R g+w $PYENV_ROOT
72
+
73
+ RUN pyenv install 3.10 \
74
+ && pyenv global 3.10 \
75
+ && pyenv rehash \
76
+ && pip install --no-cache-dir --upgrade pip==22.3.1 setuptools wheel \
77
+ && pip install --no-cache-dir datasets "huggingface-hub>=0.12.1" "protobuf<4" "click<8.1" "pydantic~=1.0"
78
+
79
+ RUN --mount=target=requirements.txt,source=requirements.txt \
80
+ pip install --no-cache-dir -r requirements.txt
81
+
82
+ RUN --mount=target=pre-requirements.txt,source=pre-requirements.txt \
83
+ pip install --no-cache-dir -r pre-requirements.txt
84
+
85
+ RUN pip install --no-cache-dir gradio[oauth]==3.47.1 uvicorn>=0.14.0 spaces==0.16.1
86
+
87
+ RUN chown -R user $HOME/app
88
+ COPY --chown=user . $HOME/app
89
+
90
+ ## launch app
91
+ # -----------------------------------------------------------------------------
92
+ USER user
93
+
94
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -3,9 +3,7 @@ title: AutomaTikZ
3
  emoji: 🏝️
4
  colorFrom: blue
5
  colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 3.47.1
8
- app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  suggested_hardware: a100-large
 
3
  emoji: 🏝️
4
  colorFrom: blue
5
  colorTo: indigo
6
+ sdk: docker
 
 
7
  pinned: false
8
  license: apache-2.0
9
  suggested_hardware: a100-large
app.py CHANGED
@@ -1,120 +1,23 @@
1
- from os import getenv, system
2
  from textwrap import dedent
3
  from torch import cuda
4
- from functools import partial
5
 
6
- from tempfile import NamedTemporaryFile
7
  import gradio as gr
8
- from automatikz.infer import TikzDocument
9
- import fitz
10
- #from src.automatikz.examples.webui.webui import get_banner
11
 
12
- tikz=r"""\documentclass[tikz,border=3mm]{standalone}
13
- \usepackage{pgfplots}
14
- \pgfplotsset{compat=1.16}
15
- \usepgfplotslibrary{colormaps}
16
- \tikzset{pics/2d-grid/.style 2 args={code={%
17
- \pgfmathtruncatemacro{\mymax}{max(#1,#2)}
18
- \draw[blue,thick] (0,0) grid[xstep=0.2,ystep=0.2] (\mymax,0.1);
19
- \foreach \x in {0.1,0.3,...,\mymax}
20
- \node[below,font=\sffamily] at (\x,0) {\pgfmathprintnumber{\x}};
21
- \foreach \y in {0.1,0.2,...,\mymax}
22
- \node[left,font=\sffamily] at (0.2,\y) {\pgfmathprintnumber{\y}};
23
- \foreach \sh in {0.05,0.1,...,#1}
24
- \foreach \ch in {0.05,0.1,...,#2}
25
- \path[blue,fill=blue!30] (\sh,\ch) rectangle (\sh+0.05,\ch+0.05);
26
- }}}
27
- \begin{document}
28
- \begin{tikzpicture}[scale=1.5]
29
- \begin{axis}[
30
- view={35}{40},
31
- grid=major,
32
- colormap/violet,
33
- zlabel=$x_1$,
34
- ylabel=$x_2$,
35
- xlabel=$x_3$,
36
- zlabel at={(3.6,2.)},
37
- ylabel at={(2.5,3.6)},
38
- zlabel shift={(0pt, -12pt)},
39
- set layers=standard,
40
- axis on top,
41
- legend style={font=\small,legend cell align=right,
42
- legend plot pos=right,
43
- legend style={draw=none,
44
- /tikz/every even column/.append style={column sep=0.35cm}},
45
- },
46
- ]
47
- \begin{scope}[on background layer]
48
- \pic{2d-grid={17}{7.2}};
49
- \end{scope}
50
- \begin{scope}[on background layer]
51
- \pic{2d-grid={12}{1.5}};
52
- \end{scope}
53
- \begin{scope}[on background layer]
54
- \pic{2d-grid={9}{0.3}};
55
- \end{scope}
56
- \begin{scope}[on background layer]
57
- \pic{2d-grid={9}{0.3}};
58
- \end{scope}
59
- \begin{scope}[on background layer]
60
- \pic{2d-grid={4.2}{0.4}};
61
- \end{scope}
62
- \begin{scope}[on background layer]
63
- \pic{2d-grid={4.2}{0.4}};
64
- \end{scope}
65
- \begin{scope}[on background layer]
66
- \pic{2d-grid={4.2}{0.4}};
67
- \end{scope}
68
- \begin{scope}[on background layer]
69
- \pic{2d-grid={4.2}{0.4}};
70
- \end{scope}
71
- \begin{scope}[on background layer]
72
- \pic{2d-grid={4.2}{0.4}};
73
- \end{scope}
74
- %
75
- \end{axis}
76
- \end{tikzpicture}
77
- \end{document}"""
78
 
79
- def convert_to_svg(pdf):
80
- doc = fitz.open("pdf", pdf.raw) # type: ignore
81
- return doc[0].get_svg_image()
82
-
83
- def tex_compile(
84
- code: str,
85
- timeout: int = 120,
86
- rasterize: bool = False
87
- ):
88
- tikzdoc = TikzDocument(code, timeout=timeout)
89
- if not tikzdoc.has_content:
90
- if tikzdoc.compiled_with_errors:
91
- raise gr.Error("TikZ code did not compile!") # type: ignore
92
- else:
93
- gr.Warning("TikZ code compiled to an empty image!") # type: ignore
94
- elif tikzdoc.compiled_with_errors:
95
- gr.Warning("TikZ code compiled with errors!") # type: ignore
96
-
97
- if rasterize:
98
- yield tikzdoc.rasterize()
99
- else:
100
- with NamedTemporaryFile(suffix=".svg", buffering=0) as tmpfile:
101
- if pdf:=tikzdoc.pdf:
102
- tmpfile.write(convert_to_svg(pdf).encode())
103
- yield tmpfile.name if pdf else None
104
-
105
- with gr.Blocks(css=".gradio-container {text-align: center}", theme=gr.themes.Soft()) as demo:
106
- with open("/etc/os-release") as f:
107
- print(f.read())
108
- system("latexmk --version")
109
- #with open("bla.tex", "w") as f:
110
- # f.write(tikz)
111
- # system("latexmk -f -nobibtex -norc -file-line-error -interaction=nonstopmode bla.tex")
112
- #doc = TikzDocument(tikz)
113
- #print(doc.log, doc.has_content, doc.compiled_with_errors, doc.status)
114
- image = gr.Image()
115
- run_btn = gr.Button("Run", variant="primary")
116
- run_btn.click(
117
- partial(tex_compile, code=tikz),
118
- outputs=image
119
- )
120
- demo.queue().launch()
 
1
+ from os import getenv
2
  from textwrap import dedent
3
  from torch import cuda
 
4
 
 
5
  import gradio as gr
6
+ from src.automatikz.examples.webui.webui import build_ui, get_banner
 
 
7
 
8
+ PUBLIC_DEMO = getenv("SPACE_ID") == "nllg/AutomaTikZ"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ if PUBLIC_DEMO and not cuda.is_available():
11
+ with gr.Blocks(css=".gradio-container {text-align: center}", theme=gr.themes.Soft(), title="AutomaTikZ") as demo:
12
+ badge = "https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-xl.svg"
13
+ link = "https://huggingface.co/spaces/nllg/AutomaTikZ?duplicate=true"
14
+ html = f'<a style="display:inline-block" href="{link}"> <img src="{badge}" alt="Duplicate this Space"> </a>'
15
+ message = dedent("""\
16
+ The size of our models exceeds the resource constraints offered by the
17
+ free tier of Hugging Face Spaces. For full functionality, we recommend
18
+ duplicating this space on a paid private GPU runtime.
19
+ """)
20
+ gr.Markdown(f'{get_banner()}\n{message}\n{html}')
21
+ demo.launch()
22
+ else:
23
+ build_ui(lock=PUBLIC_DEMO).queue().launch(server_name="0.0.0.0", server_port=7860)