1+ import argparse
2+ import logging
3+ from flask import Flask , render_template , request
4+ from .onnx_modifier import onnxModifier
5+ logging .basicConfig (level = logging .INFO )
6+
7+ app = Flask (__name__ )
8+ onnx_modifier = None
9+
10+ @app .route ('/' )
11+ def index ():
12+ return render_template ('index.html' )
13+
14+ @app .route ('/open_model' , methods = ['POST' ])
15+ def open_model ():
16+ # https://blog.miguelgrinberg.com/post/handling-file-uploads-with-flask
17+ onnx_file = request .files ['file' ]
18+
19+ global onnx_modifier
20+ onnx_modifier = onnxModifier .from_name_protobuf_stream (
21+ onnx_file .filename , onnx_file .stream )
22+
23+ return 'OK' , 200
24+
25+ @app .route ('/download' , methods = ['POST' ])
26+ def modify_and_download_model ():
27+ modify_info = request .get_json ()
28+
29+ global onnx_modifier
30+ onnx_modifier .reload () # allow downloading for multiple times
31+ onnx_modifier .modify (modify_info )
32+ save_path = onnx_modifier .check_and_save_model ()
33+
34+ return save_path
35+
36+ def parse_args ():
37+ parser = argparse .ArgumentParser ()
38+ parser .add_argument ('--host' , type = str , default = '127.0.0.1' ,
39+ help = 'The hostname to listen on. \
40+ Set this to "0.0.0.0" to have the server available externally as well' )
41+ parser .add_argument ('--port' , type = int , default = 5000 ,
42+ help = 'The port of the webserver. Defaults to 5000.' )
43+ parser .add_argument ('--debug' , type = bool , default = False ,
44+ help = 'Enable or disable debug mode.' )
45+
46+ args = parser .parse_args ()
47+ return args
48+
49+ def launch_flask_server ():
50+ args = parse_args ()
51+ app .run (host = args .host , port = args .port , debug = args .debug )
52+
53+ def build_desktop_app ():
54+ '''generating excutable files.
55+
56+ The following are some notes about How I worked for it.
57+ 1. How to make flaskwebgui work as expected:
58+ a. install flaskwebgui: `pip install flaskwebgui`
59+ - flaskwebgui github repo: https://github.com/ClimenteA/flaskwebgui
60+ b. add some scripts to keep server running while gui is running
61+ - see here: https://github.com/ClimenteA/flaskwebgui#install
62+ - I added the code in the static/index.js (find "keep_alive_server()")
63+ c. Then run: `python entry.py`, the web browser will be automatically launched for onnx-modifier
64+
65+ 2. How to generate executable files:
66+ a. For Windows:
67+ - Run `pyinstaller -F -n onnx-modifier -i onnx_modifier/static/favicon.png --add-data "onnx_modifier/templates;templates" --add-data "onnx_modifier/static;static" entry.py`
68+ - see here: https://stackoverflow.com/a/48976223/10096987
69+ - Then we can find the the target `.exe` file in the ./dist folder.
70+ - The icon will not show until we change it in another directory due to Windows Explorer caching.
71+ - see here: https://stackoverflow.com/a/35783199/10096987
72+
73+ b. For Ubuntu (not done):
74+ - Run `pyinstaller -F -n onnx-modifier -i ./static/favicon.png --add-data "templates:templates" --add-data "static:static" app_desktop.py`
75+ - However, I get a file with size of 400+MB
76+
77+ '''
78+ from flaskwebgui import FlaskUI
79+ flask_ui = FlaskUI (app , maximized = True , idle_interval = float ("inf" ))
80+ flask_ui .run ()
0 commit comments