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