-
Notifications
You must be signed in to change notification settings - Fork 9
/
flask_app.py
43 lines (26 loc) · 1.02 KB
/
flask_app.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
from flask import Flask, Request, request, render_template
from werkzeug.datastructures import ImmutableOrderedMultiDict
from predict import predict
import pandas as pd
class OrderedRequest(Request):
parameter_storage_class = ImmutableOrderedMultiDict
class MyFlask(Flask):
request_class = OrderedRequest
app = MyFlask(__name__, static_folder="public", template_folder="views")
@app.route("/")
def homepage():
return render_template("index.html")
@app.route("/api/predict")
def loan_risk_predict():
assert isinstance(request.args, ImmutableOrderedMultiDict)
prediction = predict(request.args)
if pd.isna(prediction):
return {"error": "There's something wrong with your input."}, 400
if prediction < 0:
prediction = 0
elif prediction > 1:
prediction = 1
description = f"This loan is predicted to recover {round(prediction * 100, 1)}% of its expected return."
return {"value": str(prediction), "description": description}
if __name__ == "__main__":
app.run()