brian-xetdata commited on
Commit
4fb0ad8
β€’
1 Parent(s): 53bd7a4

No frills netron viewer

Browse files
Files changed (6) hide show
  1. .gitignore +4 -0
  2. Dockerfile +12 -0
  3. README.md +4 -4
  4. proxy.py +67 -0
  5. requirements.txt +3 -0
  6. wrapper.sh +11 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ __pycache__
2
+
3
+ .venv
4
+
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ ENV Path="/home/user/.local/bin:$PATH"
4
+
5
+ WORKDIR /app
6
+
7
+ COPY ./requirements.txt requirements.txt
8
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
9
+
10
+ COPY --chmod=0755 . /app
11
+
12
+ CMD "/app/wrapper.sh"
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Netron Docker
3
- emoji: ⚑
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: docker
7
  pinned: false
8
  ---
 
1
  ---
2
+ title: Netron
3
+ emoji: 🌍
4
+ colorFrom: green
5
+ colorTo: red
6
  sdk: docker
7
  pinned: false
8
  ---
proxy.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ #
3
+ from flask import Flask, request, Response
4
+ import requests # pip package requests
5
+
6
+ app = Flask(__name__)
7
+
8
+ API_HOST = "http://127.0.0.1:8080/"
9
+
10
+ # @app.route('/', defaults={'path': ''}, methods=["GET", "POST"]) # ref. https://medium.com/@zwork101/making-a-flask-proxy-server-online-in-10-lines-of-code-44b8721bca6
11
+
12
+ @app.route('/<path>', methods=["GET", "POST"]) # NOTE: better to specify which methods to be accepted. Otherwise, only GET will be accepted. Ref: https://flask.palletsprojects.com/en/3.0.x/quickstart/#http-methods
13
+ def redirect_to_API_HOST(path): #NOTE var :path will be unused as all path we need will be read from :request ie from flask import request
14
+ forward = ''
15
+ if path == 'netron':
16
+ url = request.args.get('url')
17
+ forward = f'{API_HOST}/?url={url}'
18
+ else:
19
+ forward = request.url.replace(request.host_url, f'{API_HOST}/')
20
+
21
+ res = requests.request( # ref. https://stackoverflow.com/a/36601467/248616
22
+ method = request.method,
23
+ url = forward,
24
+ headers = {k:v for k,v in request.headers if k.lower() != 'host'}, # exclude 'host' header
25
+ data = request.get_data(),
26
+ cookies = request.cookies,
27
+ allow_redirects = False,
28
+ )
29
+
30
+ #region exlcude some keys in :res response
31
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] #NOTE we here exclude all "hop-by-hop headers" defined by RFC 2616 section 13.5.1 ref. https://www.rfc-editor.org/rfc/rfc2616#section-13.5.1
32
+ headers = [
33
+ (k,v) for k,v in res.raw.headers.items()
34
+ if k.lower() not in excluded_headers
35
+ ]
36
+ #endregion exlcude some keys in :res response
37
+
38
+ response = Response(res.content, res.status_code, headers)
39
+ return response
40
+
41
+ @app.get("/")
42
+ def chooser():
43
+ return """
44
+ <html>
45
+ <head>
46
+ <title>Netron</title>
47
+ <script type="text/javascript">
48
+ function setFile() {
49
+ console.log("Query params", window.location.search);
50
+ if (!window.location.search) {
51
+ return;
52
+ }
53
+
54
+ const urlParams = new URLSearchParams(window.location.search);
55
+ const url = urlParams.get('url');
56
+ var iframe = document.getElementById('netron');
57
+ iframe.src = '/netron?url=' + url;
58
+ }
59
+
60
+ document.onload(setFileBefore());
61
+ </script>
62
+ </head>
63
+ <body style="padding: 0; margin: 0; display: flex; flex-direction: row; align-items: center;">
64
+ <iframe id="netron" src="/netron?url=https://huggingface.co/brianronan/chessbot-test/resolve/main/chessbot.pb" style="width: 100%; height: calc(100% - 30px); border: none;"></iframe>
65
+ </body>
66
+ </html>
67
+ """
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask
2
+ netron
3
+ requests
wrapper.sh ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Start the netron server
4
+ netron &
5
+
6
+ # Start the the web app
7
+ flask --app proxy run --host 0.0.0.0 --port 7860 &
8
+
9
+ wait -n
10
+
11
+ exit $?