-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
170 lines (159 loc) · 4.94 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Import librairies and functions from other files
import pandas as pd
import numpy as np
import streamlit as st
from preprocessing.cleaning_data import dataframe_zip_code, preprocess
from predict.prediction import predict
def main():
"""
Main script to launch the app and get input data from users
for predicting real estate prices.
"""
# Loading CSS style
st.markdown(
"<style>" + open("style.css").read() + "</style>", unsafe_allow_html=True
)
# Sidebar
with st.sidebar:
st.sidebar.header("How to use the app")
st.sidebar.write(
"""
1. Fill in all the fields in the form.
2. Click on 'See the result'.
3. Your prediction will appear below the button.
"""
)
st.sidebar.header("The features")
st.sidebar.write(
"This app is based on a linear regression. Here are the features used to predict the price:"
)
st.sidebar.write(
"""
1. The property type
2. Mean income in the zip code
3. Median price in the district (based on the property)
4. The district
5. Living area
6. Surface of the plot
7. The building condition
8. Swimming pool
"""
)
# Title and subheader
st.title("Real estate price prediction")
# Space
st.markdown(
"<style>div.block-container{padding-top:4rem;}</style>", unsafe_allow_html=True
)
# Input data: property type
house_types = [
"House",
"Bungalow",
"Castle",
"Chalet",
"Country cottage",
"Exceptional property",
"Farmhouse",
"Manor house",
"Mansion",
"Town house",
"Villa",
]
apartment_types = [
"Apartment",
"Loft",
"Penthouse",
"Triplex",
"Duplex",
"Studio",
"Kot",
]
with st.container():
property = st.radio(
"Is it a house or an apartment?",
options=["House", "Apartment"],
index=0,
horizontal=True,
)
if property == "House":
property_type = st.selectbox(
"What's the type of house?", options=house_types
)
elif property == "Apartment":
property_type = st.selectbox(
"What's the type of apartment?", options=apartment_types
)
# Input data: ZIP CODE
with st.container():
zip_code = st.number_input(
"Where is it located? Enter the zip code:",
placeholder="1000",
min_value=1000,
max_value=9999,
)
zip_code_df = dataframe_zip_code()
# Checking if the zip code exists
if zip_code not in zip_code_df["Postal code"].values:
st.warning("Enter a valid zip code")
# Input data: surfaces
with st.container():
col1, col2 = st.columns(2)
# LIVING AREA
living_area = col1.number_input(
"What's the living area (in square meters)?", placeholder=100, min_value=5
)
if living_area < 5:
col1.warning("Enter a valid answer")
# SURFACE OF THE PLOT
surface_plot = col2.number_input(
"What's the surface of the plot (in square meters)?",
placeholder=100,
min_value=5,
)
if surface_plot < 5:
col2.warning("Please enter a valid answer")
# Input data: BUILDING CONDITION
with st.container():
options_condition = [
"As new",
"Just renovated",
"Good",
"To be done up",
"To renovate",
"To restore",
]
building_condition = st.selectbox(
"What's the building condition?", options=options_condition, index=2
)
# Input data: SWIMMING POOL
with st.container():
swimming_pool = st.radio(
"Does the property have a swimming pool?",
options=["Yes", "No"],
index=1,
horizontal=True,
)
result = ""
# Calling the functions to clean the input data and get a price prediction once the user click oon the button
try:
if st.button("See the result"):
columns_data = preprocess(
property,
property_type,
zip_code,
living_area,
surface_plot,
building_condition,
swimming_pool,
)
result = predict(columns_data)
result = int(result)
# Display the price prediction stored in the variable "result"
st.markdown(
f"<div style='text-align:center; border-radius:15px;witdh: 100%; background-color: #27334e;color: #fff; font-size:30px;padding:20px;'>The predicted price is <br> <b>{(result)}</b> €</div>",
unsafe_allow_html=True,
)
except:
print("Please fill in the form with correct info")
if __name__ == "__main__":
main()