Skip to content

Commit c4927f8

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

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

+158
-167
lines changed

.gitignore

+3-1
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

-14
This file was deleted.

LICENSE

+1-1
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

+12-33

README_zh-CN.md

+21-14

app.py

-60
This file was deleted.

app_desktop.py

-39
This file was deleted.

entry.py

+10
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

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .onnx_modifier import *
2+
from .flask_server import *

onnx_modifier/flask_server.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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()

onnx_modifier.py renamed to onnx_modifier/onnx_modifier.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import numpy as np
1212
import onnx
1313
from onnx import numpy_helper
14-
from utils import str2np, str2val
15-
from utils import np2onnxdtype, str2onnxdtype
16-
from utils import make_new_node, make_attr_changed_node, make_input
17-
from utils import get_infered_shape
14+
from .utils import str2np, str2val
15+
from .utils import np2onnxdtype, str2onnxdtype
16+
from .utils import make_new_node, make_attr_changed_node, make_input
17+
from .utils import get_infered_shape
1818

1919
class onnxModifier:
2020
def __init__(self, model_name, model_proto):
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

templates/index.html renamed to onnx_modifier/templates/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@
441441
<button id="add-node" class="graph-op-button-addNode">Add node</button>
442442
<select id="add-node-dropdown" class="graph-op-add-node-dropdown">
443443
</select>
444-
<button id="load-model" class="graph-button-load">LoadModel</button>
445444

446445
<button id="back-button" class="toolbar-back-button" title="Back">
447446
&#x276E;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

pyproject.toml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[project]
2+
name = "onnx-modifier"
3+
version = "1.0.0"
4+
description = "A tool to modify ONNX models in a visualization fashion, based on Netron and Flask."
5+
readme = "README_pypi.md"
6+
authors = [
7+
{ name="Zhang Ge", email="[email protected]" },
8+
]
9+
10+
dependencies = [
11+
"onnx==1.13.0",
12+
"flask==2.2.5",
13+
"onnx-tool==0.8.1"
14+
]
15+
16+
[build-system]
17+
requires = ["flit_core<4"]
18+
build-backend = "flit_core.buildapi"
19+
20+
[project.scripts]
21+
onnx-modifier = "onnx_modifier.flask_server:launch_flask_server"
22+
23+
[project.urls]
24+
Homepage = "https://github.com/ZhangGe6/onnx-modifier"
25+
Issues = "https://github.com/ZhangGe6/onnx-modifier/issues"

utils/toposort.py

Whitespace-only changes.

0 commit comments

Comments
 (0)