taesiri commited on
Commit
cb6e75a
1 Parent(s): 899b1ae

initial commit

Browse files
Files changed (6) hide show
  1. app.py +78 -0
  2. etc/cmd.tex +78 -0
  3. fonts/B Nazanin Bold.ttf +0 -0
  4. fonts/B Nazanin.ttf +0 -0
  5. packages.txt +7 -0
  6. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ from PIL import Image
4
+ import re
5
+
6
+ def convert_and_compile(input_file):
7
+ docx_file = input_file.name
8
+ latex_file = "output.tex"
9
+ pdf_file = "output.pdf"
10
+ png_file = "output.png"
11
+
12
+ # Define the helper functions
13
+ def separate_text_equations(text):
14
+ text = re.sub(r"\\\(", "\n\\(", text)
15
+ text = re.sub(r"\\\)", "\\)\n", text)
16
+ return text
17
+
18
+ def remove_unnecessary_latex_commands(text):
19
+ lines = text.split("\n")
20
+ for i in range(len(lines)):
21
+ line = lines[i]
22
+ if "\\RL" in line:
23
+ line = re.sub(r"\\RL\{([^}]*)\}", r"\1", line)
24
+ if "\\text" in line:
25
+ line = re.sub(r"\\text\{([^}]*)\}", r"\1", line)
26
+ lines[i] = line
27
+ return "\n".join(lines).strip()
28
+
29
+ # Convert .docx to .md
30
+ subprocess.run(["pandoc", "-s", docx_file, "-t", "markdown", "-o", "intermediate.md"], check=True)
31
+
32
+ # Convert .md to .tex
33
+ subprocess.run(["pandoc", "-s", "intermediate.md", "-t", "latex", "-o", "intermediate.tex"], check=True)
34
+
35
+ # Extract content between \begin{document} and \end{document}
36
+ with open("intermediate.tex", "r") as f:
37
+ content = f.read()
38
+ content = re.search(r"\\begin\{document\}([\s\S]*)\\end\{document\}", content).group(1)
39
+
40
+ # Separate text and equations, and remove unnecessary LaTeX commands
41
+ content = separate_text_equations(content)
42
+ content = remove_unnecessary_latex_commands(content)
43
+
44
+ # Generate a new .tex file
45
+ with open(latex_file, "w") as f:
46
+ f.write("\\documentclass[12pt]{standalone}\n")
47
+ f.write("\\input{etc/cmd}\n")
48
+ f.write("\\usepackage[utf8]{inputenc}\n")
49
+ f.write("\\usepackage{amsmath}\n")
50
+ f.write("\\usepackage{amssymb}\n")
51
+ f.write("\\usepackage{hyperref}\n")
52
+ f.write("\\usepackage{varwidth}\n")
53
+ f.write("\\usepackage{adjustbox}\n")
54
+ f.write("\\begin{document}\n")
55
+ f.write("\\begin{adjustbox}{margin=5mm}\n")
56
+ f.write("\\begin{varwidth}{\\linewidth}\n")
57
+ f.write(content)
58
+ f.write("\n\\end{varwidth}\n")
59
+ f.write("\\end{adjustbox}\n")
60
+ f.write("\\end{document}")
61
+
62
+ # Compile LaTeX to PDF with XeLaTeX
63
+ subprocess.run(["xelatex", "-interaction=nonstopmode", latex_file], check=True)
64
+
65
+ # Convert PDF to PNG with pdftoppm
66
+ subprocess.run(["pdftoppm", "-png", "-singlefile", pdf_file, "output"], check=True)
67
+
68
+ # Load the PNG image and return it
69
+ return Image.open(png_file)
70
+
71
+ iface = gr.Interface(
72
+ fn=convert_and_compile,
73
+ inputs=gr.File(label="Upload .DocX"),
74
+ outputs=gr.Image(type="pil", label="Latex Output", show_label=False),
75
+ )
76
+
77
+ iface.launch(debug=True, share=True, server_name="tehran.emma-jewellery.ir", server_port=21401, ssl_certfile="/root/cert.crt", ssl_keyfile="/root/private.key")
78
+ # iface.launch(debug=True, share=True)
etc/cmd.tex ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ \usepackage{amsthm,amssymb,amsmath,amsfonts}
2
+ \usepackage[a4paper, top=25mm, bottom=30mm, left=25mm, right=25mm]{geometry}
3
+ \usepackage[pagebackref=false,colorlinks,linkcolor=black,citecolor=black]{hyperref}
4
+ \usepackage[nameinlink]{cleveref}
5
+ \AtBeginDocument{%
6
+ \crefname{equation}{برابری}{equations}%
7
+ \crefname{chapter}{فصل}{chapters}%
8
+ \crefname{section}{بخش}{sections}%
9
+ \crefname{appendix}{پیوست}{appendices}%
10
+ \crefname{enumi}{مورد}{items}%
11
+ \crefname{footnote}{زیرنویس}{footnotes}%
12
+ \crefname{figure}{شکل}{figures}%
13
+ \crefname{table}{جدول}{tables}%
14
+ \crefname{theorem}{قضیه}{theorems}%
15
+ \crefname{lemma}{لم}{lemmas}%
16
+ \crefname{corollary}{نتیجه}{corollaries}%
17
+ \crefname{proposition}{گزاره}{propositions}%
18
+ \crefname{definition}{تعریف}{definitions}%
19
+ \crefname{result}{نتیجه}{results}%
20
+ \crefname{example}{مثال}{examples}%
21
+ \crefname{remark}{نکته}{remarks}%
22
+ \crefname{note}{یادداشت}{notes}%
23
+ \crefname{observation}{مشاهده}{observations}%
24
+ \crefname{algorithm}{الگوریتم}{algorithms}%
25
+ \crefname{cproof}{برهان}{cproofs}%
26
+ }
27
+
28
+ \usepackage{tikz}
29
+ \usepackage{graphicx}
30
+ \usepackage{color}
31
+
32
+ \usepackage{setspace}
33
+ \doublespacing
34
+
35
+ \usepackage{titletoc}
36
+ \usepackage{tocloft}
37
+ \usepackage{enumitem}
38
+
39
+ \usepackage{algorithm}
40
+ % \usepackage[noend]{algpseudocode}
41
+ \usepackage[noend]{algorithmic}
42
+ \renewcommand{\algorithmicrequire}{\textbf{Input:}}
43
+ \renewcommand{\algorithmicensure}{\textbf{Output:}}
44
+
45
+ \usepackage{tabularx}
46
+ \makeatletter
47
+ \newcommand{\multiline}[1]{%
48
+ \begin{tabularx}{\dimexpr\linewidth-\ALG@thistlm}[t]{@{}X@{}}
49
+ #1
50
+ \end{tabularx}
51
+ }
52
+ \makeatother
53
+
54
+ \usepackage{float}
55
+ \usepackage{verbatim}
56
+ \makeindex
57
+ \usepackage{sectsty}
58
+ \usepackage{xepersian}
59
+ \SepMark{-}
60
+ \settextfont[Scale=1.2,Path=fonts/,BoldFont=B Nazanin Bold.ttf]{B Nazanin.ttf}
61
+ \setlatintextfont{Times New Roman}
62
+ \renewcommand{\labelitemi}{$\bullet$}
63
+
64
+ \theoremstyle{definition}
65
+ \newtheorem{definition}{تعریف}[section]
66
+ \newtheorem{remark}[definition]{نکته}
67
+ \newtheorem{note}[definition]{یادداشت}
68
+ \newtheorem{example}[definition]{نمونه}
69
+ \newtheorem{question}[definition]{سوال}
70
+ \newtheorem{remember}[definition]{یاداوری}
71
+ \newtheorem{observation}[definition]{مشاهده}
72
+ \theoremstyle{theorem}
73
+ \newtheorem{theorem}[definition]{قضیه}
74
+ \newtheorem{lemma}[definition]{لم}
75
+ \newtheorem{proposition}[definition]{گزاره}
76
+ \newtheorem{corollary}[definition]{نتیجه}
77
+ \newtheorem*{cproof}{برهان}
78
+
fonts/B Nazanin Bold.ttf ADDED
Binary file (60 kB). View file
 
fonts/B Nazanin.ttf ADDED
Binary file (60.8 kB). View file
 
packages.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ texlive-full
2
+ texlive-fonts-extra
3
+ texlive-font-utils
4
+ ttf-mscorefonts-installer
5
+ pandoc
6
+ poppler-utils
7
+ unzip
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ Pillow