Skip to content

Commit c3400ca

Browse files
committed
Make onnx-modifier installable and publish to PyPI (#121)
1 parent e626bca commit c3400ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+163
-167
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ gym/
1414
local_tmp/
1515
*ppt
1616
*pptx
17-
local_test.py
17+
*.local.*
18+
README_pypi.md
19+

Dockerfile

Lines changed: 0 additions & 14 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 ZhangGe
3+
Copyright (c) 2025 ZhangGe
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 12 additions & 33 deletions

README_zh-CN.md

Lines changed: 21 additions & 14 deletions

app.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

app_desktop.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

entry.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from onnx_modifier.flask_server import (launch_flask_server,
2+
build_desktop_app)
3+
4+
MODE = "LAUNCH_SERVER"
5+
assert MODE in ["LAUNCH_SERVER", "BUILD_EXE"]
6+
7+
if MODE == "LAUNCH_SERVER":
8+
launch_flask_server()
9+
else:
10+
build_desktop_app()

onnx_modifier/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .onnx_modifier import *

onnx_modifier/flask_server.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)