Spaces:
Sleeping
Sleeping
import os | |
# | |
from flask import Flask, request, Response | |
import requests # pip package requests | |
app = Flask(__name__) | |
API_HOST = "http://127.0.0.1:8080/" | |
# @app.route('/', defaults={'path': ''}, methods=["GET", "POST"]) # ref. https://medium.com/@zwork101/making-a-flask-proxy-server-online-in-10-lines-of-code-44b8721bca6 | |
# 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 | |
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 | |
forward = '' | |
if path == 'netron': | |
url = request.args.get('url') | |
forward = f'{API_HOST}/?url={url}' | |
else: | |
forward = request.url.replace(request.host_url, f'{API_HOST}/') | |
res = requests.request( # ref. https://stackoverflow.com/a/36601467/248616 | |
method = request.method, | |
url = forward, | |
headers = {k:v for k,v in request.headers if k.lower() != 'host'}, # exclude 'host' header | |
data = request.get_data(), | |
cookies = request.cookies, | |
allow_redirects = False, | |
) | |
#region exlcude some keys in :res response | |
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 | |
headers = [ | |
(k,v) for k,v in res.raw.headers.items() | |
if k.lower() not in excluded_headers | |
] | |
#endregion exlcude some keys in :res response | |
response = Response(res.content, res.status_code, headers) | |
return response | |
def chooser(): | |
return """ | |
<html> | |
<head> | |
<title>Netron</title> | |
<script type="text/javascript"> | |
function setFileBefore() { | |
var url = document.getElementById('urlbefore').value; | |
var iframe = document.getElementById('netronbefore'); | |
iframe.src = '/netron?url=' + url; | |
} | |
function setFileAfter() { | |
var url = document.getElementById('urlafter').value; | |
var iframe = document.getElementById('netronafter'); | |
iframe.src = '/netron?url=' + url; | |
} | |
console.log("Query params", window.location.search); | |
</script> | |
</head> | |
<body style="padding: 0; margin: 0; display: flex; flex-direction: row; align-items: center;"> | |
<div id="before" style="width: 50%; height: 100%; border-right: 1px solid gray"> | |
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between;"> | |
<input type="text" id="urlbefore" value="https://huggingface.co./brianronan/chessbot-test/resolve/main/chessbot.pb" style="width: calc(100% - 100px); height: 30px;"/> | |
<button type="submit" style="height: 30px; width: 100px;" onClick="setFileBefore()">Load</button> | |
</div> | |
<iframe id="netronbefore" src="/netron?url=https://huggingface.co./brianronan/chessbot-test/resolve/main/chessbot.pb" style="width: 100%; height: calc(100% - 30px); border: none;"></iframe> | |
</div> | |
<div id="after" style="width: 50%; height: 100%;"> | |
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between;"> | |
<input type="text" id="urlafter" value="https://huggingface.co./brianronan/chessbot-test/resolve/updated/chessbot.pb" style="width: calc(100% - 100px); height: 30px;"/> | |
<button type="submit" style="height: 30px; width: 100px;" onClick="setFileAfter()">Load</button> | |
</div> | |
<iframe id="netronafter" src="/netron?url=https://huggingface.co./brianronan/chessbot-test/resolve/updated/chessbot.pb" style="width: 100%; height: calc(100% - 30px); border: none;"></iframe> | |
</div> | |
</body> | |
</html> | |
""" |