-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
118 lines (111 loc) · 3.87 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Dash app
import dash
from dash import html, dcc, Input, Output, State
import pandas as pd
import pickle
# Load Model
model = pickle.load(open("./models/model.pkl", "rb"))
# Initialize the Dash app
app = dash.Dash(__name__)
# Define the layout of the app
app.layout = html.Div(
[
html.Div(
[
html.H1("Real Estate Price Prediction", style={"text-align": "center"}),
html.Div(
[
dcc.Input(
id="distance_to_mrt",
type="number",
placeholder="Distance to the nearest MRT station",
style={"margin": "10px", "padding": "10px"},
),
dcc.Input(
id="num_convenience_stores",
type="number",
placeholder="Number of Convenience Stores",
style={"margin": "10px", "padding": "10px"},
),
dcc.Input(
id="latitude",
type="number",
placeholder="Latitude",
style={"margin": "10px", "padding": "10px"},
),
dcc.Input(
id="longitude",
type="number",
placeholder="Longitude",
style={"margin": "10px", "padding": "10px"},
),
html.Button(
"Predict Price",
id="predict_button",
n_clicks=0,
style={
"margin": "10px",
"padding": "10px",
"background-color": "#007BFF",
"color": "white",
},
),
],
style={"text-align": "center"},
),
html.Div(
id="prediction_output",
style={
"text-align": "center",
"font-size": "20px",
"margin-top": "20px",
},
),
],
style={
"width": "50%",
"margin": "0 auto",
"border": "2px solid #007BFF",
"padding": "20px",
"border-radius": "10px",
},
)
]
)
# Define a callback to update output
@app.callback(
Output("prediction_output", "children"),
[Input("predict_button", "n_clicks")],
[
State("distance_to_mrt", "value"),
State("num_convenience_stores", "value"),
State("latitude", "value"),
State("longitude", "value"),
],
)
def update_output(
n_clicks, distance_to_mrt, num_convenience_stores, latitude, longitude
):
if n_clicks > 0 and all(
v is not None
for v in [distance_to_mrt, num_convenience_stores, latitude, longitude]
):
# Prepare the featuer vector
features = pd.DataFrame(
[[distance_to_mrt, num_convenience_stores, latitude, longitude]],
columns=[
"Distance to the nearest MRT station",
"Number of convenience stores",
"Latitude",
"Longitude",
],
)
# Predict
prediction = model.predict(features)[0]
return f"Predicted House Price of Unit Area: {prediction:.2f}"
elif n_clicks > 0:
return "Please enter all values to get a prediction"
return
# Run the app
if __name__ == "__main__":
app.run_server(debug=True, port="8050")