-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (44 loc) · 1.6 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
import numpy as np
import onnxruntime as rt
from flask import Flask, render_template, request
app = Flask(__name__)
session = rt.InferenceSession("model.onnx")
def is_valid_date(year: int, month: int):
if year < 1950 or year > 2050:
return False
if month < 1 or month > 12:
return False
return True
@app.route("/", methods=["POST", "GET"])
def home():
if request.method == "POST":
form_data = request.form
year = form_data.get("year")
month = form_data.get("month")
if not year or not month:
return render_template(
"index.html", month_post=0, result_post="not a valid date!",
)
try:
year = int(year)
month = int(month)
except ValueError:
return render_template(
"index.html", month_post=0, result_post="not a valid date!",
)
if not is_valid_date(year, month):
return render_template(
"index.html", month_post=0, result_post="not a valid date!",
)
input_name = session.get_inputs()[0].name
label_name = session.get_outputs()[0].name
data = np.array([[year, month]], dtype=np.float32)
pred_onx = session.run([label_name], {input_name: data})[0]
res = str(round(pred_onx[0, 0], 2))
result = f"{res} mm"
return render_template(
"index.html", month_post=month, year_post=year, result_post=result
)
return render_template("index.html", month_post=0, result_post="")
if __name__ == "__main__":
app.run(debug=True)