-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (63 loc) · 2.04 KB
/
Copy pathapp.py
File metadata and controls
79 lines (63 loc) · 2.04 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
from flask import Flask, request, url_for, render_template
import pickle
import numpy as np
import json
app = Flask(__name__)
app.config.update(
dict(SECRET_KEY="powerful secretkey", WTF_CSRF_SECRET_KEY="a csrf secret key")
)
__data_columns = None
__locations = None
model = None
def load_saved_artifacts():
print("Loading the saved artifacts...start !")
global __data_columns
global __locations
global model
# Load Model
with open("bangalore_home_prices_model.pickle", "rb") as f:
model = pickle.load(f)
# Load Columns
with open("columns.json", "r") as f:
__data_columns = json.loads(f.read())["data_columns"]
__locations = __data_columns[3:] # first 3 are sqft, bath, bhk
# Initialize artifacts immediately
load_saved_artifacts()
def get_estimated_price(input_json):
try:
loc_index = __data_columns.index(input_json['location'].lower())
except:
loc_index = -1
x = np.zeros(len(__data_columns))
x[0] = input_json['sqft']
x[1] = input_json['bath']
x[2] = input_json['bhk']
if loc_index >= 0:
x[loc_index] = 1
result = round(model.predict([x])[0],2)
return result
@app.route("/get_location_names", methods=["GET"])
def get_location_names_route():
return json.dumps({"locations": __locations})
@app.route("/")
def index():
return render_template('index.html')
@app.route("/prediction", methods=["POST"])
def prediction():
if request.method == 'POST':
input_json = {
"location": request.form['sLocation'],
"sqft": request.form['Squareft'],
"bhk": request.form['uiBHK'],
"bath": request.form['uiBathrooms']
}
result = get_estimated_price(input_json)
if result > 100:
result = round(result/100, 2)
result = str(result) + ' Crore'
else:
result = str(result) + ' Lakhs'
return render_template('prediction.html', result=result)
if __name__ == "__main__":
print("Starting Python Flask Server")
app.run(debug=True)