-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
91 lines (69 loc) · 2.68 KB
/
Copy pathapp.py
File metadata and controls
91 lines (69 loc) · 2.68 KB
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
85
86
87
88
89
90
91
import numpy as np
from flask import Flask, request, jsonify
import pickle
# The model was created using scikit-learn==1.4.1.post1 so we import this version here to avoid warnings.
app = Flask(__name__)
model = pickle.load(open("model.pkl", "rb")) # Loading the model
# model = joblib.load('model.joblib')
encoding = pickle.load(open("encoding_dict.pkl", "rb"))
scaler = pickle.load(open("scaler.pkl", "rb"))
@app.route("/predict", methods=["POST"])
def predict():
json_dic = request.get_json()
# Getting the input data from the user
house_type = json_dic.get("House_Type")
municipality = json_dic.get("Municipality")
living_Area = json_dic.get("Living_Area")
built_On = json_dic.get("Built_On")
rooms = json_dic.get("Rooms")
lift = (1 if json_dic.get("Lift") == "Yes" else 0)
balcony = (1 if json_dic.get("Balcony") == "Yes" else 0)
plot_Area = json_dic.get("Plot_Area")
other_Area = json_dic.get("Other_Area")
municipality_encode = encoding[municipality]
# Altering the user data to fit for the input to the model
value1 = []
if (built_On == "1900-1950"):
value1 = [1, 0, 0, 0, 0]
elif (built_On == "1951-2000"):
value1 = [0, 1, 0, 0, 0]
elif (built_On == "2001-2010"):
value1 = [0, 0, 1, 0, 0]
elif (built_On == "2011-present"):
value1 = [0, 0, 0, 1, 0]
elif (built_On == "Before 1900s"):
value1 = [0, 0, 0, 0, 1]
value2 = []
if (house_type == "Fritidshus"):
value2 = [1, 0, 0, 0, 0, 0]
elif (house_type == "Kedjehus"):
value2 = [0, 1, 0, 0, 0, 0]
elif (house_type == "Lägenhet"):
value2 = [0, 0, 1, 0, 0, 0]
elif (house_type == "Parhus"):
value2 = [0, 0, 0, 1, 0, 0]
elif (house_type == "Tomt"):
value2 = [0, 0, 0, 0, 1, 0]
elif (house_type == "Villa"):
value2 = [0, 0, 0, 0, 0, 1]
value3 = []
if (house_type == "Lägenhet"):
value3 = [municipality_encode, rooms,
lift, balcony, living_Area, 0, 0]
else:
value3 = [municipality_encode, 0, 0,
0, living_Area, plot_Area, other_Area]
final_values = value3 + value1 + value2
final_values = np.array(final_values)
final_values_matrix = final_values.reshape(1, -1)
final_values_matrix[:, (1, 4, 5, 6)] = scaler.transform(
final_values_matrix[:, (1, 4, 5, 6)])
prediction = model.predict(final_values_matrix)
return jsonify({"value": np.array2string(round(prediction[0], 3))})
@app.route("/")
def root():
app = Flask(__name__, static_url_path='/static')
return app.send_static_file("index.html")
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0", port=5000)