Spaces:
Sleeping
Sleeping
fix: lock model
Browse files
app.py
CHANGED
@@ -1,23 +1,120 @@
|
|
1 |
-
from os import getenv
|
2 |
from textwrap import dedent
|
3 |
from torch import cuda
|
|
|
4 |
|
|
|
5 |
import gradio as gr
|
6 |
-
from
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
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()
|