-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
84 lines (75 loc) · 2.33 KB
/
main.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import subprocess
from flask import Flask, render_template, request
import os
import json
import Masuku
import re
from werkzeug.utils import secure_filename
from flasgger import Swagger
# Inference Model
model = Masuku.model(os.path.join("models", "newbest.onnx"))
# Flask App
UPLOAD_FOLDER = './static/uploads'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
swagger = Swagger(app)
def syntax_highlight(json_data):
json_str = json.dumps(json_data, indent=2)
json_str = json_str.replace("&", "&").replace("<", "<").replace(">", ">")
def replacer(match):
cls = "number"
if match.group(0).startswith('"'):
if match.group(0).endswith(':'):
cls = "key"
else:
cls = "string"
elif re.fullmatch("true|false", match.group(0)):
cls = "boolean"
elif re.fullmatch("null", match.group(0)):
cls = "null"
return '<span class="' + cls + '">' + match.group(0) + "</span>"
pattern = r'"(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?'
json_str = re.sub(pattern, replacer, json_str)
return json_str
@app.route("/")
def index():
"""
Home Page
---
responses:
200:
description: The home page is returned
"""
return render_template("index.html")
@app.route('/upload', methods=['POST'])
def upload_file():
"""
File Upload
---
parameters:
- in: formData
name: file
type: file
required: true
description: The file to upload
responses:
200:
description: The result of the file processing
"""
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
json_data = model.infer(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print(json_data)
# return f"{json_data}" ,200
return syntax_highlight(json_data)
if __name__ == "__main__":
app.debug = True
if app.debug:
subprocess.Popen("npx tailwindcss -i ./static/base.css -o ./static/style.css --watch", shell=True)
app.run()