forked from shanakaChathu/churn_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
69 lines (58 loc) · 1.98 KB
/
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
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
from flask import Flask, render_template, request, jsonify
import os
import numpy as np
import yaml
import joblib
webapp_root = "webapp"
params_path = "params.yaml"
static_dir = os.path.join(webapp_root, "static")
template_dir = os.path.join(webapp_root, "templates")
app = Flask(__name__, static_folder=static_dir,template_folder=template_dir)
class NotANumber(Exception):
def __init__(self, message="Values entered are not Numerical"):
self.message = message
super().__init__(self.message)
def read_params(config_path):
with open(config_path) as yaml_file:
config = yaml.safe_load(yaml_file)
return config
def predict(data):
config = read_params(params_path)
model_dir_path = config["model_webapp_dir"]
model = joblib.load(model_dir_path)
prediction = model.predict(data).tolist()[0]
return prediction
def validate_input(dict_request):
for _, val in dict_request.items():
try:
val=float(val)
except Exception as e:
raise NotANumber
return True
def form_response(dict_request):
try:
if validate_input(dict_request):
data = dict_request.values()
data = [list(map(float, data))]
response = predict(data)
return response
except NotANumber as e:
response = str(e)
return response
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
try:
if request.form:
dict_req = dict(request.form)
response = form_response(dict_req)
return render_template("index.html", response=response)
except Exception as e:
print(e)
error = {"error": "Something went wrong!! Try again later!"}
error = {"error": e}
return render_template("404.html", error=error)
else:
return render_template("index.html")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)