Skip to content

Commit 30c519d

Browse files
Merge pull request lukas-blecher#179 from Freed-Wu/main
use same parser to parse cli and gui arguments
2 parents 57efee6 + 2580a79 commit 30c519d

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

pix2tex/__main__.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
def main():
3+
from argparse import ArgumentParser
4+
5+
parser = ArgumentParser()
6+
parser.add_argument('-t', '--temperature', type=float, default=.333, help='Softmax sampling frequency')
7+
parser.add_argument('-c', '--config', type=str, default='settings/config.yaml', help='path to config file')
8+
parser.add_argument('-m', '--checkpoint', type=str, default='checkpoints/weights.pth', help='path to weights file')
9+
parser.add_argument('--no-cuda', action='store_true', help='Compute on CPU')
10+
parser.add_argument('--no-resize', action='store_true', help='Resize the image beforehand')
11+
12+
parser.add_argument('-s', '--show', action='store_true', help='Show the rendered predicted latex code (cli only)')
13+
parser.add_argument('-f', '--file', type=str, default=None, help='Predict LaTeX code from image file instead of clipboard (cli only)')
14+
parser.add_argument('-k', '--katex', action='store_true', help='Render the latex code in the browser (cli only)')
15+
16+
parser.add_argument('--gui', action='store_true', help='Use GUI (gui only)')
17+
parser.add_argument('--gnome', action='store_true', help='Use gnome-screenshot to capture screenshot (gui only)')
18+
19+
arguments = parser.parse_args()
20+
21+
import os
22+
import sys
23+
24+
name = os.path.split(sys.argv[0])[-1]
25+
if arguments.gui or arguments.gnome or name in ['pix2tex_gui', 'latexocr']:
26+
from .gui import main
27+
else:
28+
from .cli import main
29+
main(arguments)
30+
31+
32+
if __name__ == '__main__':
33+
main()

pix2tex/cli.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from PIL import Image
55
import os
66
from typing import Tuple
7-
import argparse
87
import logging
98
import yaml
109
import re
@@ -150,17 +149,7 @@ def output_prediction(pred, args):
150149
webbrowser.open(url)
151150

152151

153-
def main():
154-
parser = argparse.ArgumentParser(description='Use model')
155-
parser.add_argument('-t', '--temperature', type=float, default=.333, help='Softmax sampling frequency')
156-
parser.add_argument('-c', '--config', type=str, default='settings/config.yaml')
157-
parser.add_argument('-m', '--checkpoint', type=str, default='checkpoints/weights.pth')
158-
parser.add_argument('-s', '--show', action='store_true', help='Show the rendered predicted latex code')
159-
parser.add_argument('-f', '--file', type=str, default=None, help='Predict LaTeX code from image file instead of clipboard')
160-
parser.add_argument('-k', '--katex', action='store_true', help='Render the latex code in the browser')
161-
parser.add_argument('--no-cuda', action='store_true', help='Compute on CPU')
162-
parser.add_argument('--no-resize', action='store_true', help='Resize the image beforehand')
163-
arguments = parser.parse_args()
152+
def main(arguments):
164153
with in_model_path():
165154
model = LatexOCR(arguments)
166155
file = None
@@ -221,7 +210,3 @@ def main():
221210
except KeyboardInterrupt:
222211
pass
223212
file = None
224-
225-
226-
if __name__ == "__main__":
227-
main()

pix2tex/gui.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22
import os
3-
import argparse
43
import tempfile
54
from PyQt5 import QtCore, QtGui
65
from PyQt5.QtCore import QObject, Qt, pyqtSlot, pyqtSignal, QThread
@@ -298,20 +297,8 @@ def mouseReleaseEvent(self, event):
298297
self.parent.returnSnip(img)
299298

300299

301-
def main():
302-
parser = argparse.ArgumentParser(description='GUI arguments')
303-
parser.add_argument('-t', '--temperature', type=float, default=.2, help='Softmax sampling frequency')
304-
parser.add_argument('-c', '--config', type=str, default='settings/config.yaml', help='path to config file')
305-
parser.add_argument('-m', '--checkpoint', type=str, default='checkpoints/weights.pth', help='path to weights file')
306-
parser.add_argument('--no-cuda', action='store_true', help='Compute on CPU')
307-
parser.add_argument('--no-resize', action='store_true', help='Resize the image beforehand')
308-
parser.add_argument('--gnome', action='store_true', help='Use gnome-screenshot to capture screenshot')
309-
arguments = parser.parse_args()
300+
def main(arguments):
310301
with in_model_path():
311302
app = QApplication(sys.argv)
312303
ex = App(arguments)
313304
sys.exit(app.exec_())
314-
315-
316-
if __name__ == '__main__':
317-
main()

setup.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@
7373
},
7474
entry_points={
7575
'console_scripts': [
76-
'pix2tex_gui = pix2tex.gui:main',
77-
'pix2tex_cli = pix2tex.cli:main',
78-
'latexocr = pix2tex.gui:main',
79-
'pix2tex = pix2tex.cli:main',
76+
'pix2tex_gui = pix2tex.__main__:main',
77+
'pix2tex_cli = pix2tex.__main__:main',
78+
'latexocr = pix2tex.__main__:main',
79+
'pix2tex = pix2tex.__main__:main',
8080
],
8181
},
8282
classifiers=[

0 commit comments

Comments
 (0)