File size: 2,628 Bytes
4fb0ad8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a007d51
 
4fb0ad8
 
0ac63a7
 
 
 
8047141
0ac63a7
 
a007d51
 
6d5ce82
8047141
0ac63a7
6d5ce82
4fb0ad8
 
 
 
 
 
6d5ce82
4fb0ad8
 
6d5ce82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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

@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
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

from huggingface_hub import hf_hub_url

@app.get("/")
def chooser():
    repo_id = request.args.get("repo_id")
    filename = request.args.get("filename")

    if repo_id is None or filename is None:
        print("repo_id or filename is missing", repo_id, filename)
        return "" 

    file_url = hf_hub_url(request.args.get("repo_id"), request.args.get("filename"))

    if file_url is None:
        print("could not find file url")
        return ""

    return """
<html>
    <head>
        <title>Netron</title>
    </head>
    <body style="padding: 0; margin: 0; display: flex; flex-direction: row; align-items: center;">
        <iframe id="netron" src="/netron?url={file_url}" style="width: 100%; height: 100%; border: none;"></iframe>
    </body>
</html>
""".format(file_url = file_url)